Skip to content

Commit f58d899

Browse files
author
Eran Hammer
committed
Expose request object in inject(). Closes hapijs#2335
1 parent 438623d commit f58d899

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

API.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,11 +1023,13 @@ for performing injections, with some additional options and response properties:
10231023
- `payload` - the response payload string.
10241024
- `rawPayload` - the raw response payload buffer.
10251025
- `raw` - an object with the injection request and response objects:
1026-
- `req` - the [request object](#request-object).
1027-
- `res` - the response object.
1028-
- `result` - the raw handler response (e.g. when not a stream) before it is serialized for
1029-
transmission. If not available, set to `payload`. Useful for inspection and reuse of the
1030-
internal objects returned (instead of parsing the response string).
1026+
- `req` - the simulated node request object.
1027+
- `res` - the simulated node response object.
1028+
- `result` - the raw handler response (e.g. when not a stream or a view) before it is
1029+
serialized for transmission. If not available, the value is set to `payload`. Useful for
1030+
inspection and reuse of the internal objects returned (instead of parsing the response
1031+
string).
1032+
- `request` - the [request object](#request-object).
10311033

10321034
When the server contains more than one connection, each [`server.connections`](#serverconnections)
10331035
array member provides its own `connection.inject()`.

lib/connection.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,11 @@ internals.Connection.prototype.inject = function (options, callback) {
239239

240240
if (res.raw.res._hapi) {
241241
res.result = res.raw.res._hapi.result;
242+
res.request = res.raw.res._hapi.request;
242243
delete res.raw.res._hapi;
243244
}
244-
else {
245+
246+
if (res.result === undefined) {
245247
res.result = res.payload;
246248
}
247249

lib/transmit.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,14 @@ internals.transmit = function (response, callback) {
316316

317317
// Injection
318318

319-
if (response.variety === 'plain' &&
320-
Shot.isInjection(request.raw.req)) {
319+
if (Shot.isInjection(request.raw.req)) {
320+
request.raw.res._hapi = {
321+
request: request
322+
};
321323

322-
request.raw.res._hapi = { result: response._isPayloadSupported() ? response.source : null };
324+
if (response.variety === 'plain') {
325+
request.raw.res._hapi.result = response._isPayloadSupported() ? response.source : null;
326+
}
323327
}
324328
};
325329

test/connection.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,26 @@ describe('Connection', function () {
601601
done();
602602
});
603603
});
604+
605+
it('returns the request object', function (done) {
606+
607+
var handler = function (request, reply) {
608+
609+
request.app.key = 'value';
610+
return reply();
611+
};
612+
613+
var server = new Hapi.Server();
614+
server.connection();
615+
server.route({ method: 'GET', path: '/', config: { handler: handler } });
616+
617+
server.inject('/', function (res) {
618+
619+
expect(res.statusCode).to.equal(200);
620+
expect(res.request.app.key).to.equal('value');
621+
done();
622+
});
623+
});
604624
});
605625

606626
describe('table()', function () {

0 commit comments

Comments
 (0)