@@ -487,8 +487,6 @@ describe('SwaggerClient', function () {
487487 var interceptor = {
488488 requestInterceptor : {
489489 apply : function ( requestObj ) {
490- // rewrites an invalid pet id (-100) to be valid (1)
491- // you can do what you want here, like inject headers, etc.
492490 startTime = new Date ( ) . getTime ( ) ;
493491 return requestObj ;
494492 }
@@ -507,7 +505,6 @@ describe('SwaggerClient', function () {
507505 requestInterceptor : interceptor . requestInterceptor ,
508506 responseInterceptor : interceptor . responseInterceptor
509507 } ) . then ( function ( client ) {
510- console . log ( 'got the client!' ) ;
511508 client . pet . getPetById ( { petId : 1 } ) . then ( function ( pet ) {
512509 expect ( pet . obj ) . toBeAn ( 'object' ) ;
513510 expect ( elapsed ) . toBeGreaterThan ( 0 ) ;
@@ -517,4 +514,73 @@ describe('SwaggerClient', function () {
517514 done ( exception ) ;
518515 } ) ;
519516 } ) ;
517+
518+
519+ it ( 'passes headers to the request interceptor' , function ( done ) {
520+ var spec = {
521+ paths : {
522+ '/foo' : {
523+ post : {
524+ operationId : 'addFoo' ,
525+ tags : [
526+ 'nada'
527+ ] ,
528+ parameters : [
529+ {
530+ in : 'header' ,
531+ name : 'username' ,
532+ type : 'string'
533+ }
534+ ] ,
535+ responses : {
536+ '200' : {
537+ description : 'ok'
538+ }
539+ }
540+ }
541+ }
542+ }
543+ } ;
544+ var interceptor = {
545+ requestInterceptor : {
546+ apply : function ( requestObj ) {
547+ /**
548+ * Verify the payload. You have the following available in `requestObj`:
549+ *
550+ * request.Obj.headers <= map of headers to be sent, includes Content-Type, Accept headers
551+ * requestObj.body <= the content to send, undefined of none
552+ * requestObj.method <= the HTTP operation to execute
553+ * requestObj.url <= the fully resolved URL, including query params, to send
554+ * requestObj.on.response <= success function to execute
555+ * requestObj.on.error <= error function to execute on failure
556+ *
557+ * NOTE! It is not recommended to override the on.response / on.error functions as it may
558+ * interrupt downstream processing in the client. Use the responseInterceptor pattern instead
559+ *
560+ **/
561+
562+ // ensure the headers are present
563+ expect ( requestObj . headers . username ) . toBe ( 'bob' ) ;
564+
565+ // rewrite this request to something that'll work locally
566+ requestObj . method = 'GET' ;
567+ requestObj . url = 'http://localhost:8000/v2/api/pet/1'
568+ return requestObj ;
569+ }
570+ }
571+ } ;
572+
573+ new SwaggerClient ( {
574+ url : 'http://petstore.swagger.io/v2/swagger.json' ,
575+ spec : spec ,
576+ usePromise : true ,
577+ requestInterceptor : interceptor . requestInterceptor
578+ } ) . then ( function ( client ) {
579+ client . nada . addFoo ( { username : 'bob' } ) . then ( function ( data ) {
580+ done ( ) ;
581+ } ) ;
582+ } ) . catch ( function ( exception ) {
583+ done ( exception ) ;
584+ } ) ;
585+ } ) ;
520586} ) ;
0 commit comments