55 */
66
77const AuthenticateHandler = require ( '../../../lib/handlers/authenticate-handler' ) ;
8+ const InvalidRequestError = require ( '../../../lib/errors/invalid-request-error' ) ;
89const Request = require ( '../../../lib/request' ) ;
910const sinon = require ( 'sinon' ) ;
1011const should = require ( 'chai' ) . should ( ) ;
@@ -16,6 +17,33 @@ const ServerError = require('../../../lib/errors/server-error');
1617
1718describe ( 'AuthenticateHandler' , function ( ) {
1819 describe ( 'getTokenFromRequest()' , function ( ) {
20+ describe ( 'with bearer token in the request authorization header' , function ( ) {
21+ it ( 'should throw an error if the token is malformed' , ( ) => {
22+ const handler = new AuthenticateHandler ( {
23+ model : { getAccessToken ( ) { } } ,
24+ } ) ;
25+ const request = new Request ( {
26+ body : { } ,
27+ headers : {
28+ Authorization : 'foo Bearer bar' ,
29+ } ,
30+ method : 'ANY' ,
31+ query : { } ,
32+ } ) ;
33+
34+ try {
35+ handler . getTokenFromRequestHeader ( request ) ;
36+
37+ should . fail ( 'should.fail' , '' ) ;
38+ } catch ( e ) {
39+ e . should . be . an . instanceOf ( InvalidRequestError ) ;
40+ e . message . should . equal (
41+ 'Invalid request: malformed authorization header' ,
42+ ) ;
43+ }
44+ } ) ;
45+ } ) ;
46+
1947 describe ( 'with bearer token in the request authorization header' , function ( ) {
2048 it ( 'should call `getTokenFromRequestHeader()`' , function ( ) {
2149 const handler = new AuthenticateHandler ( { model : { getAccessToken : function ( ) { } } } ) ;
0 commit comments