From 0c78138fd989c38c73ffb55019641baabf3e9397 Mon Sep 17 00:00:00 2001 From: Lloyd Brookes Date: Fri, 27 Nov 2015 22:48:53 +0000 Subject: [PATCH] mock response routes no longer pay attention to query strings --- README.md | 8 ++++---- example/mock/.local-web-server.json | 4 ++-- example/mock/mocks/five.js | 4 ++-- jsdoc2md/README.hbs | 8 ++++---- lib/middleware.js | 8 ++++---- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c7a889e..1ceaa6e 100644 --- a/README.md +++ b/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 = `

id: ${id}, name: ${name}

` + body: function (ctx, id) { + ctx.body = `

id: ${id}, name: ${ctx.query.name}

` } } } diff --git a/example/mock/.local-web-server.json b/example/mock/.local-web-server.json index 1865ed2..fe66001 100644 --- a/example/mock/.local-web-server.json +++ b/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" } ] diff --git a/example/mock/mocks/five.js b/example/mock/mocks/five.js index 9138cc0..091186b 100644 --- a/example/mock/mocks/five.js +++ b/example/mock/mocks/five.js @@ -1,5 +1,5 @@ module.exports = { - response: function (ctx, id, name) { - ctx.body = `

id: ${id}, name: ${name}

` + response: function (ctx, id) { + ctx.body = `

id: ${id}, name: ${ctx.query.name}

` } } diff --git a/jsdoc2md/README.hbs b/jsdoc2md/README.hbs index 2e21209..1d09b00 100644 --- a/jsdoc2md/README.hbs +++ b/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 = `

id: ${id}, name: ${name}

` + body: function (ctx, id) { + ctx.body = `

id: ${id}, name: ${ctx.query.name}

` } } } diff --git a/lib/middleware.js b/lib/middleware.js index c4b88be..9b16912 100644 --- a/lib/middleware.js +++ b/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)