-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test/unit: match real querystring processing (#1243)
odk-central-backend uses express's default querystring parser (qs), whereas node-mocks-http uses querystring. See: eugef/node-mocks-http#299
- Loading branch information
Showing
4 changed files
with
44 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Safe wrapper for node-mocks-http - ensure that query string parsing matches | ||
// what is happening in production. N.B. if express's `query parser` option is | ||
// set, this wrapper will need to change too. | ||
// | ||
// See: https://expressjs.com/en/api.html#app.settings.table | ||
// See: https://github.com/eugef/node-mocks-http/issues/299 | ||
|
||
const wrapped = require('node-mocks-http'); | ||
|
||
const qs = (() => { | ||
try { | ||
// In case express has its own version of qs, try loading that first: | ||
return require('../../node_modules/express/node_modules/qs'); // eslint-disable-line import/extensions,import/no-unresolved | ||
} catch (err) { | ||
// Try loading the global qs. This is not written as `require('qs')` to avoid loading of the qs module from a surprising place. | ||
try { | ||
return require('../../node_modules/qs'); // eslint-disable-line import/extensions,import/no-unresolved | ||
} catch (err) { // eslint-disable-line no-shadow | ||
// node_modules layout may change in future (e.g. using yarn with different nodeLinker config) | ||
throw new Error('Unexpected missing module: qs. Please confirm node_modules directory is initialised, and dependency resolution has not changed recently.'); | ||
} | ||
} | ||
})(); | ||
|
||
const createRequest = options => { | ||
if (!options?.url) return wrapped.createRequest(options); | ||
|
||
const { search } = new URL(options.url, 'http://example.test'); | ||
const { query } = options; | ||
|
||
if (!search) return wrapped.createRequest(options); | ||
|
||
if (query != null) throw new Error('Unsupported: .query option and query string in .url simultaneously.'); | ||
|
||
return wrapped.createRequest({ ...options, query: qs.parse(search.substr(1)) }); | ||
}; | ||
|
||
module.exports = { | ||
createRequest, | ||
createResponse: wrapped.createResponse, | ||
}; |