Browse Source

mock response routes no longer pay attention to query strings

master
Lloyd Brookes 9 years ago
parent
commit
0c78138fd9
  1. 8
      README.md
  2. 4
      example/mock/.local-web-server.json
  3. 4
      example/mock/mocks/five.js
  4. 8
      jsdoc2md/README.hbs
  5. 8
      lib/middleware.js

8
README.md

@ -258,21 +258,21 @@ If the route contains tokens, their values are passed to the response. For examp
{
"mocks": [
{
"route": "/five/:id\\?name=:name",
"route": "/five/:id",
"module": "/mocks/example.js"
}
]
}
```
...the values `id` and `name` are passed to the body function. For example, a path of `/five/10?name=Lionel` would pass `10` and `Lionel` to the body function:
...the `id` value is passed to the body function. For example, a path of `/five/10?name=Lionel` would pass `10` to the body function. Additional, the value `Lionel` would be available on `ctx.query.name`:
```js
const fs = require('fs')
module.exports = {
response: {
body: function (ctx, id, name) {
ctx.body = `<h1>id: ${id}, name: ${name}</h1>`
body: function (ctx, id) {
ctx.body = `<h1>id: ${id}, name: ${ctx.query.name}</h1>`
}
}
}

4
example/mock/.local-web-server.json

@ -43,11 +43,11 @@
"module": "/mocks/stream-self.js"
},
{
"route": "/five/:id\\?name=:name",
"route": "/five/:id",
"module": "/mocks/five.js"
},
{
"route": "/users*",
"route": "/users",
"module": "/mocks/users.js"
}
]

4
example/mock/mocks/five.js

@ -1,5 +1,5 @@
module.exports = {
response: function (ctx, id, name) {
ctx.body = `<h1>id: ${id}, name: ${name}</h1>`
response: function (ctx, id) {
ctx.body = `<h1>id: ${id}, name: ${ctx.query.name}</h1>`
}
}

8
jsdoc2md/README.hbs

@ -258,21 +258,21 @@ If the route contains tokens, their values are passed to the response. For examp
{
"mocks": [
{
"route": "/five/:id\\?name=:name",
"route": "/five/:id",
"module": "/mocks/example.js"
}
]
}
```
...the values `id` and `name` are passed to the body function. For example, a path of `/five/10?name=Lionel` would pass `10` and `Lionel` to the body function:
...the `id` value is passed to the body function. For example, a path of `/five/10?name=Lionel` would pass `10` to the body function. Additional, the value `Lionel` would be available on `ctx.query.name`:
```js
const fs = require('fs')
module.exports = {
response: {
body: function (ctx, id, name) {
ctx.body = `<h1>id: ${id}, name: ${name}</h1>`
body: function (ctx, id) {
ctx.body = `<h1>id: ${id}, name: ${ctx.query.name}</h1>`
}
}
}

8
lib/middleware.js

@ -78,11 +78,11 @@ function mime (mimeTypes) {
function mockResponses (route, targets) {
targets = arrayify(targets)
debug('mock route: %s, targets: %j', route, targets);
debug('mock route: %s, targets: %s', route, targets.length);
const pathRe = pathToRegexp(route)
return function mockResponse (ctx, next) {
if (pathRe.test(ctx.url)) {
if (pathRe.test(ctx.path)) {
const testValue = require('test-value')
/* find a mock with compatible method and accepts */
@ -101,9 +101,9 @@ function mockResponses (route, targets) {
}
if (target) {
debug('target response: %s', target.response)
// debug('target response: %s', target.response)
if (t.isFunction(target.response)) {
const pathMatches = ctx.url.match(pathRe).slice(1)
const pathMatches = ctx.path.match(pathRe).slice(1)
target.response.apply(null, [ctx].concat(pathMatches))
} else if (t.isPlainObject(target.response)) {
Object.assign(ctx.response, target.response)

Loading…
Cancel
Save