(t *testing.T)
| 12 | ) |
| 13 | |
| 14 | func TestExchangeCodeForToken(t *testing.T) { |
| 15 | t.Run("Successfully call token endpoint", func(t *testing.T) { |
| 16 | ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 17 | w.Header().Set("Content-Type", "application/json") |
| 18 | _, err := io.WriteString(w, `{ |
| 19 | "access_token": "access-token-here", |
| 20 | "id_token": "id-token-here", |
| 21 | "token_type": "token-type-here", |
| 22 | "expires_in": 1000 |
| 23 | }`) |
| 24 | require.NoError(t, err) |
| 25 | })) |
| 26 | |
| 27 | defer ts.Close() |
| 28 | parsedURL, err := url.Parse(ts.URL) |
| 29 | assert.NoError(t, err) |
| 30 | |
| 31 | token, err := ExchangeCodeForToken(ts.Client(), parsedURL.Host, "some-client-id", "some-client-secret", "some-code", "http://localhost:8484") |
| 32 | |
| 33 | assert.NoError(t, err) |
| 34 | assert.Equal(t, "access-token-here", token.AccessToken) |
| 35 | assert.Equal(t, "id-token-here", token.IDToken) |
| 36 | assert.Equal(t, "token-type-here", token.TokenType) |
| 37 | assert.Equal(t, int64(1000), token.ExpiresIn) |
| 38 | }) |
| 39 | |
| 40 | testCases := []struct { |
| 41 | name string |
| 42 | expect string |
| 43 | httpStatus int |
| 44 | response string |
| 45 | }{ |
| 46 | { |
| 47 | name: "Bad status code", |
| 48 | expect: "unable to exchange code for token: 404 Not Found", |
| 49 | httpStatus: http.StatusNotFound, |
| 50 | }, |
| 51 | { |
| 52 | name: "Malformed JSON", |
| 53 | expect: "cannot decode response: unexpected EOF", |
| 54 | httpStatus: http.StatusOK, |
| 55 | response: `{ "foo": "bar" `, |
| 56 | }, |
| 57 | } |
| 58 | |
| 59 | for _, testCase := range testCases { |
| 60 | t.Run(testCase.name, func(t *testing.T) { |
| 61 | ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 62 | w.WriteHeader(testCase.httpStatus) |
| 63 | if testCase.response != "" { |
| 64 | _, err := io.WriteString(w, testCase.response) |
| 65 | require.NoError(t, err) |
| 66 | } |
| 67 | })) |
| 68 | |
| 69 | defer ts.Close() |
| 70 | parsedURL, err := url.Parse(ts.URL) |
| 71 | assert.NoError(t, err) |
nothing calls this directly
no test coverage detected