@@ -2,12 +2,15 @@ package apitest_test
22
33import (
44 "context"
5+ "io"
56 "net/http"
67 "testing"
78
89 "github.com/adeithe/go-twitch/api"
910 "github.com/adeithe/go-twitch/apitest"
1011 "github.com/stretchr/testify/require"
12+ "golang.org/x/oauth2"
13+ "golang.org/x/oauth2/twitch"
1114)
1215
1316func TestMockAPI (t * testing.T ) {
@@ -278,3 +281,58 @@ func TestMockAPI_TokenMismatch(t *testing.T) {
278281 require .Exactly (t , 0 , endpoint .Successes )
279282 require .Exactly (t , 1 , endpoint .Failures )
280283}
284+
285+ func TestMockAPI_OAuth2_EndpointNotFound (t * testing.T ) {
286+ // The /oauth2/authorize endpoint is not supported by the apitest package.
287+ // This is because it's the user-facing endpoint for Twitch's OAuth2 flow and is not used by the API client.
288+ // This test ensures that requests to unsupported endpoints are properly handled by the mock server.
289+ req , err := http .NewRequest (http .MethodPost , "http://id.twitch.tv/oauth2/authorize" , nil )
290+ require .NoError (t , err )
291+
292+ mock := apitest .NewMockAPI (t )
293+ res , err := mock .Client ().Do (req )
294+ require .NoError (t , err )
295+ require .Equal (t , http .StatusNotFound , res .StatusCode )
296+
297+ bs , err := io .ReadAll (res .Body )
298+ require .NoError (t , err )
299+ require .Exactly (t , "404 Not Found" , string (bs ))
300+ }
301+
302+ func TestMockAPI_OAuth2_InvalidClient (t * testing.T ) {
303+ req , err := http .NewRequest (http .MethodPost , "http://id.twitch.tv/oauth2/token" , nil )
304+ require .NoError (t , err )
305+
306+ mock := apitest .NewMockAPI (t )
307+ res , err := mock .Client ().Do (req )
308+ require .NoError (t , err )
309+ require .Equal (t , http .StatusBadRequest , res .StatusCode )
310+
311+ bs , err := io .ReadAll (res .Body )
312+ require .NoError (t , err )
313+ require .JSONEq (t , `{"status": 400, "message": "invalid client"}` , string (bs ))
314+ }
315+
316+ func TestMockAPI_OAuth2_InvalidGrantType (t * testing.T ) {
317+ mock := apitest .NewMockAPI (t )
318+ ctx , cancel := context .WithCancel (context .WithValue (t .Context (), oauth2 .HTTPClient , mock .Client ()))
319+ defer cancel ()
320+
321+ oauthEndpoint := mock .OAuthTokenEndpoint ()
322+ clientID , clientSecret , err := mock .RegisterApplication ()
323+ require .NoError (t , err )
324+ oauth2Config := & oauth2.Config {
325+ ClientID : clientID ,
326+ ClientSecret : clientSecret ,
327+ Endpoint : twitch .Endpoint ,
328+ }
329+
330+ oauth2Config .Endpoint .AuthStyle = oauth2 .AuthStyleInParams
331+ token , err := oauth2Config .PasswordCredentialsToken (ctx , "username" , "password" )
332+ require .Nil (t , token )
333+ require .Error (t , err )
334+ require .Contains (t , err .Error (), "invalid grant type" )
335+ require .Exactly (t , 1 , oauthEndpoint .TimesCalled )
336+ require .Exactly (t , 0 , oauthEndpoint .Successes )
337+ require .Exactly (t , 1 , oauthEndpoint .Failures )
338+ }
0 commit comments