1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-12-24 10:07:04 +02:00
go-micro/registry/encoding_test.go

147 lines
2.6 KiB
Go
Raw Normal View History

2016-03-16 23:23:41 +02:00
package registry
import (
2016-03-29 19:16:55 +02:00
"encoding/json"
2016-03-16 23:23:41 +02:00
"testing"
)
func TestEncodingEndpoints(t *testing.T) {
2016-03-29 19:16:55 +02:00
eps := []*Endpoint{
&Endpoint{
Name: "endpoint1",
Request: &Value{
Name: "request",
Type: "request",
},
Response: &Value{
Name: "response",
Type: "response",
},
Metadata: map[string]string{
"foo1": "bar1",
},
},
&Endpoint{
Name: "endpoint2",
Request: &Value{
Name: "request",
Type: "request",
},
Response: &Value{
Name: "response",
Type: "response",
},
Metadata: map[string]string{
"foo2": "bar2",
},
},
&Endpoint{
Name: "endpoint3",
Request: &Value{
Name: "request",
Type: "request",
},
Response: &Value{
Name: "response",
Type: "response",
},
Metadata: map[string]string{
"foo3": "bar3",
2016-03-16 23:23:41 +02:00
},
},
}
2016-03-29 19:16:55 +02:00
testEp := func(ep *Endpoint, enc string) {
// encode endpoint
e := encodeEndpoints([]*Endpoint{ep})
2016-03-16 23:23:41 +02:00
2016-03-29 19:16:55 +02:00
// check there are two tags; old and new
2016-05-22 19:41:06 +02:00
if len(e) != 1 {
t.Fatalf("Expected 1 encoded tags, got %v", e)
2016-03-16 23:23:41 +02:00
}
2016-03-29 19:16:55 +02:00
// check old encoding
var seen bool
2016-03-16 23:23:41 +02:00
2016-03-29 19:16:55 +02:00
for _, en := range e {
if en == enc {
seen = true
break
}
2016-03-16 23:23:41 +02:00
}
2016-03-29 19:16:55 +02:00
if !seen {
2016-04-06 19:03:27 +02:00
t.Fatalf("Expected %s but not found", enc)
2016-03-16 23:23:41 +02:00
}
2016-03-29 19:16:55 +02:00
// decode
d := decodeEndpoints([]string{enc})
2016-03-16 23:23:41 +02:00
if len(d) == 0 {
2016-03-29 19:16:55 +02:00
t.Fatalf("Expected %v got %v", ep, d)
2016-03-16 23:23:41 +02:00
}
2016-03-29 19:16:55 +02:00
// check name
if d[0].Name != ep.Name {
t.Fatalf("Expected ep %s got %s", ep.Name, d[0].Name)
2016-03-16 23:23:41 +02:00
}
2016-03-29 19:16:55 +02:00
// check all the metadata exists
for k, v := range ep.Metadata {
2016-03-16 23:23:41 +02:00
if gv := d[0].Metadata[k]; gv != v {
t.Fatalf("Expected key %s val %s got val %s", k, v, gv)
}
}
}
2016-03-29 19:16:55 +02:00
for _, ep := range eps {
// JSON encoded
jencoded, err := json.Marshal(ep)
if err != nil {
t.Fatal(err)
}
// HEX encoded
hencoded := encode(jencoded)
// endpoint tag
hepTag := "e-" + hencoded
testEp(ep, hepTag)
}
2016-03-16 23:23:41 +02:00
}
func TestEncodingVersion(t *testing.T) {
testData := []struct {
2016-05-22 19:41:06 +02:00
decoded string
encoded string
2016-03-16 23:23:41 +02:00
}{
2016-05-22 19:41:06 +02:00
{"1.0.0", "v-789c32d433d03300040000ffff02ce00ee"},
{"latest", "v-789cca492c492d2e01040000ffff08cc028e"},
2016-03-16 23:23:41 +02:00
}
for _, data := range testData {
e := encodeVersion(data.decoded)
2016-05-22 19:41:06 +02:00
if e[0] != data.encoded {
2016-03-16 23:23:41 +02:00
t.Fatalf("Expected %s got %s", data.encoded, e)
}
d, ok := decodeVersion(e)
2016-03-16 23:23:41 +02:00
if !ok {
2016-04-06 19:03:27 +02:00
t.Fatalf("Unexpected %t for %s", ok, data.encoded)
2016-03-16 23:23:41 +02:00
}
if d != data.decoded {
2016-04-06 19:03:27 +02:00
t.Fatalf("Expected %s got %s", data.decoded, d)
2016-03-16 23:23:41 +02:00
}
d, ok = decodeVersion([]string{data.encoded})
if !ok {
2016-04-06 19:03:27 +02:00
t.Fatalf("Unexpected %t for %s", ok, data.encoded)
2016-03-16 23:23:41 +02:00
}
if d != data.decoded {
2016-04-06 19:03:27 +02:00
t.Fatalf("Expected %s got %s", data.decoded, d)
2016-03-16 23:23:41 +02:00
}
}
}