github.com/resonatecoop/go-oauth2-server@v1.0.1/oauth/handlers_test.go (about)

     1  package oauth_test
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"net/url"
     7  
     8  	"github.com/RichardKnop/go-oauth2-server/oauth"
     9  	"github.com/RichardKnop/go-oauth2-server/test-util"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func (suite *OauthTestSuite) TestTokensHandlerClientAuthenticationRequired() {
    14  	// Prepare a request
    15  	r, err := http.NewRequest("POST", "http://1.2.3.4/v1/oauth/tokens", nil)
    16  	assert.NoError(suite.T(), err, "Request setup should not get an error")
    17  	r.PostForm = url.Values{"grant_type": {"client_credentials"}}
    18  
    19  	// Serve the request
    20  	w := httptest.NewRecorder()
    21  	suite.router.ServeHTTP(w, r)
    22  
    23  	// Check the response
    24  	testutil.TestResponseForError(
    25  		suite.T(),
    26  		w,
    27  		oauth.ErrInvalidClientIDOrSecret.Error(),
    28  		401,
    29  	)
    30  }
    31  
    32  func (suite *OauthTestSuite) TestTokensHandlerInvalidGrantType() {
    33  	// Make a request
    34  	r, err := http.NewRequest("POST", "http://1.2.3.4/v1/oauth/tokens", nil)
    35  	assert.NoError(suite.T(), err, "Request setup should not get an error")
    36  	r.SetBasicAuth("test_client", "test_secret")
    37  	r.PostForm = url.Values{"grant_type": {"bogus"}}
    38  
    39  	// Serve the request
    40  	w := httptest.NewRecorder()
    41  	suite.router.ServeHTTP(w, r)
    42  
    43  	// Check the response
    44  	testutil.TestResponseForError(
    45  		suite.T(),
    46  		w,
    47  		oauth.ErrInvalidGrantType.Error(),
    48  		400,
    49  	)
    50  }
    51  
    52  func (suite *OauthTestSuite) TestIntrospectHandlerClientAuthenticationRequired() {
    53  	// Prepare a request
    54  	r, err := http.NewRequest("POST", "http://1.2.3.4/v1/oauth/introspect", nil)
    55  	assert.NoError(suite.T(), err, "Request setup should not get an error")
    56  	r.PostForm = url.Values{"token": {"token"}}
    57  
    58  	// Serve the request
    59  	w := httptest.NewRecorder()
    60  	suite.router.ServeHTTP(w, r)
    61  
    62  	// Check the response
    63  	testutil.TestResponseForError(
    64  		suite.T(),
    65  		w,
    66  		oauth.ErrInvalidClientIDOrSecret.Error(),
    67  		401,
    68  	)
    69  }