import moxios from "moxios" ; import axios from "axios" ; import aws4Interceptor from "." ; describe ( "axios interceptor" , ( ) => { beforeEach ( ( ) => { moxios . install ( ) ; } ) ; afterEach ( ( ) => { moxios . uninstall ( ) ; } ) ; it ( "should not mutate request config object" , async ( ) => { const client = axios . create ( ) ; client . interceptors . request . use ( aws4Interceptor ( { region : "local" } ) ) ; const url = "https://localhost/foo" ; const config = { headers : { "X-Custom-Header" : "foo" , "Content-Type" : "application/json" } , params : { foo : "bar" } , } ; moxios . stubOnce ( "GET" , /./ , { } ) ; await client . get ( url , config ) ; const request = moxios . requests . first ( ) ; expect ( request . url ) . toBe ( `${ url }?foo=bar` ) ; expect ( config . params ) . toStrictEqual ( { foo : "bar" } ) ; } ) ; it ( "should preserve headers" , async ( ) => { const client = axios . create ( ) ; client . interceptors . request . use ( aws4Interceptor ( { region : "local" } ) ) ; const data = { foo : "bar" } ; const url = "https://localhost/foo" ; moxios . stubOnce ( "POST" , url , { } ) ; await client . post ( url , data , { headers : { "X-Custom-Header" : "foo" , "Content-Type" : "application/json" } , } ) ; const request = moxios . requests . first ( ) ; expect ( request . headers [ "Content-Type" ] ) . toEqual ( "application/json" ) ; expect ( request . headers [ "X-Custom-Header" ] ) . toEqual ( "foo" ) ; expect ( request . headers [ "Authorization" ] ) . toContain ( "AWS" ) ; } ) ; it ( "should preserve default headers - without interceptor" , async ( ) => { const client = axios . create ( ) ; const data = { foo : "bar" } ; const url = "https://localhost/foo" ; moxios . stubOnce ( "POST" , url , { } ) ; await client . post ( url , data , { } ) ; const request = moxios . requests . first ( ) ; expect ( request . headers [ "Content-Type" ] ) . toEqual ( "application/json;charset=utf-8" ) ; } ) ; it ( "should preserve default headers - with interceptor" , async ( ) => { const client = axios . create ( ) ; client . interceptors . request . use ( aws4Interceptor ( { region : "local" } ) ) ; const data = { foo : "bar" } ; const url = "https://localhost/foo" ; moxios . stubOnce ( "POST" , url , { } ) ; await client . post ( url , data , { } ) ; const request = moxios . requests . first ( ) ; expect ( request . headers [ "Content-Type" ] ) . toEqual ( "application/json;charset=utf-8" ) ; } ) ; } ) ;