Commit 0a9aef5e authored by Thomas Urban's avatar Thomas Urban

Merge branch 'master' into develop

parents a6a7059b 8a69c791
Pipeline #2199 passed with stage
in 22 seconds
......@@ -245,6 +245,7 @@ See the following table for a list of probable sources:
| `path` | leading part of request URL up to first `?` |
| `query.*` | query parameter extracted from part of request URL following first contained `?` - Replace `*` with name of parameter to read. |
| `params.*` | parameter extracted from pathname of request URL (in opposition to parameters found in its query) - Works after using convenient path test as described below, only. Replace `*` with name of parameter to read |
| `body.*` | parameter extracted from transmitted body - Works if body is encoded as JSON and header "content-type" is set. Replace `*` with name of parameter to read. |
#### Conveniently Test Path
......
......@@ -82,8 +82,44 @@ class HttpService extends AbstractService {
req.query = {};
}
// try all the rules looking for the one matching current request
this.process( req, res, () => Promise.reject( new Error( "no defined rule is matching request" ) ) )
let readJSON;
if (
[ "GET", "HEAD", "OPTIONS" ].indexOf( req.method ) === -1
&& /^(application|text)\/json/.test( req.headers["content-type"] )
) {
readJSON = () => new Promise( ( resolve2, reject2 ) => {
let rawData = "";
req.setEncoding( "utf8" );
req.on( "data", chunk => {
rawData += chunk;
} );
req.on( "end", () => {
if ( rawData ) {
try {
req.body = JSON.parse( rawData );
} catch( error ) {
reject2( error );
return;
}
} else {
req.body = {};
}
resolve2();
} );
req.once( "error", reject2 );
} );
} else {
req.body = {};
readJSON = () => Promise.resolve();
}
readJSON()
// try all the rules looking for the one matching current request
.then( () => this.process(
req,
res,
() => Promise.reject( new Error( "no defined rule is matching request" ) )
) )
.catch( error => new Promise( ( _resolve, _reject ) => {
res.once( "error", _reject );
res.once( "end", _resolve );
......
......@@ -102,6 +102,14 @@ EOT
Hello {{query.type}} message-tester!
EOM
method=PATCH AND /query/with/json/body AND body.firstName=Jane AND body.lastName=Doe ==>
Hello dissatisfied JSON-body message-tester!
EOT
method=PATCH AND /query/with/json/body AND body.firstName=John AND body.lastName=Doe ==>
Hello satisfied JSON-body message-tester!
EOT
` );
describe( "Mock-up service for HTTP", () => {
......@@ -125,7 +133,7 @@ describe( "Mock-up service for HTTP", () => {
} );
it( "has discovered proper number of rules", () => {
Mock.rules.should.have.length( 16 );
Mock.rules.should.have.length( 18 );
} );
it( "responds to query as defined", () => {
......@@ -282,4 +290,17 @@ describe( "Mock-up service for HTTP", () => {
response.body.toString( "utf-8" ).should.equal( "Hello actual message-tester!" );
} );
} );
it( "exposes JSON-data of transferred body", () => {
return Mock.query(
"PATCH",
"/query/with/json/body",
{ "content-type": "application/json" },
JSON.stringify( { firstName: "John", lastName: "Doe" } )
)
.then( response => {
response.body.should.be.instanceOf( Buffer );
response.body.toString( "utf-8" ).should.equal( "Hello satisfied JSON-body message-tester!" );
} );
} );
} );
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment