I want to test my oauth2 client and use a mock for TokenURL endpoint. The mock responds with access_token but there is always an error with missing access_token.
I am using golang.org/x/oauth2 and default go testing package.
My mock server:
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/token" {
// Mock the token endpoint
json.NewEncoder(w).Encode(struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
Expiry time.Time `json:"expiry"`
}{
AccessToken: "access-token",
TokenType: "Bearer",
RefreshToken: "refresh-token",
Expiry: time.Now().Add(2 * time.Hour),
})
}
}))
defer server.Close()
mockConfig.Endpoint.TokenURL = server.URL + "/token"
example: I want to refresh access token
tokenSource := a.config.TokenSource(ctx, &oauth2.Token{
RefreshToken: session.RefreshToken,
})
error while refreshing token:
oauth2: server response missing access_token
I have already checked/tried
- the mock response, if access_token is given
- the code of oauth2 package to see when this error occurs, but have not found any further information
- verified the right config parameter (oauth2.Config.Endpoint.TokenURL)
The code is running with my oauth2 provider, so it is just a testing issue.
Thanks!