micro/registry/encoding_test.go

162 lines
2.9 KiB
Go
Raw Normal View History

2016-03-17 00:23:41 +03:00
package registry
import (
2016-03-29 20:16:55 +03:00
"encoding/json"
2016-03-17 00:23:41 +03:00
"testing"
)
func TestEncodingEndpoints(t *testing.T) {
2016-03-29 20:16:55 +03: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-17 00:23:41 +03:00
},
},
}
2016-03-29 20:16:55 +03:00
testEp := func(ep *Endpoint, enc string) {
// encode endpoint
e := encodeEndpoints([]*Endpoint{ep})
2016-03-17 00:23:41 +03:00
2016-03-29 20:16:55 +03:00
// check there are two tags; old and new
if len(e) != 2 {
2016-04-06 20:03:27 +03:00
t.Fatalf("Expected 2 encoded tags, got %v", e)
2016-03-17 00:23:41 +03:00
}
2016-03-29 20:16:55 +03:00
// check old encoding
var seen bool
2016-03-17 00:23:41 +03:00
2016-03-29 20:16:55 +03:00
for _, en := range e {
if en == enc {
seen = true
break
}
2016-03-17 00:23:41 +03:00
}
2016-03-29 20:16:55 +03:00
if !seen {
2016-04-06 20:03:27 +03:00
t.Fatalf("Expected %s but not found", enc)
2016-03-17 00:23:41 +03:00
}
2016-03-29 20:16:55 +03:00
// decode
d := decodeEndpoints([]string{enc})
2016-03-17 00:23:41 +03:00
if len(d) == 0 {
2016-03-29 20:16:55 +03:00
t.Fatalf("Expected %v got %v", ep, d)
2016-03-17 00:23:41 +03:00
}
2016-03-29 20:16:55 +03:00
// check name
if d[0].Name != ep.Name {
t.Fatalf("Expected ep %s got %s", ep.Name, d[0].Name)
2016-03-17 00:23:41 +03:00
}
2016-03-29 20:16:55 +03:00
// check all the metadata exists
for k, v := range ep.Metadata {
2016-03-17 00:23:41 +03: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 20:16:55 +03: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
jepTag := "e=" + string(jencoded)
hepTag := "e-" + hencoded
// test old
testEp(ep, jepTag)
// test new
testEp(ep, hepTag)
}
2016-03-17 00:23:41 +03:00
}
func TestEncodingVersion(t *testing.T) {
testData := []struct {
decoded string
encoded string
oldEncoded string
}{
{"1.0.0", "v-789c32d433d03300040000ffff02ce00ee", "v=1.0.0"},
{"latest", "v-789cca492c492d2e01040000ffff08cc028e", "v=latest"},
}
for _, data := range testData {
e := encodeVersion(data.decoded)
if e[1] != data.encoded {
2016-03-17 00:23:41 +03:00
t.Fatalf("Expected %s got %s", data.encoded, e)
}
d, ok := decodeVersion(e)
2016-03-17 00:23:41 +03:00
if !ok {
2016-04-06 20:03:27 +03:00
t.Fatalf("Unexpected %t for %s", ok, data.encoded)
2016-03-17 00:23:41 +03:00
}
if d != data.decoded {
2016-04-06 20:03:27 +03:00
t.Fatalf("Expected %s got %s", data.decoded, d)
2016-03-17 00:23:41 +03:00
}
d, ok = decodeVersion([]string{data.encoded})
if !ok {
2016-04-06 20:03:27 +03:00
t.Fatalf("Unexpected %t for %s", ok, data.encoded)
2016-03-17 00:23:41 +03:00
}
if d != data.decoded {
2016-04-06 20:03:27 +03:00
t.Fatalf("Expected %s got %s", data.decoded, d)
2016-03-17 00:23:41 +03:00
}
d, ok = decodeVersion([]string{data.oldEncoded})
if !ok {
2016-04-06 20:03:27 +03:00
t.Fatalf("Unexpected %t for %s", ok, data.oldEncoded)
2016-03-17 00:23:41 +03:00
}
if d != data.decoded {
2016-04-06 20:03:27 +03:00
t.Fatalf("Expected %s got %s", data.decoded, d)
2016-03-17 00:23:41 +03:00
}
}
}