Skip to content

Commit 3f5303a

Browse files
authored
Refactor e2e_test (#234)
* Refactor e2e_test * Fix
1 parent f38e97b commit 3f5303a

File tree

6 files changed

+165
-150
lines changed

6 files changed

+165
-150
lines changed

e2e_test/authserver/handler.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,23 @@ type TokenRequest struct {
2929

3030
// Handler handles HTTP requests.
3131
type Handler struct {
32-
T *testing.T
32+
TestingT *testing.T
3333

3434
// This should return a URL with query parameters of authorization response.
3535
// See https://tools.ietf.org/html/rfc6749#section-4.1.2
36-
NewAuthorizationResponse func(r AuthorizationRequest) string
36+
NewAuthorizationResponse func(req AuthorizationRequest) string
3737

3838
// This should return a JSON body of access token response or error response.
3939
// See https://tools.ietf.org/html/rfc6749#section-5.1
4040
// and https://tools.ietf.org/html/rfc6749#section-5.2
41-
NewTokenResponse func(r TokenRequest) (int, string)
41+
NewTokenResponse func(req TokenRequest) (int, string)
4242
}
4343

4444
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
45-
h.T.Logf("authServer: %s %s", r.Method, r.RequestURI)
45+
h.TestingT.Logf("authServer: %s %s", r.Method, r.RequestURI)
4646
if err := h.serveHTTP(w, r); err != nil {
47-
h.T.Errorf("Handler error: %s", err)
48-
http.Error(w, err.Error(), 500)
47+
h.TestingT.Errorf("Handler error: %s", err)
48+
http.Error(w, err.Error(), http.StatusInternalServerError)
4949
}
5050
}
5151

@@ -63,13 +63,13 @@ func (h *Handler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
6363
if redirectURI == "" {
6464
return errors.New("redirect_uri is missing")
6565
}
66-
to := h.NewAuthorizationResponse(AuthorizationRequest{
66+
authorizationResponseURL := h.NewAuthorizationResponse(AuthorizationRequest{
6767
Scope: scope,
6868
State: state,
6969
RedirectURI: redirectURI,
7070
Raw: q,
7171
})
72-
http.Redirect(w, r, to, 302)
72+
http.Redirect(w, r, authorizationResponseURL, http.StatusFound)
7373

7474
case r.Method == "POST" && r.URL.Path == "/token":
7575
if err := r.ParseForm(); err != nil {
@@ -82,13 +82,13 @@ func (h *Handler) serveHTTP(w http.ResponseWriter, r *http.Request) error {
8282
if redirectURI == "" {
8383
return errors.New("redirect_uri is missing")
8484
}
85-
status, b := h.NewTokenResponse(TokenRequest{
85+
status, body := h.NewTokenResponse(TokenRequest{
8686
Code: code,
8787
Raw: r.Form,
8888
})
8989
w.Header().Add("Content-Type", "application/json")
9090
w.WriteHeader(status)
91-
if _, err := w.Write([]byte(b)); err != nil {
91+
if _, err := w.Write([]byte(body)); err != nil {
9292
return fmt.Errorf("error while writing response body: %w", err)
9393
}
9494

e2e_test/context_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@ import (
1616
func TestContextCancelOnWaitingForBrowser(t *testing.T) {
1717
ctx, cancel := context.WithTimeout(context.TODO(), 100*time.Millisecond)
1818
defer cancel()
19-
s := httptest.NewServer(&authserver.Handler{
20-
T: t,
21-
NewAuthorizationResponse: func(r authserver.AuthorizationRequest) string {
22-
return fmt.Sprintf("%s?error=server_error", r.RedirectURI)
19+
testServer := httptest.NewServer(&authserver.Handler{
20+
TestingT: t,
21+
NewAuthorizationResponse: func(req authserver.AuthorizationRequest) string {
22+
return fmt.Sprintf("%s?error=server_error", req.RedirectURI)
2323
},
24-
NewTokenResponse: func(r authserver.TokenRequest) (int, string) {
24+
NewTokenResponse: func(req authserver.TokenRequest) (int, string) {
2525
return 500, "should not reach here"
2626
},
2727
})
28-
defer s.Close()
28+
defer testServer.Close()
2929
cfg := oauth2cli.Config{
3030
OAuth2Config: oauth2.Config{
3131
ClientID: "YOUR_CLIENT_ID",
3232
ClientSecret: "YOUR_CLIENT_SECRET",
3333
Scopes: []string{"email", "profile"},
3434
Endpoint: oauth2.Endpoint{
35-
AuthURL: s.URL + "/auth",
36-
TokenURL: s.URL + "/token",
35+
AuthURL: testServer.URL + "/auth",
36+
TokenURL: testServer.URL + "/token",
3737
},
3838
},
3939
Logf: t.Logf,
@@ -53,24 +53,24 @@ func TestContextCancelOnLocalServerReadyChan(t *testing.T) {
5353
defer cancel()
5454
openBrowserCh := make(chan string)
5555
defer close(openBrowserCh)
56-
s := httptest.NewServer(&authserver.Handler{
57-
T: t,
58-
NewAuthorizationResponse: func(r authserver.AuthorizationRequest) string {
59-
return fmt.Sprintf("%s?error=server_error", r.RedirectURI)
56+
testServer := httptest.NewServer(&authserver.Handler{
57+
TestingT: t,
58+
NewAuthorizationResponse: func(req authserver.AuthorizationRequest) string {
59+
return fmt.Sprintf("%s?error=server_error", req.RedirectURI)
6060
},
61-
NewTokenResponse: func(r authserver.TokenRequest) (int, string) {
61+
NewTokenResponse: func(req authserver.TokenRequest) (int, string) {
6262
return 500, "should not reach here"
6363
},
6464
})
65-
defer s.Close()
65+
defer testServer.Close()
6666
cfg := oauth2cli.Config{
6767
OAuth2Config: oauth2.Config{
6868
ClientID: "YOUR_CLIENT_ID",
6969
ClientSecret: "YOUR_CLIENT_SECRET",
7070
Scopes: []string{"email", "profile"},
7171
Endpoint: oauth2.Endpoint{
72-
AuthURL: s.URL + "/auth",
73-
TokenURL: s.URL + "/token",
72+
AuthURL: testServer.URL + "/auth",
73+
TokenURL: testServer.URL + "/token",
7474
},
7575
},
7676
LocalServerReadyChan: openBrowserCh,

e2e_test/e2e_test.go

Lines changed: 76 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"net/http/httptest"
8-
"strings"
8+
"net/url"
99
"sync"
1010
"testing"
1111
"time"
@@ -29,37 +29,35 @@ func TestHappyPath(t *testing.T) {
2929
defer wg.Done()
3030
defer close(openBrowserCh)
3131
// Start a local server and get a token.
32-
s := httptest.NewServer(&authserver.Handler{
33-
T: t,
34-
NewAuthorizationResponse: func(r authserver.AuthorizationRequest) string {
35-
if w := "email profile"; r.Scope != w {
36-
t.Errorf("scope wants %s but %s", w, r.Scope)
37-
return fmt.Sprintf("%s?error=invalid_scope", r.RedirectURI)
32+
testServer := httptest.NewServer(&authserver.Handler{
33+
TestingT: t,
34+
NewAuthorizationResponse: func(req authserver.AuthorizationRequest) string {
35+
if want := "email profile"; req.Scope != want {
36+
t.Errorf("scope wants %s but %s", want, req.Scope)
37+
return fmt.Sprintf("%s?error=invalid_scope", req.RedirectURI)
3838
}
39-
redirectURIPrefix := "http://localhost:"
40-
if !strings.HasPrefix(r.RedirectURI, redirectURIPrefix) {
41-
t.Errorf("redirect_uri wants prefix %s but was %s", redirectURIPrefix, r.RedirectURI)
42-
return fmt.Sprintf("%s?error=invalid_redirect_uri", r.RedirectURI)
39+
if !assertRedirectURI(t, req.RedirectURI, "http", "localhost") {
40+
return fmt.Sprintf("%s?error=invalid_redirect_uri", req.RedirectURI)
4341
}
44-
return fmt.Sprintf("%s?state=%s&code=%s", r.RedirectURI, r.State, "AUTH_CODE")
42+
return fmt.Sprintf("%s?state=%s&code=%s", req.RedirectURI, req.State, "AUTH_CODE")
4543
},
46-
NewTokenResponse: func(r authserver.TokenRequest) (int, string) {
47-
if w := "AUTH_CODE"; r.Code != w {
48-
t.Errorf("code wants %s but %s", w, r.Code)
44+
NewTokenResponse: func(req authserver.TokenRequest) (int, string) {
45+
if want := "AUTH_CODE"; req.Code != want {
46+
t.Errorf("code wants %s but %s", want, req.Code)
4947
return 400, invalidGrantResponse
5048
}
5149
return 200, validTokenResponse
5250
},
5351
})
54-
defer s.Close()
52+
defer testServer.Close()
5553
cfg := oauth2cli.Config{
5654
OAuth2Config: oauth2.Config{
5755
ClientID: "YOUR_CLIENT_ID",
5856
ClientSecret: "YOUR_CLIENT_SECRET",
5957
Scopes: []string{"email", "profile"},
6058
Endpoint: oauth2.Endpoint{
61-
AuthURL: s.URL + "/auth",
62-
TokenURL: s.URL + "/token",
59+
AuthURL: testServer.URL + "/auth",
60+
TokenURL: testServer.URL + "/token",
6361
},
6462
},
6563
LocalServerReadyChan: openBrowserCh,
@@ -71,11 +69,11 @@ func TestHappyPath(t *testing.T) {
7169
t.Errorf("could not get a token: %s", err)
7270
return
7371
}
74-
if "ACCESS_TOKEN" != token.AccessToken {
72+
if token.AccessToken != "ACCESS_TOKEN" {
7573
t.Errorf("AccessToken wants %s but %s", "ACCESS_TOKEN", token.AccessToken)
7674
}
77-
if "REFRESH_TOKEN" != token.RefreshToken {
78-
t.Errorf("RefreshToken wants %s but %s", "REFRESH_TOKEN", token.AccessToken)
75+
if token.RefreshToken != "REFRESH_TOKEN" {
76+
t.Errorf("RefreshToken wants %s but %s", "REFRESH_TOKEN", token.RefreshToken)
7977
}
8078
}()
8179
wg.Add(1)
@@ -101,37 +99,35 @@ func TestRedirectURLHostname(t *testing.T) {
10199
defer wg.Done()
102100
defer close(openBrowserCh)
103101
// Start a local server and get a token.
104-
s := httptest.NewServer(&authserver.Handler{
105-
T: t,
106-
NewAuthorizationResponse: func(r authserver.AuthorizationRequest) string {
107-
if w := "email profile"; r.Scope != w {
108-
t.Errorf("scope wants %s but %s", w, r.Scope)
109-
return fmt.Sprintf("%s?error=invalid_scope", r.RedirectURI)
102+
testServer := httptest.NewServer(&authserver.Handler{
103+
TestingT: t,
104+
NewAuthorizationResponse: func(req authserver.AuthorizationRequest) string {
105+
if want := "email profile"; req.Scope != want {
106+
t.Errorf("scope wants %s but %s", want, req.Scope)
107+
return fmt.Sprintf("%s?error=invalid_scope", req.RedirectURI)
110108
}
111-
redirectURIPrefix := "http://127.0.0.1:"
112-
if !strings.HasPrefix(r.RedirectURI, redirectURIPrefix) {
113-
t.Errorf("redirect_uri wants prefix %s but was %s", redirectURIPrefix, r.RedirectURI)
114-
return fmt.Sprintf("%s?error=invalid_redirect_uri", r.RedirectURI)
109+
if !assertRedirectURI(t, req.RedirectURI, "http", "127.0.0.1") {
110+
return fmt.Sprintf("%s?error=invalid_redirect_uri", req.RedirectURI)
115111
}
116-
return fmt.Sprintf("%s?state=%s&code=%s", r.RedirectURI, r.State, "AUTH_CODE")
112+
return fmt.Sprintf("%s?state=%s&code=%s", req.RedirectURI, req.State, "AUTH_CODE")
117113
},
118-
NewTokenResponse: func(r authserver.TokenRequest) (int, string) {
119-
if w := "AUTH_CODE"; r.Code != w {
120-
t.Errorf("code wants %s but %s", w, r.Code)
114+
NewTokenResponse: func(req authserver.TokenRequest) (int, string) {
115+
if want := "AUTH_CODE"; req.Code != want {
116+
t.Errorf("code wants %s but %s", want, req.Code)
121117
return 400, invalidGrantResponse
122118
}
123119
return 200, validTokenResponse
124120
},
125121
})
126-
defer s.Close()
122+
defer testServer.Close()
127123
cfg := oauth2cli.Config{
128124
OAuth2Config: oauth2.Config{
129125
ClientID: "YOUR_CLIENT_ID",
130126
ClientSecret: "YOUR_CLIENT_SECRET",
131127
Scopes: []string{"email", "profile"},
132128
Endpoint: oauth2.Endpoint{
133-
AuthURL: s.URL + "/auth",
134-
TokenURL: s.URL + "/token",
129+
AuthURL: testServer.URL + "/auth",
130+
TokenURL: testServer.URL + "/token",
135131
},
136132
},
137133
RedirectURLHostname: "127.0.0.1",
@@ -144,11 +140,11 @@ func TestRedirectURLHostname(t *testing.T) {
144140
t.Errorf("could not get a token: %s", err)
145141
return
146142
}
147-
if "ACCESS_TOKEN" != token.AccessToken {
143+
if token.AccessToken != "ACCESS_TOKEN" {
148144
t.Errorf("AccessToken wants %s but %s", "ACCESS_TOKEN", token.AccessToken)
149145
}
150-
if "REFRESH_TOKEN" != token.RefreshToken {
151-
t.Errorf("RefreshToken wants %s but %s", "REFRESH_TOKEN", token.AccessToken)
146+
if token.RefreshToken != "REFRESH_TOKEN" {
147+
t.Errorf("RefreshToken wants %s but %s", "REFRESH_TOKEN", token.RefreshToken)
152148
}
153149
}()
154150
wg.Add(1)
@@ -174,29 +170,27 @@ func TestSuccessRedirect(t *testing.T) {
174170
defer wg.Done()
175171
defer close(openBrowserCh)
176172
// start a local server of oauth2 endpoint
177-
s := httptest.NewServer(&authserver.Handler{
178-
T: t,
179-
NewAuthorizationResponse: func(r authserver.AuthorizationRequest) string {
180-
if w := "email profile"; r.Scope != w {
181-
t.Errorf("scope wants %s but %s", w, r.Scope)
182-
return fmt.Sprintf("%s?error=invalid_scope", r.RedirectURI)
173+
testServer := httptest.NewServer(&authserver.Handler{
174+
TestingT: t,
175+
NewAuthorizationResponse: func(req authserver.AuthorizationRequest) string {
176+
if want := "email profile"; req.Scope != want {
177+
t.Errorf("scope wants %s but %s", want, req.Scope)
178+
return fmt.Sprintf("%s?error=invalid_scope", req.RedirectURI)
183179
}
184-
redirectURIPrefix := "http://localhost:"
185-
if !strings.HasPrefix(r.RedirectURI, redirectURIPrefix) {
186-
t.Errorf("redirect_uri wants prefix %s but was %s", redirectURIPrefix, r.RedirectURI)
187-
return fmt.Sprintf("%s?error=invalid_redirect_uri", r.RedirectURI)
180+
if !assertRedirectURI(t, req.RedirectURI, "http", "localhost") {
181+
return fmt.Sprintf("%s?error=invalid_redirect_uri", req.RedirectURI)
188182
}
189-
return fmt.Sprintf("%s?state=%s&code=%s", r.RedirectURI, r.State, "AUTH_CODE")
183+
return fmt.Sprintf("%s?state=%s&code=%s", req.RedirectURI, req.State, "AUTH_CODE")
190184
},
191-
NewTokenResponse: func(r authserver.TokenRequest) (int, string) {
192-
if w := "AUTH_CODE"; r.Code != w {
193-
t.Errorf("code wants %s but %s", w, r.Code)
185+
NewTokenResponse: func(req authserver.TokenRequest) (int, string) {
186+
if want := "AUTH_CODE"; req.Code != want {
187+
t.Errorf("code wants %s but %s", want, req.Code)
194188
return 400, invalidGrantResponse
195189
}
196190
return 200, validTokenResponse
197191
},
198192
})
199-
defer s.Close()
193+
defer testServer.Close()
200194
// start a local server to be redirected
201195
sr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
202196
if r.URL.Path == "/success" && r.Method == "GET" {
@@ -213,8 +207,8 @@ func TestSuccessRedirect(t *testing.T) {
213207
ClientSecret: "YOUR_CLIENT_SECRET",
214208
Scopes: []string{"email", "profile"},
215209
Endpoint: oauth2.Endpoint{
216-
AuthURL: s.URL + "/auth",
217-
TokenURL: s.URL + "/token",
210+
AuthURL: testServer.URL + "/auth",
211+
TokenURL: testServer.URL + "/token",
218212
},
219213
},
220214
LocalServerReadyChan: openBrowserCh,
@@ -228,11 +222,11 @@ func TestSuccessRedirect(t *testing.T) {
228222
t.Errorf("could not get a token: %s", err)
229223
return
230224
}
231-
if "ACCESS_TOKEN" != token.AccessToken {
225+
if token.AccessToken != "ACCESS_TOKEN" {
232226
t.Errorf("AccessToken wants %s but %s", "ACCESS_TOKEN", token.AccessToken)
233227
}
234-
if "REFRESH_TOKEN" != token.RefreshToken {
235-
t.Errorf("RefreshToken wants %s but %s", "REFRESH_TOKEN", token.AccessToken)
228+
if token.RefreshToken != "REFRESH_TOKEN" {
229+
t.Errorf("RefreshToken wants %s but %s", "REFRESH_TOKEN", token.RefreshToken)
236230
}
237231
}()
238232
wg.Add(1)
@@ -248,6 +242,27 @@ func TestSuccessRedirect(t *testing.T) {
248242
wg.Wait()
249243
}
250244

245+
func assertRedirectURI(t *testing.T, actualURI, scheme, hostname string) bool {
246+
redirect, err := url.Parse(actualURI)
247+
if err != nil {
248+
t.Errorf("could not parse redirect_uri: %s", err)
249+
return false
250+
}
251+
if redirect.Scheme != scheme {
252+
t.Errorf("redirect_uri wants scheme %s but was %s", scheme, redirect.Scheme)
253+
return false
254+
}
255+
if actualHostname := redirect.Hostname(); actualHostname != hostname {
256+
t.Errorf("redirect_uri wants hostname %s but was %s", hostname, actualHostname)
257+
return false
258+
}
259+
if redirect.Path != "" {
260+
t.Errorf("redirect_uri wants path `` but was %s", redirect.Path)
261+
return false
262+
}
263+
return true
264+
}
265+
251266
func loggingMiddleware(t *testing.T) func(h http.Handler) http.Handler {
252267
return func(h http.Handler) http.Handler {
253268
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)