Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Commit

Permalink
Make proper PassAccessTokenTest methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Bland committed Apr 7, 2015
1 parent 9887ac3 commit 3595fdb
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions oauthproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,12 @@ func NewPassAccessTokenTest(opts PassAccessTokenTestOptions) *PassAccessTokenTes
return t
}

func Close(t *PassAccessTokenTest) {
func (t *PassAccessTokenTest) Close() {
t.provider_server.Close()
}

func getCallbackEndpoint(pac_test *PassAccessTokenTest) (http_code int, cookie string) {
func (pac_test *PassAccessTokenTest) getCallbackEndpoint() (http_code int,
cookie string) {
rw := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/oauth2/callback?code=callback_code",
strings.NewReader(""))
Expand All @@ -167,8 +168,8 @@ func getCallbackEndpoint(pac_test *PassAccessTokenTest) (http_code int, cookie s
return rw.Code, rw.HeaderMap["Set-Cookie"][0]
}

func getRootEndpoint(pac_test *PassAccessTokenTest, cookie string) (http_code int,
access_token string) {
func (pac_test *PassAccessTokenTest) getRootEndpoint(
cookie string) (http_code int, access_token string) {
cookie_key := pac_test.proxy.CookieKey
var value string
key_prefix := cookie_key + "="
Expand Down Expand Up @@ -206,17 +207,17 @@ func TestForwardAccessTokenUpstream(t *testing.T) {
pac_test := NewPassAccessTokenTest(PassAccessTokenTestOptions{
PassAccessToken: true,
})
defer Close(pac_test)
defer pac_test.Close()

// A successful validation will redirect and set the auth cookie.
code, cookie := getCallbackEndpoint(pac_test)
code, cookie := pac_test.getCallbackEndpoint()
assert.Equal(t, 302, code)
assert.NotEqual(t, nil, cookie)

// Now we make a regular request; the access_token from the cookie is
// forwarded as the "X-Forwarded-Access-Token" header. The token is
// read by the test provider server and written in the response body.
code, payload := getRootEndpoint(pac_test, cookie)
code, payload := pac_test.getRootEndpoint(cookie)
assert.Equal(t, 200, code)
assert.Equal(t, "my_auth_token", payload)
}
Expand All @@ -225,16 +226,16 @@ func TestDoNotForwardAccessTokenUpstream(t *testing.T) {
pac_test := NewPassAccessTokenTest(PassAccessTokenTestOptions{
PassAccessToken: false,
})
defer Close(pac_test)
defer pac_test.Close()

// A successful validation will redirect and set the auth cookie.
code, cookie := getCallbackEndpoint(pac_test)
code, cookie := pac_test.getCallbackEndpoint()
assert.Equal(t, 302, code)
assert.NotEqual(t, nil, cookie)

// Now we make a regular request, but the access token header should
// not be present.
code, payload := getRootEndpoint(pac_test, cookie)
code, payload := pac_test.getRootEndpoint(cookie)
assert.Equal(t, 200, code)
assert.Equal(t, "No access token found.", payload)
}
Expand Down

0 comments on commit 3595fdb

Please sign in to comment.