Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:framework-one/fw1 into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
seancorfield committed Jun 16, 2017
2 parents 7950cd1 + 4232884 commit 7e08197
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 1 deletion.
34 changes: 34 additions & 0 deletions examples/rest/Application.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
component {
this.name = 'fw1-examples-rest';
this.mappings[ '/framework' ] = expandPath( '../framework' );

function _get_framework_one() {
if ( !structKeyExists( request, '_framework_one' ) ) {
request._framework_one = new framework.one( {
decodeRequestBody = true,
reloadApplicationOnEveryRequest = true
} );
}
return request._framework_one;
}

// delegation of lifecycle methods to FW/1:
function onApplicationStart() {
return _get_framework_one().onApplicationStart();
}
function onError( exception, event ) {
return _get_framework_one().onError( exception, event );
}
function onRequest( targetPath ) {
return _get_framework_one().onRequest( targetPath );
}
function onRequestEnd() {
return _get_framework_one().onRequestEnd();
}
function onRequestStart( targetPath ) {
return _get_framework_one().onRequestStart( targetPath );
}
function onSessionStart() {
return _get_framework_one().onSessionStart();
}
}
35 changes: 35 additions & 0 deletions examples/rest/controllers/main.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
component {

function init( any fw ) {
variables.fw = fw;
return this;
}

function patch( struct rc, struct headers ) {
var response = {
"method": "PATCH",
"multi": rc.multi,
"single": rc.single
};
variables.fw.renderData().type( 'json' ).data( response );
}

function post( struct rc, struct headers ) {
var response = {
"method": "POST",
"multi": rc.multi,
"single": rc.single
};
variables.fw.renderData().type( 'json' ).data( response );
}

function put( struct rc, struct headers ) {
var response = {
"method": "PUT",
"multi": rc.multi,
"single": rc.single
};
variables.fw.renderData().type( 'json' ).data( response );
}

}
1 change: 1 addition & 0 deletions examples/rest/index.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!--- intentionally blank --->
1 change: 1 addition & 0 deletions examples/rest/views/main/default.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This rest example is used by the tests to assert that data is decoded as expected
8 changes: 7 additions & 1 deletion framework/one.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -2853,7 +2853,13 @@ component {
var paramPairs = listToArray( body, "&" );
for ( var pair in paramPairs ) {
var parts = listToArray( pair, "=", true ); // handle blank values
request.context[ parts[ 1 ] ] = urlDecode( parts[ 2 ] );
var keyName = parts[ 1 ];
var keyValue = urlDecode( parts[ 2 ] );
if ( !structKeyExists( request.context, keyName ) ) {
request.context[ keyName ] = keyValue;
} else {
request.context[ keyName ] = listAppend( request.context[ keyName ], keyValue );
}
}
} catch ( any e ) {
throw( type = "FW1.JSONPOST",
Expand Down
73 changes: 73 additions & 0 deletions tests/rest/DecodeTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
component extends="mxunit.framework.TestCase" {

function testPostFormEncodedRequestDecodesMultiField() {
var actual = doFormEncodedHTTPRequest( "POST" );
assertEquals( "POST", actual.method );
assertEquals( "a,b,c", actual.single );
assertEquals( "1,2,3,40,50", actual.multi );
}

function testPatchFormEncodedRequestDecodesMultiField() skip="engineNotSupported" {
var actual = doFormEncodedHTTPRequest( "PATCH" );
assertEquals( "PATCH", actual.method );
assertEquals( "a,b,c", actual.single );
assertEquals( "1,2,3,40,50", actual.multi );
}

function testPutFormEncodedRequestDecodesMultiField() {
var actual = doFormEncodedHTTPRequest( "PUT" );
assertEquals( "PUT", actual.method );
assertEquals( "a,b,c", actual.single );
assertEquals( "1,2,3,40,50", actual.multi );
}

function testPostJSONEncodedRequestDecodesMultiField() {
var actual = doJSONEncodedHTTPRequest( "POST" );
assertEquals( "POST", actual.method );
assertEquals( "a,b,c", actual.single );
assertEquals( "1,2,3,40,50", actual.multi );
}

function testPatchJSONRequestDecodesMultiField() skip="engineNotSupported" {
var actual = doJSONEncodedHTTPRequest( "PATCH" );
assertEquals( "PATCH", actual.method );
assertEquals( "a,b,c", actual.single );
assertEquals( "1,2,3,40,50", actual.multi );
}

function testPutJSONRequestDecodesMultiField() {
var actual = doJSONEncodedHTTPRequest( "PUT" );
assertEquals( "PUT", actual.method );
assertEquals( "a,b,c", actual.single );
assertEquals( "1,2,3,40,50", actual.multi );
}



private function doFormEncodedHTTPRequest( verb ) {
return doHTTPRequest( verb, "application/x-www-form-urlencoded", "multi=1%2C2%2C3&multi=40&multi=50&single=a%2Cb%2Cc" );
}

private function doJSONEncodedHTTPRequest( verb ) {
return doHTTPRequest( verb, "application/json", '{"multi": "1,2,3,40,50","single": "a,b,c"}' );
}

private function doHTTPRequest( verb, contentType, body ) {
var httpService = new http();
httpService.setmethod( verb );
httpService.setCharset( "utf-8" );
httpService.setUrl( "http://#CGI.SERVER_NAME#:#CGI.SERVER_PORT#/examples/rest/?action=main.#verb#" );
httpService.addParam( type = "header", name = "content-type", value = contentType );
httpService.addParam( type = "body", value = body );
var response = httpService.send().getPrefix().filecontent;
if ( isJson( response ) ) {
return deserializeJSON( response );
}
fail( "expected a JSON response for #verb# #contentType#" );
}

function engineNotSupported() {
return server.coldfusion.productname != "Lucee" && ListFirst( server.coldfusion.productversion ) == 10;
}

}

0 comments on commit 7e08197

Please sign in to comment.