Skip to content

Commit 60100c4

Browse files
committed
feat(HyperRequest): Default to a HyperCFML/#versionNumber# User-Agent
Hyper now includes a default User-Agent where it had no default before. The default User-Agent is `HyperCFML/#versionNumber#`.
1 parent beab1b7 commit 60100c4

3 files changed

Lines changed: 40 additions & 7 deletions

File tree

‎models/HyperRequest.cfc‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ component accessors="true" {
185185
variables.files = [];
186186
variables.requestCallbacks = [];
187187
variables.responseCallbacks = [];
188+
189+
setUserAgent( "HyperCFML/#getHyperVersion()#" );
188190
// This is overwritten by the HyperBuilder if WireBox exists.
189191
variables.interceptorService = {
190192
"processState" : function() {
@@ -941,6 +943,18 @@ component accessors="true" {
941943
return this;
942944
}
943945

946+
/**
947+
* A convenience method to set the User-Agent header.
948+
*
949+
* @type The User-Agent value for the request.
950+
*
951+
* @returns The HyperRequest instance.
952+
*/
953+
public HyperRequest function setUserAgent( required string userAgent ) {
954+
setHeader( "User-Agent", arguments.userAgent );
955+
return this;
956+
}
957+
944958
/**
945959
* A convenience method to set the Content-Type header.
946960
*
@@ -1383,4 +1397,11 @@ component accessors="true" {
13831397
return variables.pathPatternMatcher;
13841398
}
13851399

1400+
public string function getHyperVersion() {
1401+
if ( !structKeyExists( application, "hyperVersion" ) ) {
1402+
application.hyperVersion = deserializeJSON( fileRead( expandPath( "/hyper/box.json" ) ) ).version;
1403+
}
1404+
return application.hyperVersion;
1405+
}
1406+
13861407
}

‎tests/specs/integration/DebugSpec.cfc‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
99
describe( "debug spec", function() {
1010
it( "can print out information about the cfhttp call that will be made", function() {
1111
var binaryBody = charsetDecode( "some binary body", "utf-8" );
12-
var debugReq = hyper
12+
var req = hyper
1313
.withHeaders( {
1414
"Accept" : "application/json;odata=verbose",
1515
"Authorization" : "Bearer token",
@@ -19,8 +19,9 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
1919
.setBody( binaryBody )
2020
.setBodyFormat( "other|binary" )
2121
.setMethod( "POST" )
22-
.setUrl( "https://example.com" )
23-
.debug();
22+
.setUrl( "https://example.com" );
23+
24+
var debugReq = req.debug();
2425

2526
expect( debugReq ).toBeStruct();
2627

@@ -52,7 +53,7 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
5253
return compare( a.name, b.name );
5354
} );
5455
expect( headers ).toBeArray();
55-
expect( headers ).toHaveLength( 3 );
56+
expect( headers ).toHaveLength( 4 );
5657

5758
expect( headers[ 1 ] ).toBeStruct();
5859
expect( headers[ 1 ] ).toHaveKey( "name" );
@@ -71,6 +72,12 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
7172
expect( headers[ 3 ].name ).toBe( "Content-Type" );
7273
expect( headers[ 3 ] ).toHaveKey( "value" );
7374
expect( headers[ 3 ].value ).toBe( "application/octet-stream" );
75+
76+
expect( headers[ 4 ] ).toBeStruct();
77+
expect( headers[ 4 ] ).toHaveKey( "name" );
78+
expect( headers[ 4 ].name ).toBe( "User-Agent" );
79+
expect( headers[ 4 ] ).toHaveKey( "value" );
80+
expect( headers[ 4 ].value ).toBe( "HyperCFML/#req.getHyperVersion()#" );
7481
} );
7582
} );
7683
}

‎tests/specs/unit/HyperRequestSpec.cfc‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ component extends="testbox.system.BaseSpec" {
176176
expect( method ).toBe( "PATCH" );
177177
expect( headers ).toBe( {
178178
"Accept" : "application/xml",
179-
"Content-Type" : "application/json"
179+
"Content-Type" : "application/json",
180+
"User-Agent" : "HyperCFML/#req.getHyperVersion()#"
180181
} );
181182
} );
182183

@@ -232,8 +233,12 @@ component extends="testbox.system.BaseSpec" {
232233
expect( req.prepareBody() ).toBe( '{"query":{},"size":0,"from":0}' );
233234
} );
234235

235-
it( "defaults to no Content-Type or Headers", function() {
236-
expect( req.getHeaders() ).toBeEmpty();
236+
it( "defaults to no Content-Type", function() {
237+
expect( req.getHeader( "Content-Type" ) ).toBeEmpty();
238+
} );
239+
240+
it( "defaults to the Hyper User-Agent", () => {
241+
expect( req.getHeader( "User-Agent" ) ).toBe( "HyperCFML/#req.getHyperVersion()#" );
237242
} );
238243
} );
239244
}

0 commit comments

Comments
 (0)