Created
November 1, 2020 12:27
-
-
Save RicardoLinck/261e2a88354761b2d41f6d1b935c0d45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Test_FetchData(t *testing.T) { | |
router := http.NewServeMux() | |
router.HandleFunc("/api/v1/some-data", func(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "GET" { | |
w.WriteHeader(http.StatusBadRequest) | |
return | |
} | |
w.Write([]byte(`{"user_name":"test_user", | |
"dob":"1999-05-23T18:25:43.511Z", | |
"fav_sports":[{"name":"football","order":2},{"name":"hockey", "order":1}]}`)) | |
}) | |
router.HandleFunc("/api/v2/data", func(w http.ResponseWriter, r *http.Request) { | |
if r.Method != "GET" { | |
w.WriteHeader(http.StatusBadRequest) | |
return | |
} | |
w.Write([]byte(`{"name":"test_user", | |
"age":21, | |
"sports":["football","hockey"]}`)) | |
}) | |
srv := httptest.NewServer(router) | |
defer srv.Close() | |
t.Run("errors when invalid api version", func(t *testing.T) { | |
c := Configuration{ | |
baseURL: "", | |
apiVersion: 3, | |
} | |
_, err := c.FetchData() | |
assert.Error(t, ErrInvalidAPIVersion, err) | |
}) | |
t.Run("Returns /api/v1/some-data when api version 1", func(t *testing.T) { | |
config := Configuration{ | |
baseURL: srv.URL, | |
apiVersion: 1, | |
} | |
got, err := config.FetchData() | |
assert.NoError(t, err) | |
want := Person{ | |
Name: "test_user", | |
Age: 21, | |
Sports: []string{"hockey", "football"}, | |
} | |
assert.Equal(t, want, got) | |
}) | |
t.Run("Returns /api/v2/data when api version 2", func(t *testing.T) { | |
config := Configuration{ | |
baseURL: srv.URL, | |
apiVersion: 2, | |
} | |
got, err := config.FetchData() | |
assert.NoError(t, err) | |
want := Person{ | |
Name: "test_user", | |
Age: 21, | |
Sports: []string{"football", "hockey"}, | |
} | |
assert.Equal(t, want, got) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment