Skip to content

Commit c0171fc

Browse files
committed
feat: Add ability to exclude keys from HyperRequest and HyperResponse mementos
1 parent 1aa65e0 commit c0171fc

File tree

4 files changed

+96
-45
lines changed

4 files changed

+96
-45
lines changed

models/HyperRequest.cfc

Lines changed: 60 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,35 +1397,40 @@ component accessors="true" {
13971397
*
13981398
* @return struct
13991399
*/
1400-
public struct function getMemento() {
1401-
return {
1402-
"requestID" : getRequestID(),
1403-
"baseUrl" : getBaseUrl(),
1404-
"url" : getUrl(),
1405-
"fullUrl" : getFullUrl(),
1406-
"method" : getMethod(),
1407-
"queryParams" : getQueryParams(),
1408-
"headers" : getHeaders(),
1409-
"cookies" : getCookies(),
1410-
"files" : getFiles(),
1411-
"bodyFormat" : getBodyFormat(),
1412-
"body" : getBody(),
1413-
"referrerId" : isNull( variables.referrer ) ? "" : variables.referrer.getResponseID(),
1414-
"throwOnError" : getThrowOnError(),
1415-
"timeout" : getTimeout(),
1416-
"maximumRedirects" : getMaximumRedirects(),
1417-
"authType" : getAuthType(),
1418-
"username" : getUsername(),
1419-
"password" : getPassword(),
1420-
"clientCert" : isNull( variables.clientCert ) ? "" : variables.clientCert,
1421-
"clientCertPassword" : isNull( variables.clientCertPassword ) ? "" : variables.clientCertPassword,
1422-
"domain" : getDomain(),
1423-
"workstation" : getWorkstation(),
1424-
"resolveUrls" : getResolveUrls(),
1425-
"encodeUrl" : getEncodeUrl(),
1426-
"retries" : getRetries(),
1427-
"currentRequestCount" : getCurrentRequestCount()
1428-
};
1400+
public struct function getMemento( array excludes = [] ) {
1401+
return structFilter(
1402+
{
1403+
"requestID" : getRequestID(),
1404+
"baseUrl" : getBaseUrl(),
1405+
"url" : getUrl(),
1406+
"fullUrl" : getFullUrl(),
1407+
"method" : getMethod(),
1408+
"queryParams" : getQueryParams(),
1409+
"headers" : getHeaders(),
1410+
"cookies" : getCookies(),
1411+
"files" : getFiles(),
1412+
"bodyFormat" : getBodyFormat(),
1413+
"body" : getBody(),
1414+
"referrerId" : isNull( variables.referrer ) ? "" : variables.referrer.getResponseID(),
1415+
"throwOnError" : getThrowOnError(),
1416+
"timeout" : getTimeout(),
1417+
"maximumRedirects" : getMaximumRedirects(),
1418+
"authType" : getAuthType(),
1419+
"username" : getUsername(),
1420+
"password" : getPassword(),
1421+
"clientCert" : isNull( variables.clientCert ) ? "" : variables.clientCert,
1422+
"clientCertPassword" : isNull( variables.clientCertPassword ) ? "" : variables.clientCertPassword,
1423+
"domain" : getDomain(),
1424+
"workstation" : getWorkstation(),
1425+
"resolveUrls" : getResolveUrls(),
1426+
"encodeUrl" : getEncodeUrl(),
1427+
"retries" : getRetries(),
1428+
"currentRequestCount" : getCurrentRequestCount()
1429+
},
1430+
function( key ) {
1431+
return !arrayContainsNoCase( excludes, key );
1432+
}
1433+
);
14291434
}
14301435

14311436
/**
@@ -1463,7 +1468,19 @@ component accessors="true" {
14631468
for ( var pattern in variables.fakeConfiguration ) {
14641469
if ( getPathPatternMatcher().matchPattern( pattern, getFullUrl() ) ) {
14651470
if ( variables.builder.hasSequenceForPattern( pattern ) ) {
1466-
return variables.builder.record( this, variables.builder.popResponseForSequence( pattern ) );
1471+
var fakeRes = variables.builder.record( this, variables.builder.popResponseForSequence( pattern ) );
1472+
if ( fakeRes.getRequest().getThrowOnError() && fakeRes.isError() ) {
1473+
throw(
1474+
type = "HyperRequestError",
1475+
message = "Received a [#fakeRes.getStatus()#] response when requesting [#fakeRes.getRequest().getFullUrl()#]",
1476+
detail = fakeRes.getData(),
1477+
extendedinfo = serializeJSON( {
1478+
"request" : fakeRes.getRequest().getMemento(),
1479+
"response" : fakeRes.getMemento()
1480+
} )
1481+
);
1482+
}
1483+
return fakeRes;
14671484
}
14681485

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

14871504
variables.builder.registerSequence( pattern, res );
1488-
return variables.builder.record( this, variables.builder.popResponseForSequence( pattern ) );
1505+
var fakeRes = variables.builder.record( this, variables.builder.popResponseForSequence( pattern ) );
1506+
if ( fakeRes.getRequest().getThrowOnError() && fakeRes.isError() ) {
1507+
throw(
1508+
type = "HyperRequestError",
1509+
message = "Received a [#fakeRes.getStatus()#] response when requesting [#fakeRes.getRequest().getFullUrl()#]",
1510+
detail = fakeRes.getData(),
1511+
extendedinfo = serializeJSON( {
1512+
"request" : fakeRes.getRequest().getMemento(),
1513+
"response" : fakeRes.getMemento()
1514+
} )
1515+
);
1516+
}
1517+
return fakeRes;
14891518
}
14901519
}
14911520

models/HyperResponse.cfc

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -322,20 +322,25 @@ component accessors="true" {
322322
*
323323
* @returns struct
324324
*/
325-
public struct function getMemento() {
326-
return {
327-
"responseID" : getResponseID(),
328-
"requestID" : getRequestID(),
329-
"statusCode" : getStatusCode(),
330-
"statusText" : getStatusText(),
331-
"status" : getStatus(),
332-
"data" : getData(),
333-
"charset" : getCharset(),
334-
"headers" : getHeaders(),
335-
"timestamp" : getTimestamp(),
336-
"executionTime" : getExecutionTime(),
337-
"cookies" : getCookies()
338-
};
325+
public struct function getMemento( array excludes = [] ) {
326+
return structFilter(
327+
{
328+
"responseID" : getResponseID(),
329+
"requestID" : getRequestID(),
330+
"statusCode" : getStatusCode(),
331+
"statusText" : getStatusText(),
332+
"status" : getStatus(),
333+
"data" : getData(),
334+
"charset" : getCharset(),
335+
"headers" : getHeaders(),
336+
"timestamp" : getTimestamp(),
337+
"executionTime" : getExecutionTime(),
338+
"cookies" : getCookies()
339+
},
340+
function( key ) {
341+
return !arrayContainsNoCase( excludes, key );
342+
}
343+
);
339344
}
340345

341346
/**

tests/specs/unit/HyperRequestSpec.cfc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ component extends="testbox.system.BaseSpec" {
4343
} );
4444
} );
4545

46+
it( "can exclude keys from the memento", () => {
47+
var memento = variables.req.getMemento( excludes = [ "cookies" ] );
48+
expect( memento ).notToHaveKey( "cookies" );
49+
} );
50+
4651
it( "can set multiple values at once from a struct", function() {
4752
expect( req.getUrl() ).toBe( "" );
4853
expect( req.getMethod() ).toBe( "GET" );

tests/specs/unit/HyperResponseSpec.cfc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ component extends="testbox.system.BaseSpec" {
2525
} );
2626
} );
2727

28+
it( "can exclude keys from the memento", function() {
29+
var res = new Hyper.models.HyperResponse(
30+
originalRequest = createStub( extends = "models.HyperRequest" ).$( "getRequestID", createUUID() ),
31+
statusCode = 200,
32+
executionTime = 100,
33+
headers = { "Content-Type" : "text/html; charset=utf-8" }
34+
);
35+
36+
var memento = res.getMemento( excludes = [ "cookies" ] );
37+
expect( memento ).notToHaveKey( "cookies" );
38+
} );
39+
2840
it( "throws an exception when requesting json data but the data is not json", function() {
2941
var res = new Hyper.models.HyperResponse(
3042
originalRequest = createStub( extends = "models.HyperRequest" ),

0 commit comments

Comments
 (0)