Skip to content

Commit 7410b13

Browse files
committed
Partial implementation of qs options. For hapijs#2623, hapijs#2622
1 parent 94dfd29 commit 7410b13

File tree

8 files changed

+77
-10
lines changed

8 files changed

+77
-10
lines changed

API.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 8.5.x API Reference
1+
# 8.8.x API Reference
22

33
- [Server](#server)
44
- [`new Server([options])`](#new-serveroptions)
@@ -148,6 +148,10 @@ Creates a new `Server` object where:
148148
which is used to store configuration values and `connection.plugins` which is meant for
149149
storing run-time state.
150150

151+
- `query` - incoming request query component parsing options:
152+
- `qs` - optional URI parsing [options](https://www.npmjs.com/package/qs#parsing-objects).
153+
Defaults to the **qs** module parsing defaults.
154+
151155
- <a name="connection.config.router"></a>`router` - controls how incoming request URIs are
152156
matched against the routing table:
153157
- `isCaseSensitive` - determines whether the paths '/example' and '/EXAMPLE' are
@@ -2730,7 +2734,8 @@ Changes the request URI before the router begins processing the request where:
27302734
- `url` - the new request path value.
27312735
- `stripTrailingSlash` - if `true`, strip the trailing slash from the path. Defaults to `false`.
27322736
- `parserOptions` - optional URI parsing [options](https://www.npmjs.com/package/qs#parsing-objects).
2733-
Defaults to the **qs** module parsing defaults.
2737+
Defaults to the route connection `query.qs` settings if present, otherwise the **qs** module
2738+
parsing defaults.
27342739

27352740
```js
27362741
var Hapi = require('hapi');

lib/connection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ exports = module.exports = internals.Connection = function (server, options) {
5454
Hoek.assert(this.settings.autoListen || !this.settings.port, 'Cannot specify port when autoListen is false');
5555
Hoek.assert(this.settings.autoListen || !this.settings.address, 'Cannot specify address when autoListen is false');
5656

57+
this.settings.query = this.settings.query || {};
58+
5759
// Connection facilities
5860

5961
this._started = false;

lib/request.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,9 @@ Hoek.inherits(internals.Request, Events.EventEmitter);
206206
internals.Request.prototype._setUrl = function (url, stripTrailingSlash, parserOptions) {
207207

208208
this.url = Url.parse(url, false);
209-
this.url.query = Qs.parse(this.url.query, parserOptions); // Override parsed value
209+
this.url.query = Qs.parse(this.url.query, this.connection.settings.query.qs || parserOptions); // Override parsed value
210210
this.query = this.url.query;
211-
this.path = this.url.pathname || ''; // pathname excludes query
211+
this.path = this.url.pathname || ''; // pathname excludes query
212212

213213
if (stripTrailingSlash &&
214214
this.path.length > 1 &&

lib/schema.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ internals.routeBase = Joi.object({
9595
maxBytes: Joi.number(),
9696
uploads: Joi.string(),
9797
failAction: Joi.string().valid('error', 'log', 'ignore'),
98-
timeout: Joi.number().integer().positive().allow(false)
98+
timeout: Joi.number().integer().positive().allow(false),
99+
qs: Joi.object()
99100
}),
100101
plugins: Joi.object(),
101102
response: Joi.object({
@@ -156,6 +157,9 @@ internals.connectionBase = Joi.object({
156157
app: Joi.object().allow(null),
157158
load: Joi.object(),
158159
plugins: Joi.object(),
160+
query: Joi.object({
161+
qs: Joi.object()
162+
}),
159163
router: Joi.object({
160164
isCaseSensitive: Joi.boolean(),
161165
stripTrailingSlash: Joi.boolean()

npm-shrinkwrap.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "hapi",
33
"description": "HTTP Server framework",
44
"homepage": "http://hapijs.com",
5-
"version": "8.7.0",
5+
"version": "8.8.0",
66
"repository": {
77
"type": "git",
88
"url": "git://github.com/hapijs/hapi"
@@ -35,7 +35,7 @@
3535
"kilt": "^1.1.x",
3636
"mimos": "2.x.x",
3737
"peekaboo": "1.x.x",
38-
"qs": "2.x.x",
38+
"qs": "3.x.x",
3939
"shot": "1.x.x",
4040
"statehood": "2.x.x",
4141
"subtext": "1.x.x",

test/request.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,35 @@ describe('Request', function () {
836836
done();
837837
});
838838
});
839+
840+
it('overrides qs settings', function (done) {
841+
842+
var server = new Hapi.Server();
843+
server.connection({
844+
query: {
845+
qs: {
846+
parseArrays: false
847+
}
848+
}
849+
});
850+
851+
server.route({
852+
method: 'GET',
853+
path: '/',
854+
config: {
855+
handler: function (request, reply) {
856+
857+
return reply(request.query);
858+
}
859+
}
860+
});
861+
862+
server.inject('/?a[0]=b&a[1]=c', function (res) {
863+
864+
expect(res.result).to.equal({ a: { 0: 'b', 1: 'c' } });
865+
done();
866+
});
867+
});
839868
});
840869

841870
describe('log()', { parallel: false }, function () {

test/route.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,31 @@ describe('Route', function () {
371371
}).to.not.throw();
372372
done();
373373
});
374+
375+
it('overrides qs settings', function (done) {
376+
377+
var server = new Hapi.Server();
378+
server.connection();
379+
server.route({
380+
method: 'POST',
381+
path: '/',
382+
config: {
383+
payload: {
384+
qs: {
385+
parseArrays: false
386+
}
387+
},
388+
handler: function (request, reply) {
389+
390+
return reply(request.payload);
391+
}
392+
}
393+
});
394+
395+
server.inject({ method: 'POST', url: '/', payload: 'a[0]=b&a[1]=c', headers: { 'content-type': 'application/x-www-form-urlencoded' } }, function (res) {
396+
397+
expect(res.result).to.deep.equal({ a: { 0: 'b', 1: 'c' } });
398+
done();
399+
});
400+
});
374401
});

0 commit comments

Comments
 (0)