Skip to content

Commit

Permalink
Add docs and tests for passing options to the querystring's
Browse files Browse the repository at this point in the history
parse and stringify methods
  • Loading branch information
simov committed May 28, 2015
1 parent 82b35f9 commit 5cdcbe5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
7 changes: 3 additions & 4 deletions README.md
Expand Up @@ -199,8 +199,7 @@ For advanced cases, you can access the form-data object itself via `r.form()`. T

```js
// NOTE: Advanced use-case, for normal use see 'formData' usage above
var r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) { // ...

var r = request.post('http://service.com/upload', function optionalCallback(err, httpResponse, body) {...})
var form = r.form();
form.append('my_field', 'my_value');
form.append('my_buffer', new Buffer([1, 2, 3]));
Expand Down Expand Up @@ -723,8 +722,8 @@ The first argument can be either a `url` or an `options` object. The only requir
---

- `qs` - object containing querystring values to be appended to the `uri`
- `qsParseOptions` - object containing options to pass to the [qs.parse](https://github.com/hapijs/qs#parsing-objects) method or [querystring.parse](https://nodejs.org/docs/v0.12.0/api/querystring.html#querystring_querystring_parse_str_sep_eq_options) method
- `qsStringifyOptions` - object containing options to pass to the [qs.stringify](https://github.com/hapijs/qs#stringifying) method or to the [querystring.stringify](https://nodejs.org/docs/v0.12.0/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options) method. For example, to change the way arrays are converted to query strings pass the `arrayFormat` option with one of `indices|brackets|repeat`
- `qsParseOptions` - object containing options to pass to the [qs.parse](https://github.com/hapijs/qs#parsing-objects) method. Alternatively pass options to the [querystring.parse](https://nodejs.org/docs/v0.12.0/api/querystring.html#querystring_querystring_parse_str_sep_eq_options) method using this format `{sep:';', eq:':', options:{}}`
- `qsStringifyOptions` - object containing options to pass to the [qs.stringify](https://github.com/hapijs/qs#stringifying) method. Alternatively pass options to the [querystring.stringify](https://nodejs.org/docs/v0.12.0/api/querystring.html#querystring_querystring_stringify_obj_sep_eq_options) method using this format `{sep:';', eq:':', options:{}}`. For example, to change the way arrays are converted to query strings using the `qs` module pass the `arrayFormat` option with one of `indices|brackets|repeat`
- `useQuerystring` - If true, use `querystring` to stringify and parse
querystrings, otherwise use `qs` (default: `false`). Set this option to
`true` if you need arrays to be serialized as `foo=bar&foo=baz` instead of the
Expand Down
42 changes: 27 additions & 15 deletions tests/test-qs.js
Expand Up @@ -13,23 +13,18 @@ var request = require('../index')
// - expectedQuerystring : expected path when using the querystring library
function runTest(name, options) {
var uri = 'http://www.google.com' + (options.suffix || '')
, requestOptsQs = {
uri : uri,
qsParseOptions: options.qsParseOptions,
qsStringifyOptions: options.qsStringifyOptions
}
, requestOptsQuerystring = {
uri : uri,
useQuerystring : true
}
var opts = {
uri : uri,
qsParseOptions: options.qsParseOptions,
qsStringifyOptions: options.qsStringifyOptions
}

if (options.qs) {
requestOptsQs.qs = options.qs
requestOptsQuerystring.qs = options.qs
opts.qs = options.qs
}

tape(name + ' using qs', function(t) {
var r = request.get(requestOptsQs)
tape(name + ' - using qs', function(t) {
var r = request.get(opts)
if (typeof options.afterRequest === 'function') {
options.afterRequest(r)
}
Expand All @@ -40,8 +35,9 @@ function runTest(name, options) {
})
})

tape(name + ' using querystring', function(t) {
var r = request.get(requestOptsQuerystring)
tape(name + ' - using querystring', function(t) {
opts.useQuerystring = true
var r = request.get(opts)
if (typeof options.afterRequest === 'function') {
options.afterRequest(r)
}
Expand Down Expand Up @@ -121,3 +117,19 @@ runTest('pass options to the qs module via the qsStringifyOptions key', {
expected : esc('/?order[]=bar&order[]=desc'),
expectedQuerystring : '/?order=bar&order=desc'
})

runTest('pass options to the querystring module via the qsParseOptions key', {
suffix : '?a=1;b=2',
qs: {},
qsParseOptions: { sep : ';' },
qsStringifyOptions: { sep : ';' },
expected : esc('/?a=1%3Bb%3D2'),
expectedQuerystring : '/?a=1;b=2'
})

runTest('pass options to the querystring module via the qsStringifyOptions key', {
qs : { order : ['bar', 'desc'] },
qsStringifyOptions: { sep : ';' },
expected : esc('/?order[0]=bar&order[1]=desc'),
expectedQuerystring : '/?order=bar;order=desc'
})

0 comments on commit 5cdcbe5

Please sign in to comment.