Skip to content

Commit c1f6b33

Browse files
committed
Handle redirects and store the request on the response
1 parent ccfd213 commit c1f6b33

11 files changed

Lines changed: 244 additions & 82 deletions

File tree

‎models/HyperRequest.cfc‎

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,79 @@
11
component accessors="true" {
22

3-
property name="url";
4-
property name="method";
3+
property name="url" default="";
4+
property name="method" default="GET";
5+
property name="maximumRedirects" default="*";
6+
property name="referrer";
57

6-
function get() {
8+
function get( url ) {
9+
if ( ! isNull( arguments.url ) ) {
10+
setUrl( arguments.url );
11+
}
712
setMethod( "GET" );
813
return makeRequest();
914
}
1015

16+
function setProperties( properties = {} ) {
17+
properties.each( function( key, value ) {
18+
invoke( this, "set#key#", { 1 = value } );
19+
} );
20+
return this;
21+
}
22+
23+
function setFollowRedirects( shouldFollow ) {
24+
if ( shouldFollow ) {
25+
setMaximumRedirects( "*" );
26+
}
27+
else {
28+
setMaximumRedirects( 0 );
29+
}
30+
return this;
31+
}
32+
1133
function makeRequest() {
12-
var res = new Hyper.models.HyperResponse();
13-
return res.populateFromCFHTTP(
14-
makeCFHTTPRequest()
15-
);
34+
if ( getUrl() == "" ) {
35+
throw( type = "NoUrlException" );
36+
}
37+
38+
var res = new Hyper.models.HyperResponse( this );
39+
res.populateFromCFHTTP( makeCFHTTPRequest() );
40+
41+
if ( res.isRedirect() ) {
42+
if ( shouldFollowRedirect() ) {
43+
return followRedirect( res );
44+
}
45+
}
46+
47+
return res;
48+
}
49+
50+
private function shouldFollowRedirect() {
51+
return getMaximumRedirects() == "*" ||
52+
getMaximumRedirects() > 0;
53+
}
54+
55+
private function followRedirect( res ) {
56+
var redirectReq = new Hyper.models.HyperRequest();
57+
redirectReq.setReferrer( res );
58+
redirectReq.setUrl( res.getHeader( "Location" ) );
59+
redirectReq.setMaximumRedirects( decrementRedirects() );
60+
return redirectReq.makeRequest();
61+
}
62+
63+
private function decrementRedirects() {
64+
if ( getMaximumRedirects() == "*" ) {
65+
return getMaximumRedirects();
66+
}
67+
return getMaximumRedirects() - 1;
1668
}
1769

1870
private function makeCFHTTPRequest() {
1971
local.res = "";
2072
cfhttp(
2173
result = "local.res"
2274
url = getUrl(),
23-
method = getMethod()
75+
method = getMethod(),
76+
redirect = false
2477
) {
2578

2679
}

‎models/HyperResponse.cfc‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
component accessors="true" {
1+
component accessors="true" setters="false" {
22

3-
property name="statusCode";
4-
property name="data";
3+
property name="statusCode" default="200";
4+
property name="data" default="";
5+
property name="request";
6+
7+
function init( req ) {
8+
variables.request = req;
9+
return this;
10+
}
511

612
function json() {
713
if ( ! isJSON( getData() ) ) {
@@ -11,10 +17,21 @@ component accessors="true" {
1117
}
1218

1319
function populateFromCFHTTP( res ) {
14-
setStatusCode( res.responseheader.status_code );
15-
setData( res.filecontent );
20+
variables.statusCode = res.responseheader.status_code;
21+
res.responseheader.each( function( key, value ) {
22+
variables.headers[ key ] = value;
23+
} );
24+
variables.data = res.filecontent;
1625

1726
return this;
1827
}
1928

29+
function getHeader( name ) {
30+
return variables.headers[ name ];
31+
}
32+
33+
function isRedirect() {
34+
return left( getStatusCode(), 1 ) == "3";
35+
}
36+
2037
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
component {
2+
3+
function index( event, rc, prc ) {
4+
return event.renderData(
5+
type = "json",
6+
data = {
7+
"message" = "Hello, world!"
8+
}
9+
);
10+
}
11+
12+
function redirect( event, rc, prc ) {
13+
param rc.times = 0;
14+
if ( rc.times == 0 ) {
15+
setNextEvent( "api.index" );
16+
}
17+
else {
18+
setNextEvent( event = "api.redirect", queryString = "times=#rc.times-1#" );
19+
}
20+
21+
}
22+
23+
}

‎tests/resources/app/views/api/index.cfm‎

Whitespace-only changes.

‎tests/specs/integration/BuilderPassThroughSpec.cfc‎

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
33
function run() {
44
describe( "Builder pass through requests", function() {
55
beforeEach( function() {
6-
variables.hyper = getInstance( "Builder@Hyper" );
6+
variables.hyper = getInstance( "HyperBuilder@Hyper" );
77
} );
88

99
afterEach( function() {
@@ -13,7 +13,7 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
1313
} );
1414

1515
it( "passes through all other methods to the HyperRequest", function() {
16-
var res = hyper.setUrl( "https://swapi.co/api/people/1" ).get();
16+
var res = hyper.setUrl( "https://jsonplaceholder.typicode.com/posts/1" ).get();
1717
expect( res ).toBeInstanceOf(
1818
"HyperResponse",
1919
"A HyperResponse object should have been returned."
@@ -22,38 +22,12 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
2222
expect( data ).toBeStruct(
2323
"Expected to deserialize JSON data from the response."
2424
);
25-
expect( data ).toBe( {
26-
"name": "Luke Skywalker",
27-
"height": "172",
28-
"mass": "77",
29-
"hair_color": "blond",
30-
"skin_color": "fair",
31-
"eye_color": "blue",
32-
"birth_year": "19BBY",
33-
"gender": "male",
34-
"homeworld": "https://swapi.co/api/planets/1/",
35-
"films": [
36-
"https://swapi.co/api/films/2/",
37-
"https://swapi.co/api/films/6/",
38-
"https://swapi.co/api/films/3/",
39-
"https://swapi.co/api/films/1/",
40-
"https://swapi.co/api/films/7/"
41-
],
42-
"species": [
43-
"https://swapi.co/api/species/1/"
44-
],
45-
"vehicles": [
46-
"https://swapi.co/api/vehicles/14/",
47-
"https://swapi.co/api/vehicles/30/"
48-
],
49-
"starships": [
50-
"https://swapi.co/api/starships/12/",
51-
"https://swapi.co/api/starships/22/"
52-
],
53-
"created": "2014-12-09T13:50:51.644000Z",
54-
"edited": "2014-12-20T21:17:56.891000Z",
55-
"url": "https://swapi.co/api/people/1/"
56-
} );
25+
expect( data ).toBe( deserializeJSON( '{
26+
"userId": 1,
27+
"id": 1,
28+
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
29+
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
30+
}' ) );
5731
} );
5832
} );
5933
}

‎tests/specs/integration/GetSpec.cfc‎

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
33
function run() {
44
describe( "GET requests", function() {
55
beforeEach( function() {
6-
variables.hyper = getInstance( "Builder@Hyper" );
6+
variables.hyper = getInstance( "HyperBuilder@Hyper" );
77
} );
88

99
afterEach( function() {
@@ -13,7 +13,7 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
1313
} );
1414

1515
it( "can make a GET request", function() {
16-
var res = hyper.get( "https://swapi.co/api/people/1" );
16+
var res = hyper.get( "https://jsonplaceholder.typicode.com/posts/1" );
1717
expect( res ).toBeInstanceOf(
1818
"HyperResponse",
1919
"A HyperResponse object should have been returned."
@@ -22,38 +22,23 @@ component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
2222
expect( data ).toBeStruct(
2323
"Expected to deserialize JSON data from the response."
2424
);
25-
expect( data ).toBe( {
26-
"name": "Luke Skywalker",
27-
"height": "172",
28-
"mass": "77",
29-
"hair_color": "blond",
30-
"skin_color": "fair",
31-
"eye_color": "blue",
32-
"birth_year": "19BBY",
33-
"gender": "male",
34-
"homeworld": "https://swapi.co/api/planets/1/",
35-
"films": [
36-
"https://swapi.co/api/films/2/",
37-
"https://swapi.co/api/films/6/",
38-
"https://swapi.co/api/films/3/",
39-
"https://swapi.co/api/films/1/",
40-
"https://swapi.co/api/films/7/"
41-
],
42-
"species": [
43-
"https://swapi.co/api/species/1/"
44-
],
45-
"vehicles": [
46-
"https://swapi.co/api/vehicles/14/",
47-
"https://swapi.co/api/vehicles/30/"
48-
],
49-
"starships": [
50-
"https://swapi.co/api/starships/12/",
51-
"https://swapi.co/api/starships/22/"
52-
],
53-
"created": "2014-12-09T13:50:51.644000Z",
54-
"edited": "2014-12-20T21:17:56.891000Z",
55-
"url": "https://swapi.co/api/people/1/"
56-
} );
25+
expect( data ).toBe( deserializeJSON( '{
26+
"userId": 1,
27+
"id": 1,
28+
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
29+
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
30+
}' ) );
31+
} );
32+
33+
it( "has access to the original HyperRequest in the HyperResponse", function() {
34+
var res = hyper.get( "https://jsonplaceholder.typicode.com/posts/1" );
35+
expect( res ).toBeInstanceOf(
36+
"HyperResponse",
37+
"A HyperResponse object should have been returned."
38+
);
39+
var req = res.getRequest();
40+
expect( req ).toBeInstanceOf( "Hyper.models.HyperRequest" );
41+
expect( req.getUrl() ).toBe( "https://jsonplaceholder.typicode.com/posts/1" );
5742
} );
5843
} );
5944
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
component extends="tests.resources.ModuleIntegrationSpec" appMapping="/app" {
2+
3+
variables.localEndpoint = "http://#CGI[ "server_name" ]#:#CGI[ "server_port" ]#/tests/resources/app/index.cfm/api";
4+
5+
function run() {
6+
describe( "GET requests", function() {
7+
beforeEach( function() {
8+
variables.hyper = getInstance( "HyperBuilder@Hyper" );
9+
} );
10+
11+
afterEach( function() {
12+
if ( variables.keyExists( "hyper" ) ) {
13+
variables.delete( "hyper" );
14+
}
15+
} );
16+
17+
it( "follows redirects by default", function() {
18+
var res = hyper.get( localEndpoint & "/redirect" );
19+
expect( res.getStatusCode() ).toBe( 200 );
20+
expect( res.json() ).toBe( { "message" = "Hello, world!" } );
21+
var referrer = res.getRequest().getReferrer();
22+
expect( referrer ).notToBeNull();
23+
expect( referrer.getStatusCode() ).toBe( 302 );
24+
expect( referrer.getRequest().getUrl() ).toBe( localEndpoint & "/redirect" );
25+
} );
26+
27+
it( "can set the maximum number of redirects to follow", function() {
28+
var res = hyper
29+
.setMaximumRedirects( 2 )
30+
.get( localEndpoint & "/redirect?times=3" );
31+
expect( res.getStatusCode() ).toBe( 302 );
32+
} );
33+
34+
it( "can turn off following redirects by setting max redirects to zero", function() {
35+
var res = hyper
36+
.setMaximumRedirects( 0 )
37+
.get( localEndpoint & "/redirect" );
38+
expect( res.getStatusCode() ).toBe( 302 );
39+
} );
40+
41+
it( "has a helper method to not follow redirects", function() {
42+
var res = hyper
43+
.setFollowRedirects( false )
44+
.get( localEndpoint & "/redirect" );
45+
expect( res.getStatusCode() ).toBe( 302 );
46+
} );
47+
} );
48+
}
49+
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
component extends="testbox.system.BaseSpec" {
2+
3+
function run() {
4+
describe( "HyperBuilder", function() {
5+
beforeEach( function() {
6+
variables.hyper = new Hyper.models.HyperBuilder();
7+
} );
8+
9+
afterEach( function() {
10+
if ( variables.keyExists( "hyper" ) ) {
11+
variables.delete( "hyper" );
12+
}
13+
} );
14+
15+
it( "passes through all other methods to the HyperRequest", function() {
16+
var req = hyper.setUrl( "https://jsonplaceholder.typicode.com/posts/1" );
17+
expect( req ).toBeInstanceOf(
18+
"Hyper.models.HyperRequest",
19+
"Expected a HyperRequest instance, since HyperBuilder does not have a `setUrl` method."
20+
);
21+
expect( req.getUrl() ).toBe( "https://jsonplaceholder.typicode.com/posts/1" );
22+
} );
23+
} );
24+
}
25+
26+
}

0 commit comments

Comments
 (0)