Skip to content

Commit

Permalink
feat: Add ability to exclude keys from HyperRequest and HyperResponse…
Browse files Browse the repository at this point in the history
… mementos
  • Loading branch information
elpete committed Jun 4, 2024
1 parent 1aa65e0 commit c0171fc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 45 deletions.
91 changes: 60 additions & 31 deletions models/HyperRequest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -1397,35 +1397,40 @@ component accessors="true" {
*
* @return struct
*/
public struct function getMemento() {
return {
"requestID" : getRequestID(),
"baseUrl" : getBaseUrl(),
"url" : getUrl(),
"fullUrl" : getFullUrl(),
"method" : getMethod(),
"queryParams" : getQueryParams(),
"headers" : getHeaders(),
"cookies" : getCookies(),
"files" : getFiles(),
"bodyFormat" : getBodyFormat(),
"body" : getBody(),
"referrerId" : isNull( variables.referrer ) ? "" : variables.referrer.getResponseID(),
"throwOnError" : getThrowOnError(),
"timeout" : getTimeout(),
"maximumRedirects" : getMaximumRedirects(),
"authType" : getAuthType(),
"username" : getUsername(),
"password" : getPassword(),
"clientCert" : isNull( variables.clientCert ) ? "" : variables.clientCert,
"clientCertPassword" : isNull( variables.clientCertPassword ) ? "" : variables.clientCertPassword,
"domain" : getDomain(),
"workstation" : getWorkstation(),
"resolveUrls" : getResolveUrls(),
"encodeUrl" : getEncodeUrl(),
"retries" : getRetries(),
"currentRequestCount" : getCurrentRequestCount()
};
public struct function getMemento( array excludes = [] ) {
return structFilter(
{
"requestID" : getRequestID(),
"baseUrl" : getBaseUrl(),
"url" : getUrl(),
"fullUrl" : getFullUrl(),
"method" : getMethod(),
"queryParams" : getQueryParams(),
"headers" : getHeaders(),
"cookies" : getCookies(),
"files" : getFiles(),
"bodyFormat" : getBodyFormat(),
"body" : getBody(),
"referrerId" : isNull( variables.referrer ) ? "" : variables.referrer.getResponseID(),
"throwOnError" : getThrowOnError(),
"timeout" : getTimeout(),
"maximumRedirects" : getMaximumRedirects(),
"authType" : getAuthType(),
"username" : getUsername(),
"password" : getPassword(),
"clientCert" : isNull( variables.clientCert ) ? "" : variables.clientCert,
"clientCertPassword" : isNull( variables.clientCertPassword ) ? "" : variables.clientCertPassword,
"domain" : getDomain(),
"workstation" : getWorkstation(),
"resolveUrls" : getResolveUrls(),
"encodeUrl" : getEncodeUrl(),
"retries" : getRetries(),
"currentRequestCount" : getCurrentRequestCount()
},
function( key ) {
return !arrayContainsNoCase( excludes, key );
}
);
}

/**
Expand Down Expand Up @@ -1463,7 +1468,19 @@ component accessors="true" {
for ( var pattern in variables.fakeConfiguration ) {
if ( getPathPatternMatcher().matchPattern( pattern, getFullUrl() ) ) {
if ( variables.builder.hasSequenceForPattern( pattern ) ) {
return variables.builder.record( this, variables.builder.popResponseForSequence( pattern ) );
var fakeRes = variables.builder.record( this, variables.builder.popResponseForSequence( pattern ) );
if ( fakeRes.getRequest().getThrowOnError() && fakeRes.isError() ) {
throw(
type = "HyperRequestError",
message = "Received a [#fakeRes.getStatus()#] response when requesting [#fakeRes.getRequest().getFullUrl()#]",
detail = fakeRes.getData(),
extendedinfo = serializeJSON( {
"request" : fakeRes.getRequest().getMemento(),
"response" : fakeRes.getMemento()
} )
);
}
return fakeRes;
}

var callback = variables.fakeConfiguration[ pattern ];
Expand All @@ -1485,7 +1502,19 @@ component accessors="true" {
}

variables.builder.registerSequence( pattern, res );
return variables.builder.record( this, variables.builder.popResponseForSequence( pattern ) );
var fakeRes = variables.builder.record( this, variables.builder.popResponseForSequence( pattern ) );
if ( fakeRes.getRequest().getThrowOnError() && fakeRes.isError() ) {
throw(
type = "HyperRequestError",
message = "Received a [#fakeRes.getStatus()#] response when requesting [#fakeRes.getRequest().getFullUrl()#]",
detail = fakeRes.getData(),
extendedinfo = serializeJSON( {
"request" : fakeRes.getRequest().getMemento(),
"response" : fakeRes.getMemento()
} )
);
}
return fakeRes;
}
}

Expand Down
33 changes: 19 additions & 14 deletions models/HyperResponse.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -322,20 +322,25 @@ component accessors="true" {
*
* @returns struct
*/
public struct function getMemento() {
return {
"responseID" : getResponseID(),
"requestID" : getRequestID(),
"statusCode" : getStatusCode(),
"statusText" : getStatusText(),
"status" : getStatus(),
"data" : getData(),
"charset" : getCharset(),
"headers" : getHeaders(),
"timestamp" : getTimestamp(),
"executionTime" : getExecutionTime(),
"cookies" : getCookies()
};
public struct function getMemento( array excludes = [] ) {
return structFilter(
{
"responseID" : getResponseID(),
"requestID" : getRequestID(),
"statusCode" : getStatusCode(),
"statusText" : getStatusText(),
"status" : getStatus(),
"data" : getData(),
"charset" : getCharset(),
"headers" : getHeaders(),
"timestamp" : getTimestamp(),
"executionTime" : getExecutionTime(),
"cookies" : getCookies()
},
function( key ) {
return !arrayContainsNoCase( excludes, key );
}
);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/specs/unit/HyperRequestSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ component extends="testbox.system.BaseSpec" {
} );
} );

it( "can exclude keys from the memento", () => {
var memento = variables.req.getMemento( excludes = [ "cookies" ] );
expect( memento ).notToHaveKey( "cookies" );
} );

it( "can set multiple values at once from a struct", function() {
expect( req.getUrl() ).toBe( "" );
expect( req.getMethod() ).toBe( "GET" );
Expand Down
12 changes: 12 additions & 0 deletions tests/specs/unit/HyperResponseSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ component extends="testbox.system.BaseSpec" {
} );
} );

it( "can exclude keys from the memento", function() {
var res = new Hyper.models.HyperResponse(
originalRequest = createStub( extends = "models.HyperRequest" ).$( "getRequestID", createUUID() ),
statusCode = 200,
executionTime = 100,
headers = { "Content-Type" : "text/html; charset=utf-8" }
);

var memento = res.getMemento( excludes = [ "cookies" ] );
expect( memento ).notToHaveKey( "cookies" );
} );

it( "throws an exception when requesting json data but the data is not json", function() {
var res = new Hyper.models.HyperResponse(
originalRequest = createStub( extends = "models.HyperRequest" ),
Expand Down

0 comments on commit c0171fc

Please sign in to comment.