5
5
*/
6
6
7
7
const AuthenticateHandler = require ( '../../../lib/handlers/authenticate-handler' ) ;
8
+ const InvalidRequestError = require ( '../../../lib/errors/invalid-request-error' ) ;
8
9
const Request = require ( '../../../lib/request' ) ;
9
10
const sinon = require ( 'sinon' ) ;
10
11
const should = require ( 'chai' ) . should ( ) ;
@@ -16,6 +17,33 @@ const ServerError = require('../../../lib/errors/server-error');
16
17
17
18
describe ( 'AuthenticateHandler' , function ( ) {
18
19
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
+
19
47
describe ( 'with bearer token in the request authorization header' , function ( ) {
20
48
it ( 'should call `getTokenFromRequestHeader()`' , function ( ) {
21
49
const handler = new AuthenticateHandler ( { model : { getAccessToken : function ( ) { } } } ) ;
0 commit comments