github.com/m-lab/locate@v0.17.6/locatetest/locatetest_test.go (about) 1 package locatetest 2 3 import ( 4 "context" 5 "errors" 6 "net/http/httptest" 7 "net/url" 8 "testing" 9 10 "github.com/m-lab/go/testingx" 11 "github.com/m-lab/locate/api/locate" 12 ) 13 14 func TestLocateServer_Success(t *testing.T) { 15 tests := []struct { 16 name string 17 srv *httptest.Server 18 path string 19 want int 20 }{ 21 { 22 name: "success-locate-server-v2", 23 srv: NewLocateServerV2(&LocatorV2{ 24 Servers: []string{"127.0.0.1"}, 25 }), 26 path: "/v2/nearest", 27 want: 1, 28 }, 29 } 30 31 for _, tt := range tests { 32 t.Run(tt.name, func(t *testing.T) { 33 c := locate.NewClient("fake-user-agent") 34 u, err := url.Parse(tt.srv.URL) 35 testingx.Must(t, err, "failed to parse locatetest url") 36 u.Path = tt.path 37 c.BaseURL = u 38 39 ctx := context.Background() 40 // NOTE: only known services (e.g. ndt/ndt7) are supported by the locate API. 41 targets, err := c.Nearest(ctx, "ndt/ndt7") 42 testingx.Must(t, err, "failed to get response from locatetest server") 43 44 if tt.want != len(targets) { 45 t.Errorf("NewLocateServer() = got %d, want %d", len(targets), tt.want) 46 } 47 }) 48 } 49 } 50 51 func TestLocateServer_Error(t *testing.T) { 52 tests := []struct { 53 name string 54 srv *httptest.Server 55 path string 56 }{ 57 { 58 name: "error-locate-server-v2", 59 srv: NewLocateServerV2(&LocatorV2{ 60 Err: errors.New("fake error"), 61 }), 62 path: "/v2/nearest", 63 }, 64 } 65 66 for _, tt := range tests { 67 t.Run(tt.name, func(t *testing.T) { 68 c := locate.NewClient("fake-user-agent") 69 u, err := url.Parse(tt.srv.URL) 70 testingx.Must(t, err, "failed to parse locatetest url") 71 u.Path = tt.path 72 c.BaseURL = u 73 74 ctx := context.Background() 75 // NOTE: only known services (e.g. ndt/ndt7) are supported by the locate API. 76 targets, err := c.Nearest(ctx, "ndt/ndt7") 77 if err == nil { 78 t.Errorf("expected error, got %#v", targets) 79 } 80 }) 81 } 82 }