Skip to content

Commit f4adf3e

Browse files
committed
Add withHeaders method to set multiple headers at once
1 parent 03a05a3 commit f4adf3e

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

models/HyperRequest.cfc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ component accessors="true" {
3737
return makeRequest();
3838
}
3939

40+
function withHeaders( headers = {} ) {
41+
structEach( headers, setHeader );
42+
return this;
43+
}
44+
4045
function setHeader( name, value ) {
4146
variables.headers[ lcase( name ) ] = value;
4247
return this;

tests/specs/unit/HyperRequestSpec.cfc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ component extends="testbox.system.BaseSpec" {
2323
expect( req.getMethod() ).toBe( "PATCH" );
2424
} );
2525

26+
it( "can set multiple headers at once", function() {
27+
expect( req.getHeader( "Accept" ) ).toBe( "" );
28+
expect( req.getHeader( "X-Requested-With" ) ).toBe( "" );
29+
req.withHeaders( {
30+
"Accept" = "application/xml",
31+
"X-Requested-With" = "XMLHTTPRequest"
32+
} );
33+
expect( req.getHeader( "Accept" ) ).toBe( "application/xml" );
34+
expect( req.getHeader( "X-Requested-With" ) ).toBe( "XMLHTTPRequest" );
35+
} );
36+
2637
it( "throws an exception if the url is empty when trying to make a request", function() {
2738
expect( function() {
2839
req.get();

0 commit comments

Comments
 (0)