Skip to content

Commit

Permalink
feat(HyperRequest): Add forwardHeaders shortcut method
Browse files Browse the repository at this point in the history
`forwardHeaders` will add an array of header names to the HyperRequest
if they exist in the headers struct.  By default, the headers struct
is the headers from the current CFML request (`getHTTPRequestData( false ).headers`).

Hyper Clients can be configured to automatically forward headers by
registering a request callback:

```
map( "MyAPIClient" )
    .to( "hyper.models.HyperBuilder" )
    .asSingleton()
    .initWith(
        baseUrl = "https://my-api.example.com",
        requestCallbacks = [
            function( req ) {
                req.forwardHeaders( [ "X-Forwarded-For" ] );
            }
        ]
    );
```

Closes #28
  • Loading branch information
elpete committed Feb 2, 2022
1 parent b428f22 commit 5924787
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ Add additional headers to the request.
| ------- | ------ | -------- | ------- | ------------------------------------------ |
| headers | struct | true | | A struct of headers to add to the request. |

##### `forwardHeaders`

Adds specified headers to the request if they exist. Usually used in conjunction with the current CFML request headers.

| Name | Type | Required | Default | Description |
| ------- | ------ | -------- | ------------------------------------- | ------------------------------------------------------------------------------------- |
| names | array | true | | An array of header names to add to the request if they exist in the `headers` struct. |
| headers | struct | false | `getHTTPRequestData( false ).headers` | A struct of headers to inspect. |

##### `hasHeader`

Check if the request has a header with the given name.
Expand Down
18 changes: 18 additions & 0 deletions models/HyperRequest.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,24 @@ component accessors="true" {
return this;
}

/**
* Adds specified headers to the request if they exist.
* Usually used in conjunction with the current CFML request headers.
*
* @names An array of header names to add to the request, if they exist in the `headers` struct.
* @headers A struct of headers to inspect. Default: `getHTTPRequestData( false ).headers`
*
* @returns The HyperRequest instance.
*/
function forwardHeaders( required array names, struct headers = getHTTPRequestData( false ).headers ) {
for ( var name in arguments.names ) {
if ( arguments.headers.keyExists( name ) ) {
setHeader( name, arguments.headers[ name ] );
}
}
return this;
}

/**
* Check if the request has a header with the given name.
*
Expand Down
15 changes: 15 additions & 0 deletions tests/specs/unit/HyperRequestSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ component extends="testbox.system.BaseSpec" {
expect( responseId ).toBe( res.getResponseId() );
expect( statusCode ).toBe( 201 );
} );

it( "can forward on headers if they exist", function() {
expect( req.hasHeader( "X-Requested-With" ) ).toBeFalse();
expect( req.hasHeader( "X-Forwarded-For" ) ).toBeFalse();
req.forwardHeaders(
[
"X-Forwarded-For",
"X-Requested-With"
],
{ "X-Forwarded-For" : "1.1.1.1" }
);
expect( req.hasHeader( "X-Requested-With" ) ).toBeFalse();
expect( req.hasHeader( "X-Forwarded-For" ) ).toBeTrue();
expect( req.getHeader( "X-Forwarded-For" ) ).toBe( "1.1.1.1" );
} );
} );
}

Expand Down

0 comments on commit 5924787

Please sign in to comment.