github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/registry/consul/encoding_test.go (about)

     1  package consul
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/volts-dev/volts/registry"
     8  )
     9  
    10  func TestEncodingEndpoints(t *testing.T) {
    11  	eps := []*registry.Endpoint{
    12  		{
    13  			Name: "endpoint1",
    14  			Request: &registry.Value{
    15  				Name: "request",
    16  				Type: "request",
    17  			},
    18  			Response: &registry.Value{
    19  				Name: "response",
    20  				Type: "response",
    21  			},
    22  			Metadata: map[string]string{
    23  				"foo1": "bar1",
    24  			},
    25  		},
    26  		{
    27  			Name: "endpoint2",
    28  			Request: &registry.Value{
    29  				Name: "request",
    30  				Type: "request",
    31  			},
    32  			Response: &registry.Value{
    33  				Name: "response",
    34  				Type: "response",
    35  			},
    36  			Metadata: map[string]string{
    37  				"foo2": "bar2",
    38  			},
    39  		},
    40  		{
    41  			Name: "endpoint3",
    42  			Request: &registry.Value{
    43  				Name: "request",
    44  				Type: "request",
    45  			},
    46  			Response: &registry.Value{
    47  				Name: "response",
    48  				Type: "response",
    49  			},
    50  			Metadata: map[string]string{
    51  				"foo3": "bar3",
    52  			},
    53  		},
    54  	}
    55  
    56  	testEp := func(ep *registry.Endpoint, enc string) {
    57  		// encode endpoint
    58  		e := encodeEndpoints([]*registry.Endpoint{ep})
    59  
    60  		// check there are two tags; old and new
    61  		if len(e) != 1 {
    62  			t.Fatalf("Expected 1 encoded tags, got %v", e)
    63  		}
    64  
    65  		// check old encoding
    66  		var seen bool
    67  
    68  		for _, en := range e {
    69  			if en == enc {
    70  				seen = true
    71  				break
    72  			}
    73  		}
    74  
    75  		if !seen {
    76  			t.Fatalf("Expected %s but not found", enc)
    77  		}
    78  
    79  		// decode
    80  		d := decodeEndpoints([]string{enc})
    81  		if len(d) == 0 {
    82  			t.Fatalf("Expected %v got %v", ep, d)
    83  		}
    84  
    85  		// check name
    86  		if d[0].Name != ep.Name {
    87  			t.Fatalf("Expected ep %s got %s", ep.Name, d[0].Name)
    88  		}
    89  
    90  		// check all the metadata exists
    91  		for k, v := range ep.Metadata {
    92  			if gv := d[0].Metadata[k]; gv != v {
    93  				t.Fatalf("Expected key %s val %s got val %s", k, v, gv)
    94  			}
    95  		}
    96  	}
    97  
    98  	for _, ep := range eps {
    99  		// JSON encoded
   100  		jencoded, err := json.Marshal(ep)
   101  		if err != nil {
   102  			t.Fatal(err)
   103  		}
   104  
   105  		// HEX encoded
   106  		hencoded := encode(jencoded)
   107  		// endpoint tag
   108  		hepTag := "e-" + hencoded
   109  		testEp(ep, hepTag)
   110  	}
   111  }
   112  
   113  func TestEncodingVersion(t *testing.T) {
   114  	testData := []struct {
   115  		decoded string
   116  		encoded string
   117  	}{
   118  		{"1.0.0", "v-789c32d433d03300040000ffff02ce00ee"},
   119  		{"latest", "v-789cca492c492d2e01040000ffff08cc028e"},
   120  	}
   121  
   122  	for _, data := range testData {
   123  		e := encodeVersion(data.decoded)
   124  
   125  		if e[0] != data.encoded {
   126  			t.Fatalf("Expected %s got %s", data.encoded, e)
   127  		}
   128  
   129  		d, ok := decodeVersion(e)
   130  		if !ok {
   131  			t.Fatalf("Unexpected %t for %s", ok, data.encoded)
   132  		}
   133  
   134  		if d != data.decoded {
   135  			t.Fatalf("Expected %s got %s", data.decoded, d)
   136  		}
   137  
   138  		d, ok = decodeVersion([]string{data.encoded})
   139  		if !ok {
   140  			t.Fatalf("Unexpected %t for %s", ok, data.encoded)
   141  		}
   142  
   143  		if d != data.decoded {
   144  			t.Fatalf("Expected %s got %s", data.decoded, d)
   145  		}
   146  	}
   147  }