github.com/btccom/go-micro/v2@v2.9.3/api/api_test.go (about) 1 package api 2 3 import ( 4 "strings" 5 "testing" 6 ) 7 8 func TestEncoding(t *testing.T) { 9 testData := []*Endpoint{ 10 nil, 11 { 12 Name: "Foo.Bar", 13 Description: "A test endpoint", 14 Handler: "meta", 15 Host: []string{"foo.com"}, 16 Method: []string{"GET"}, 17 Path: []string{"/test"}, 18 }, 19 } 20 21 compare := func(expect, got []string) bool { 22 // no data to compare, return true 23 if len(expect) == 0 && len(got) == 0 { 24 return true 25 } 26 // no data expected but got some return false 27 if len(expect) == 0 && len(got) > 0 { 28 return false 29 } 30 31 // compare expected with what we got 32 for _, e := range expect { 33 var seen bool 34 for _, g := range got { 35 if e == g { 36 seen = true 37 break 38 } 39 } 40 if !seen { 41 return false 42 } 43 } 44 45 // we're done, return true 46 return true 47 } 48 49 for _, d := range testData { 50 // encode 51 e := Encode(d) 52 // decode 53 de := Decode(e) 54 55 // nil endpoint returns nil 56 if d == nil { 57 if e != nil { 58 t.Fatalf("expected nil got %v", e) 59 } 60 if de != nil { 61 t.Fatalf("expected nil got %v", de) 62 } 63 64 continue 65 } 66 67 // check encoded map 68 name := e["endpoint"] 69 desc := e["description"] 70 method := strings.Split(e["method"], ",") 71 path := strings.Split(e["path"], ",") 72 host := strings.Split(e["host"], ",") 73 handler := e["handler"] 74 75 if name != d.Name { 76 t.Fatalf("expected %v got %v", d.Name, name) 77 } 78 if desc != d.Description { 79 t.Fatalf("expected %v got %v", d.Description, desc) 80 } 81 if handler != d.Handler { 82 t.Fatalf("expected %v got %v", d.Handler, handler) 83 } 84 if ok := compare(d.Method, method); !ok { 85 t.Fatalf("expected %v got %v", d.Method, method) 86 } 87 if ok := compare(d.Path, path); !ok { 88 t.Fatalf("expected %v got %v", d.Path, path) 89 } 90 if ok := compare(d.Host, host); !ok { 91 t.Fatalf("expected %v got %v", d.Host, host) 92 } 93 94 if de.Name != d.Name { 95 t.Fatalf("expected %v got %v", d.Name, de.Name) 96 } 97 if de.Description != d.Description { 98 t.Fatalf("expected %v got %v", d.Description, de.Description) 99 } 100 if de.Handler != d.Handler { 101 t.Fatalf("expected %v got %v", d.Handler, de.Handler) 102 } 103 if ok := compare(d.Method, de.Method); !ok { 104 t.Fatalf("expected %v got %v", d.Method, de.Method) 105 } 106 if ok := compare(d.Path, de.Path); !ok { 107 t.Fatalf("expected %v got %v", d.Path, de.Path) 108 } 109 if ok := compare(d.Host, de.Host); !ok { 110 t.Fatalf("expected %v got %v", d.Host, de.Host) 111 } 112 } 113 } 114 115 func TestValidate(t *testing.T) { 116 epPcre := &Endpoint{ 117 Name: "Foo.Bar", 118 Description: "A test endpoint", 119 Handler: "meta", 120 Host: []string{"foo.com"}, 121 Method: []string{"GET"}, 122 Path: []string{"^/test/?$"}, 123 } 124 if err := Validate(epPcre); err != nil { 125 t.Fatal(err) 126 } 127 128 epGpath := &Endpoint{ 129 Name: "Foo.Bar", 130 Description: "A test endpoint", 131 Handler: "meta", 132 Host: []string{"foo.com"}, 133 Method: []string{"GET"}, 134 Path: []string{"/test/{id}"}, 135 } 136 if err := Validate(epGpath); err != nil { 137 t.Fatal(err) 138 } 139 140 epPcreInvalid := &Endpoint{ 141 Name: "Foo.Bar", 142 Description: "A test endpoint", 143 Handler: "meta", 144 Host: []string{"foo.com"}, 145 Method: []string{"GET"}, 146 Path: []string{"/test/?$"}, 147 } 148 if err := Validate(epPcreInvalid); err == nil { 149 t.Fatalf("invalid pcre %v", epPcreInvalid.Path[0]) 150 } 151 152 }