diff options
| author | jwijenbergh <jeroenwijenbergh@protonmail.com> | 2024-02-06 16:26:05 +0100 |
|---|---|---|
| committer | Jeroen Wijenbergh <46386452+jwijenbergh@users.noreply.github.com> | 2024-02-19 14:15:07 +0100 |
| commit | 819d7f9914cbb34abb76b932c05b030a34986ec2 (patch) | |
| tree | 7da743c1a0363e800a041e0310ca8cf441bc3f90 /internal/server/api/api_test.go | |
| parent | 4d26c8489b09acc98128715e9a2ed67558eb8105 (diff) | |
Server: Delete old files to prepare for new internal API
Diffstat (limited to 'internal/server/api/api_test.go')
| -rw-r--r-- | internal/server/api/api_test.go | 150 |
1 files changed, 0 insertions, 150 deletions
diff --git a/internal/server/api/api_test.go b/internal/server/api/api_test.go deleted file mode 100644 index 2fea4c6..0000000 --- a/internal/server/api/api_test.go +++ /dev/null @@ -1,150 +0,0 @@ -package api - -import ( - "context" - "encoding/json" - "fmt" - "net/http" - "testing" - - "github.com/eduvpn/eduvpn-common/internal/server/base" - "github.com/eduvpn/eduvpn-common/internal/server/endpoints" - "github.com/eduvpn/eduvpn-common/internal/test" - "github.com/go-errors/errors" -) - -func getErrorMsg(err error) string { - if err == nil { - return "" - } - return err.Error() -} - -func compareEndpoints(ep1 endpoints.Endpoints, ep2 endpoints.Endpoints) bool { - v3_1 := ep1.API.V3 - v3_2 := ep2.API.V3 - return v3_1.API == v3_2.API && v3_1.Authorization == v3_2.Authorization && v3_1.Token == v3_2.Token -} - -func Test_APIGetEndpoints(t *testing.T) { - handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintln(w, "Hello!") - }) - hs := &test.HandlerSet{} - hs.SetHandler(handler) - s := test.NewServer(hs) - defer s.Close() - - c, err := s.Client() - if err != nil { - t.Fatalf("failed to get client for test server endpoints: %v", err) - } - - testCases := []struct { - epl endpoints.List - err error - }{ - { - epl: endpoints.List{ - API: "https://example.com/1", - Authorization: "https://example.com/2", - Token: "https://example.com/3", - }, - err: nil, - }, - { - epl: endpoints.List{ - API: "https://example.com/1", - Authorization: "http://example.com/2", - Token: "http://example.com/3", - }, - err: errors.New("API scheme: 'https', is not equal to authorization scheme: 'http'"), - }, - { - epl: endpoints.List{ - API: "https://example.com/1", - Authorization: "https://example.com/2", - Token: "ftp://example.com/3", - }, - err: errors.New("API scheme: 'https', is not equal to token scheme: 'ftp'"), - }, - { - epl: endpoints.List{ - API: "https://malicious.com/1", - Authorization: "https://example.com/2", - Token: "https://example.com/3", - }, - err: errors.New("API host: 'malicious.com', is not equal to authorization host: 'example.com'"), - }, - { - epl: endpoints.List{ - API: "https://example.com/1", - Authorization: "https://example.com/2", - Token: "https://malicious.com/3", - }, - err: errors.New("API host: 'example.com', is not equal to token host: 'malicious.com'"), - }, - { - epl: endpoints.List{ - API: "https://example.com/1", - Authorization: "https://malicious.com/2", - Token: "https://example.com/3", - }, - err: errors.New("API host: 'example.com', is not equal to authorization host: 'malicious.com'"), - }, - { - epl: endpoints.List{ - API: "https://example.com/1", - Authorization: "https://example.com/2", - Token: "ftp://example.com/3", - }, - err: errors.New("API scheme: 'https', is not equal to token scheme: 'ftp'"), - }, - { - epl: endpoints.List{ - API: "https://example.com/1", - Authorization: "ftp://example.com/2", - Token: "https://example.com/3", - }, - err: errors.New("API scheme: 'https', is not equal to authorization scheme: 'ftp'"), - }, - } - - for _, tc := range testCases { - ep := &endpoints.Endpoints{ - API: endpoints.Versions{ - V3: tc.epl, - }, - } - // Update the handler - hs.SetHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - - jsonStr, err := json.Marshal(ep) - if err != nil { - t.Fatalf("failed to marshal JSON for test case: %v, err: %v", tc, err) - } - - fmt.Fprintln(w, string(jsonStr)) - })) - b := &base.Base{ - URL: s.URL, - HTTPClient: c, - } - err = Endpoints(context.Background(), b) - if getErrorMsg(err) != getErrorMsg(tc.err) { - t.Fatalf("Errors not equal, want err: %v, got: %v", tc.err, err) - } - // The error was not nil, continue because endpoints should not be compared - if tc.err != nil { - continue - } - if ep == nil { - t.Fatalf("No test case endpoints") - } - // if no error then the endpoints should be equal - if !compareEndpoints(*ep, b.Endpoints) { - t.Fatalf("Endpoints are not equal, got: %v, want: %v", b.Endpoints, ep) - } - } -} |
