Simplify oauth server testing
This commit is contained in:
parent
5a9c6adedf
commit
ffa5afbf22
@ -121,19 +121,19 @@ func TestServerAuthCallback(t *testing.T) {
|
|||||||
assert := assert.New(t)
|
assert := assert.New(t)
|
||||||
config = newDefaultConfig()
|
config = newDefaultConfig()
|
||||||
|
|
||||||
// Setup token server
|
// Setup OAuth server
|
||||||
tokenServerHandler := &TokenServerHandler{}
|
server, serverURL := NewOAuthServer(t)
|
||||||
tokenServer := httptest.NewServer(tokenServerHandler)
|
defer server.Close()
|
||||||
defer tokenServer.Close()
|
config.Providers.Google.TokenURL = &url.URL{
|
||||||
tokenUrl, _ := url.Parse(tokenServer.URL)
|
Scheme: serverURL.Scheme,
|
||||||
config.Providers.Google.TokenURL = tokenUrl
|
Host: serverURL.Host,
|
||||||
|
Path: "/token",
|
||||||
// Setup user server
|
}
|
||||||
userServerHandler := &UserServerHandler{}
|
config.Providers.Google.UserURL = &url.URL{
|
||||||
userServer := httptest.NewServer(userServerHandler)
|
Scheme: serverURL.Scheme,
|
||||||
defer userServer.Close()
|
Host: serverURL.Host,
|
||||||
userUrl, _ := url.Parse(userServer.URL)
|
Path: "/userinfo",
|
||||||
config.Providers.Google.UserURL = userUrl
|
}
|
||||||
|
|
||||||
// Should pass auth response request to callback
|
// Should pass auth response request to callback
|
||||||
req := newDefaultHttpRequest("/_oauth")
|
req := newDefaultHttpRequest("/_oauth")
|
||||||
@ -342,21 +342,30 @@ func TestServerRouteQuery(t *testing.T) {
|
|||||||
* Utilities
|
* Utilities
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type TokenServerHandler struct{}
|
type OAuthServer struct {
|
||||||
|
t *testing.T
|
||||||
func (t *TokenServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Fprint(w, `{"access_token":"123456789"}`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserServerHandler struct{}
|
func (s *OAuthServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.URL.Path == "/token" {
|
||||||
func (t *UserServerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
fmt.Fprintf(w, `{"access_token":"123456789"}`)
|
||||||
|
} else if r.URL.Path == "/userinfo" {
|
||||||
fmt.Fprint(w, `{
|
fmt.Fprint(w, `{
|
||||||
"id":"1",
|
"id":"1",
|
||||||
"email":"example@example.com",
|
"email":"example@example.com",
|
||||||
"verified_email":true,
|
"verified_email":true,
|
||||||
"hd":"example.com"
|
"hd":"example.com"
|
||||||
}`)
|
}`)
|
||||||
|
} else {
|
||||||
|
s.t.Fatal("Unrecognised request: ", r.Method, r.URL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewOAuthServer(t *testing.T) (*httptest.Server, *url.URL) {
|
||||||
|
handler := &OAuthServer{}
|
||||||
|
server := httptest.NewServer(handler)
|
||||||
|
serverURL, _ := url.Parse(server.URL)
|
||||||
|
return server, serverURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func doHttpRequest(r *http.Request, c *http.Cookie) (*http.Response, string) {
|
func doHttpRequest(r *http.Request, c *http.Cookie) (*http.Response, string) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user