import { match , SinonMatcher } from "sinon" ; import { ServiceSpy } from ".." ; import { HTTPMethod , IIncomingHeaders , IIncomingQuery , IOutgoingHeaders , IProtocol , } from "../../../interfaces" ; import { decorateSpy , verifyOnlyOneCall } from "../decorate" ; export interface ISpyDecoration { with ( matcher ) : ServiceSpy ; withMethod ( method ) : ServiceSpy ; postRequestHost ( matcher ? ) : string ; postRequestBody ( matcher ? ) : string | undefined ; postRequestBodyAsJson ( matcher ? ) : any ; postRequestPathname ( matcher ? ) : string ; postRequestPath ( matcher ? ) : string ; postRequestHeaders ( matcher ? ) : IIncomingHeaders ; postRequestQuery ( matcher ? ) : IIncomingQuery ; postRequestProtocol ( matcher ? ) : IProtocol ; postResponseBody ( matcher ? ) : string | undefined ; postResponseBodyAsJson ( matcher ? ) : any ; postResponseCode ( matcher ? ) : number ; postResponseHeaders ( matcher ? ) : IOutgoingHeaders ; getRequestHost ( matcher ? ) : string ; getRequestBody ( matcher ? ) : string | undefined ; getRequestBodyAsJson ( matcher ? ) : any ; getRequestPathname ( matcher ? ) : string ; getRequestPath ( matcher ? ) : string ; getRequestHeaders ( matcher ? ) : IIncomingHeaders ; getRequestQuery ( matcher ? ) : IIncomingQuery ; getRequestProtocol ( matcher ? ) : IProtocol ; getResponseBody ( matcher ? ) : string | undefined ; getResponseBodyAsJson ( matcher ? ) : any ; getResponseCode ( matcher ? ) : number ; getResponseHeaders ( matcher ? ) : IOutgoingHeaders ; putRequestHost ( matcher ? ) : string ; putRequestBody ( matcher ? ) : string | undefined ; putRequestBodyAsJson ( matcher ? ) : any ; putRequestPathname ( matcher ? ) : string ; putRequestPath ( matcher ? ) : string ; putRequestHeaders ( matcher ? ) : IIncomingHeaders ; putRequestQuery ( matcher ? ) : IIncomingQuery ; putRequestProtocol ( matcher ? ) : IProtocol ; putResponseBody ( matcher ? ) : string | undefined ; putResponseBodyAsJson ( matcher ? ) : any ; putResponseCode ( matcher ? ) : number ; putResponseHeaders ( matcher ? ) : IOutgoingHeaders ; deleteRequestHost ( matcher ? ) : string ; deleteRequestBody ( matcher ? ) : string | undefined ; deleteRequestBodyAsJson ( matcher ? ) : any ; deleteRequestPathname ( matcher ? ) : string ; deleteRequestPath ( matcher ? ) : string ; deleteRequestHeaders ( matcher ? ) : IIncomingHeaders ; deleteRequestQuery ( matcher ? ) : IIncomingQuery ; deleteRequestProtocol ( matcher ? ) : IProtocol ; deleteResponseBody ( matcher ? ) : string | undefined ; deleteResponseBodyAsJson ( matcher ? ) : any ; deleteResponseCode ( matcher ? ) : number ; deleteResponseHeaders ( matcher ? ) : IOutgoingHeaders ; } export const decorators = { with ( this , matcher ) { return decorateSpy ( this . withArgs ( matcher ) ) ; } , withMethod ( this , method ) { return this . with ( match ( { method } ) ) ; } , postRequestHost ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postRequestHost" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . host ; } , postRequestBody ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postRequestBody" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . body ; } , postRequestBodyAsJson ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postRequestBodyAsJson" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . bodyAsJson ; } , postRequestPath ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postRequestPath" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . path ; } , postRequestPathname ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postRequestPathname" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . pathname ; } , postRequestHeaders ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postRequestHeaders" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . headers ; } , postRequestQuery ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postRequestQuery" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . query ; } , postRequestProtocol ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postRequestProtocol" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . protocol ; } , postResponseBody ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postResponseBody" } ) ; return spyWithMatcher . firstCall . returnValue . body ; } , postResponseBodyAsJson ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postResponseBodyAsJson" , } ) ; return spyWithMatcher . firstCall . returnValue . bodyAsJson ; } , postResponseCode ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postResponseCode" } ) ; return spyWithMatcher . firstCall . returnValue . statusCode ; } , postResponseHeaders ( this , matcher ? ) { const methodMatcher = match ( { method : "post" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "postResponseHeaders" , } ) ; return spyWithMatcher . firstCall . returnValue . headers ; } , getRequestHost ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getRequestHost" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . host ; } , getRequestBody ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getRequestBody" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . body ; } , getRequestBodyAsJson ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getRequestBodyAsJson" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . bodyAsJson ; } , getRequestPath ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getRequestPath" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . path ; } , getRequestPathname ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getRequestPathname" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . pathname ; } , getRequestHeaders ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getRequestHeaders" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . headers ; } , getRequestQuery ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getRequestQuery" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . query ; } , getRequestProtocol ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getRequestProtocol" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . protocol ; } , getResponseBody ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getResponseBody" } ) ; return spyWithMatcher . firstCall . returnValue . body ; } , getResponseBodyAsJson ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getResponseBodyAsJson" , } ) ; return spyWithMatcher . firstCall . returnValue . bodyAsJson ; } , getResponseCode ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getResponseCode" } ) ; return spyWithMatcher . firstCall . returnValue . statusCode ; } , getResponseHeaders ( this , matcher ? ) { const methodMatcher = match ( { method : "get" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "getResponseHeaders" } ) ; return spyWithMatcher . firstCall . returnValue . headers ; } , putRequestHost ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putRequestHost" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . host ; } , putRequestBody ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putRequestBody" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . body ; } , putRequestBodyAsJson ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putRequestBodyAsJson" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . bodyAsJson ; } , putRequestPath ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putRequestPath" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . path ; } , putRequestPathname ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putRequestPathname" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . pathname ; } , putRequestHeaders ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putRequestHeaders" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . headers ; } , putRequestQuery ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putRequestQuery" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . query ; } , putRequestProtocol ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putRequestProtocol" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . protocol ; } , putResponseBody ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putResponseBody" } ) ; return spyWithMatcher . firstCall . returnValue . body ; } , putResponseBodyAsJson ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putResponseBodyAsJson" , } ) ; return spyWithMatcher . firstCall . returnValue . bodyAsJson ; } , putResponseCode ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putResponseCode" } ) ; return spyWithMatcher . firstCall . returnValue . statusCode ; } , putResponseHeaders ( this , matcher ? ) { const methodMatcher = match ( { method : "put" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "putResponseHeaders" } ) ; return spyWithMatcher . firstCall . returnValue . headers ; } , deleteRequestHost ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteRequestHost" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . host ; } , deleteRequestBody ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteRequestBody" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . body ; } , deleteRequestBodyAsJson ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteRequestBodyAsJson" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . bodyAsJson ; } , deleteRequestPath ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteRequestPath" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . path ; } , deleteRequestPathname ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteRequestPathname" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . pathname ; } , deleteRequestHeaders ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteRequestHeaders" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . headers ; } , deleteRequestQuery ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteRequestQuery" } ) ; return spyWithMatcher . firstCall . args [ 0 ] . query ; } , deleteRequestProtocol ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteRequestProtocol" , } ) ; return spyWithMatcher . firstCall . args [ 0 ] . protocol ; } , deleteResponseBody ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteResponseBody" } ) ; return spyWithMatcher . firstCall . returnValue . body ; } , deleteResponseBodyAsJson ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteResponseBodyAsJson" , } ) ; return spyWithMatcher . firstCall . returnValue . bodyAsJson ; } , deleteResponseCode ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteResponseCode" } ) ; return spyWithMatcher . firstCall . returnValue . statusCode ; } , deleteResponseHeaders ( this , matcher ? ) { const methodMatcher = match ( { method : "delete" } ) ; const fullMatcher = matcher ? methodMatcher . and ( matcher ) : methodMatcher ; const spyWithMatcher = this . with ( fullMatcher ) ; verifyOnlyOneCall ( { spy : spyWithMatcher , errPrefix : "deleteResponseHeaders" , } ) ; return spyWithMatcher . firstCall . returnValue . headers ; } , } ;