Skip to content

Commit

Permalink
fix(HyperRequest): Handle when body is a string and format is JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
aliaspooryorik committed Dec 12, 2022
1 parent 7741cb1 commit 3e9e4f9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions models/HyperRequest.cfc
Expand Up @@ -1071,6 +1071,9 @@ component accessors="true" {
* @returns A simple value representing the body.
*/
public any function prepareBody() {
if ( isSimpleValue( getBody() ) ) {
return getBody();
}
return getBodyFormat() == "json" ? serializeJSON( getBody() ) : getBody();
}

Expand Down
11 changes: 11 additions & 0 deletions tests/specs/unit/HyperRequestSpec.cfc
Expand Up @@ -209,6 +209,17 @@ component extends="testbox.system.BaseSpec" {
expect( req.hasHeader( "X-Forwarded-For" ) ).toBeTrue();
expect( req.getHeader( "X-Forwarded-For" ) ).toBe( "1.1.1.1" );
} );

it( "can handle a JSON body format with a body as a string", function() {
req.setBodyFormat( "json" );
req.setBody( '{"query":{},"size":0,"from":0}' );
expect( req.prepareBody() ).toBe( '{"query":{},"size":0,"from":0}' );
} );
it( "can handle a JSON body format with a body as an struct", function() {
req.setBodyFormat( "json" );
req.setBody( { "query" : {}, "size" : 0, "from" : 0 } );
expect( req.prepareBody() ).toBe( '{"query":{},"size":0,"from":0}' );
} );
} );
}

Expand Down

0 comments on commit 3e9e4f9

Please sign in to comment.