Skip to content

Commit

Permalink
Remove WithHTTPClient method introduced in refactor
Browse files Browse the repository at this point in the history
* Directly use the HTTPClient context keys of oauth1 and
oauth2 instead of exposing an unneeded middleware
  • Loading branch information
dghubble committed Jan 8, 2017
1 parent 51af946 commit 4206d1a
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 51 deletions.
59 changes: 44 additions & 15 deletions digits/token_test.go
@@ -1,11 +1,15 @@
package digits

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/dghubble/gologin"
oauth1Login "github.com/dghubble/gologin/oauth1"
"github.com/dghubble/gologin/testutils"
"github.com/dghubble/oauth1"
Expand All @@ -30,6 +34,9 @@ func TestTokenHandler(t *testing.T) {
proxyClient, _, server := newDigitsTestServer(testAccountJSON)
defer server.Close()

// oauth1 Client will use the proxy client's base Transport
ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient)

config := &oauth1.Config{}
success := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
Expand All @@ -43,29 +50,51 @@ func TestTokenHandler(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, testDigitsToken, accessToken)
assert.Equal(t, testDigitsSecret, accessSecret)
fmt.Fprintf(w, "success handler called")
}
handler := TokenHandler(config, http.HandlerFunc(success), testutils.AssertFailureNotCalled(t))
// oauth1 Client will use the proxy client's base Transport
ts := httptest.NewServer(oauth1Login.WithHTTPClient(proxyClient, handler))
// POST Digits access token to server under test
resp, err := http.PostForm(ts.URL, url.Values{accessTokenField: {testDigitsToken}, accessTokenSecretField: {testDigitsSecret}})
assert.Nil(t, err)
if assert.NotNil(t, resp) {
assert.Equal(t, resp.StatusCode, http.StatusOK)
}

// TokenHandler assert that:
// - access token/secret are read from POST
// - digits account is obtained from the Digits accounts endpoint
// - success handler is called
// - digits account is added to the success handler ctx
tokenHandler := TokenHandler(config, http.HandlerFunc(success), testutils.AssertFailureNotCalled(t))
w := httptest.NewRecorder()
form := url.Values{accessTokenField: {testDigitsToken}, accessTokenSecretField: {testDigitsSecret}}
req, _ := http.NewRequest("POST", "/", strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
tokenHandler.ServeHTTP(w, req.WithContext(ctx))
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "success handler called", w.Body.String())
}

func TestTokenHandler_ErrorVerifyingToken(t *testing.T) {
proxyClient, server := testutils.NewErrorServer("Digits Account Endpoint Down", http.StatusInternalServerError)
defer server.Close()

config := &oauth1.Config{}
handler := TokenHandler(config, testutils.AssertSuccessNotCalled(t), nil)
// oauth1 Client will use the proxy client's base Transport
ts := httptest.NewServer(oauth1Login.WithHTTPClient(proxyClient, handler))
// assert that error occurs indicating the Digits Account could not be confirmed
resp, _ := http.PostForm(ts.URL, url.Values{accessTokenField: {testDigitsToken}, accessTokenSecretField: {testDigitsSecret}})
testutils.AssertBodyString(t, resp.Body, ErrUnableToGetDigitsAccount.Error()+"\n")
ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient)

config := &oauth1.Config{}
failure := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
err := gologin.ErrorFromContext(ctx)
if assert.Error(t, err) {
assert.Equal(t, err, ErrUnableToGetDigitsAccount)
}
fmt.Fprintf(w, "failure handler called")
}

// TokenHandler cannot verify Digits account, assert that:
// - failure handler is called
// - error is added to the failure handler context
tokenHandler := TokenHandler(config, testutils.AssertSuccessNotCalled(t), http.HandlerFunc(failure))
w := httptest.NewRecorder()
form := url.Values{accessTokenField: {testDigitsToken}, accessTokenSecretField: {testDigitsSecret}}
req, _ := http.NewRequest("POST", "/", strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
tokenHandler.ServeHTTP(w, req.WithContext(ctx))
assert.Equal(t, "failure handler called", w.Body.String())
}

func TestTokenHandler_NonPost(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions github/login_test.go
Expand Up @@ -20,6 +20,7 @@ func TestGithubHandler(t *testing.T) {
expectedUser := &github.User{ID: github.Int(917408), Name: github.String("Alyssa Hacker")}
proxyClient, server := newGithubTestServer(jsonData)
defer server.Close()

// oauth2 Client will use the proxy client's base Transport
ctx := context.WithValue(context.Background(), oauth2.HTTPClient, proxyClient)
anyToken := &oauth2.Token{AccessToken: "any-token"}
Expand Down
11 changes: 0 additions & 11 deletions oauth1/context.go
Expand Up @@ -3,9 +3,6 @@ package oauth1
import (
"context"
"fmt"
"net/http"

olib "github.com/dghubble/oauth1"
)

// unexported key type prevents collisions
Expand Down Expand Up @@ -53,11 +50,3 @@ func AccessTokenFromContext(ctx context.Context) (string, string, error) {
}
return accessToken, accessSecret, nil
}

// WithHTTPClient returns a handler that sets the provided HttpClient in the request context.
func WithHTTPClient(client *http.Client, next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, req *http.Request) {
next.ServeHTTP(w, req.WithContext(context.WithValue(req.Context(), olib.HTTPClient, client)))
}
return http.HandlerFunc(fn)
}
59 changes: 34 additions & 25 deletions twitter/token_test.go
@@ -1,10 +1,12 @@
package twitter

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"

"github.com/dghubble/gologin"
Expand All @@ -25,6 +27,9 @@ func TestTokenHandler(t *testing.T) {
proxyClient, _, server := newTwitterVerifyServer(testTwitterUserJSON)
defer server.Close()

// oauth1 Client will use the proxy client's base Transport
ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient)

config := &oauth1.Config{}
success := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
Expand All @@ -37,48 +42,52 @@ func TestTokenHandler(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, expectedUserID, user.ID)
assert.Equal(t, "1234", user.IDStr)
fmt.Fprintf(w, "success handler called")
}
handler := TokenHandler(config, http.HandlerFunc(success), testutils.AssertFailureNotCalled(t))
// oauth1 Client will use the proxy client's base Transport
ts := httptest.NewServer(oauth1Login.WithHTTPClient(proxyClient, handler))
// POST token to server under test
resp, err := http.PostForm(ts.URL, url.Values{accessTokenField: {testTwitterToken}, accessTokenSecretField: {testTwitterTokenSecret}})
assert.Nil(t, err)
if assert.NotNil(t, resp) {
assert.Equal(t, resp.StatusCode, http.StatusOK)
}
failure := testutils.AssertFailureNotCalled(t)

// TokenHandler assert that:
// - access token/secret are read from POST
// - twitter User is obtained from Twitter API
// - success handler is called
// - twitter User is added to success handler ctx
tokenHandler := TokenHandler(config, http.HandlerFunc(success), failure)
w := httptest.NewRecorder()
form := url.Values{accessTokenField: {testTwitterToken}, accessTokenSecretField: {testTwitterTokenSecret}}
req, _ := http.NewRequest("POST", "/", strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
tokenHandler.ServeHTTP(w, req.WithContext(ctx))
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "success handler called", w.Body.String())
}

func TestTokenHandler_ErrorVerifyingToken(t *testing.T) {
proxyClient, server := testutils.NewErrorServer("Twitter Verify Credentials Down", http.StatusInternalServerError)
defer server.Close()

config := &oauth1.Config{}
handler := TokenHandler(config, testutils.AssertSuccessNotCalled(t), nil)
// oauth1 Client will use the proxy client's base Transport
ts := httptest.NewServer(oauth1Login.WithHTTPClient(proxyClient, handler))
// assert that error occurs indicating the Twitter User could not be confirmed
resp, _ := http.PostForm(ts.URL, url.Values{accessTokenField: {testTwitterToken}, accessTokenSecretField: {testTwitterTokenSecret}})
testutils.AssertBodyString(t, resp.Body, ErrUnableToGetTwitterUser.Error()+"\n")
}

func TestTokenHandler_ErrorVerifyingTokenPassesError(t *testing.T) {
proxyClient, server := testutils.NewErrorServer("Twitter Verify Credentials Down", http.StatusInternalServerError)
defer server.Close()
ctx := context.WithValue(context.Background(), oauth1.HTTPClient, proxyClient)

config := &oauth1.Config{}
failure := func(w http.ResponseWriter, req *http.Request) {
ctx := req.Context()
// assert that error passed through ctx
err := gologin.ErrorFromContext(ctx)
if assert.Error(t, err) {
assert.Equal(t, err, ErrUnableToGetTwitterUser)
}
fmt.Fprintf(w, "failure handler called")
}
handler := TokenHandler(config, testutils.AssertSuccessNotCalled(t), http.HandlerFunc(failure))
// oauth1 Client will use the proxy client's base Transport
ts := httptest.NewServer(oauth1Login.WithHTTPClient(proxyClient, handler))
http.PostForm(ts.URL, url.Values{accessTokenField: {testTwitterToken}, accessTokenSecretField: {testTwitterTokenSecret}})

// TokenHandler cannot verify Twitter credentials and get User, assert that:
// - failure handler is called
// - error is added to the failure handler context
tokenHandler := TokenHandler(config, testutils.AssertSuccessNotCalled(t), http.HandlerFunc(failure))
w := httptest.NewRecorder()
form := url.Values{accessTokenField: {testTwitterToken}, accessTokenSecretField: {testTwitterTokenSecret}}
req, _ := http.NewRequest("POST", "/", strings.NewReader(form.Encode()))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
tokenHandler.ServeHTTP(w, req.WithContext(ctx))
assert.Equal(t, "failure handler called", w.Body.String())
}

func TestTokenHandler_NonPost(t *testing.T) {
Expand Down

0 comments on commit 4206d1a

Please sign in to comment.