@@ -89,77 +89,6 @@ func TestHappyPath(t *testing.T) {
8989 wg .Wait ()
9090}
9191
92- func TestRedirectURLHostname (t * testing.T ) {
93- ctx , cancel := context .WithTimeout (context .TODO (), 1 * time .Second )
94- defer cancel ()
95- openBrowserCh := make (chan string )
96- var wg sync.WaitGroup
97- wg .Add (1 )
98- go func () {
99- defer wg .Done ()
100- defer close (openBrowserCh )
101- // Start a local server and get a token.
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 )
108- }
109- if ! assertRedirectURI (t , req .RedirectURI , "http" , "127.0.0.1" , "" ) {
110- return fmt .Sprintf ("%s?error=invalid_redirect_uri" , req .RedirectURI )
111- }
112- return fmt .Sprintf ("%s?state=%s&code=%s" , req .RedirectURI , req .State , "AUTH_CODE" )
113- },
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 )
117- return 400 , invalidGrantResponse
118- }
119- return 200 , validTokenResponse
120- },
121- })
122- defer testServer .Close ()
123- cfg := oauth2cli.Config {
124- OAuth2Config : oauth2.Config {
125- ClientID : "YOUR_CLIENT_ID" ,
126- ClientSecret : "YOUR_CLIENT_SECRET" ,
127- Scopes : []string {"email" , "profile" },
128- Endpoint : oauth2.Endpoint {
129- AuthURL : testServer .URL + "/auth" ,
130- TokenURL : testServer .URL + "/token" ,
131- },
132- },
133- RedirectURLHostname : "127.0.0.1" ,
134- LocalServerReadyChan : openBrowserCh ,
135- LocalServerMiddleware : loggingMiddleware (t ),
136- Logf : t .Logf ,
137- }
138- token , err := oauth2cli .GetToken (ctx , cfg )
139- if err != nil {
140- t .Errorf ("could not get a token: %s" , err )
141- return
142- }
143- if token .AccessToken != "ACCESS_TOKEN" {
144- t .Errorf ("AccessToken wants %s but %s" , "ACCESS_TOKEN" , token .AccessToken )
145- }
146- if token .RefreshToken != "REFRESH_TOKEN" {
147- t .Errorf ("RefreshToken wants %s but %s" , "REFRESH_TOKEN" , token .RefreshToken )
148- }
149- }()
150- wg .Add (1 )
151- go func () {
152- defer wg .Done ()
153- toURL , ok := <- openBrowserCh
154- if ! ok {
155- t .Errorf ("server already closed" )
156- return
157- }
158- client .GetAndVerify (t , toURL , 200 , oauth2cli .DefaultLocalServerSuccessHTML )
159- }()
160- wg .Wait ()
161- }
162-
16392func TestSuccessRedirect (t * testing.T ) {
16493 ctx , cancel := context .WithTimeout (context .TODO (), 1 * time .Second )
16594 defer cancel ()
0 commit comments