Skip to content

Commit

Permalink
support multiple GET responses per file
Browse files Browse the repository at this point in the history
Since windows does not support special characters in the file names this
allows for all responses to a certian get request to be in one file and
distinquished like POST responses would be split by new lines and header
lines.

fixes #19
  • Loading branch information
sideshowcoder committed Nov 28, 2014
1 parent 60c9bef commit a640546
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
22 changes: 17 additions & 5 deletions canned.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var url = require('url')
var fs = require('fs')
var util = require('util')
var Response = require('./lib/response')
var qs = require('querystring')
var querystring = require('querystring')
var url = require('url')

function Canned(dir, options) {
this.logger = options.logger
Expand Down Expand Up @@ -292,16 +293,27 @@ Canned.prototype.responseFilter = function (req, res) {
var that = this
var body = ''

// assemble response body if POST/PUT
if(req.method === 'PUT' || req.method === 'POST') {
// assemble response body if GET/POST/PUT
switch(req.method) {
case 'PUT':
case 'POST':
req.on('data', function (data) {
body += data
})
req.on('end', function () {
that.responder(qs.parse(body), req, res)
that.responder(querystring.parse(body), req, res)
})
} else {
break;
case 'GET':
var query = url.parse(req.url).query
if (query && query.length > 0) {
body = querystring.parse(query)
}
that.responder(body, req, res)
break;
default:
that.responder(body, req, res)
break;
}
}

Expand Down
27 changes: 26 additions & 1 deletion spec/canned.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use strict";

var querystring = require("querystring")
var canned = require('../canned')

describe('canned', function () {
Expand Down Expand Up @@ -383,6 +383,31 @@ describe('canned', function () {
})
})

describe("variable GET responses based on params", function() {
var req, data
beforeEach(function() {
req = { method: 'GET' }
})

it("should select the right response based on the GET request data", function (done) {
req.url = "/multiple_get_responses?" + querystring.stringify({ foo: "bar" })
res.end = function (content) {
expect(content).toEqual(JSON.stringify({"response": "response for bar"}))
done()
}
can(req, res)
})

xit("should select the first response with no query string", function (done) {
req.url = "/multiple_get_responses"
res.end = function (content) {
expect(content).toEqual(JSON.stringify({"response": "response for baz"}))
done()
}
can(req, res)
})
})

describe("variable POST responses", function() {
var req, data
beforeEach(function() {
Expand Down
9 changes: 9 additions & 0 deletions spec/test_responses/_multiple_get_responses.get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! params: {"foo": "baz"}
{
"response": "response for baz"
}

//! params: {"foo": "bar"}
{
"response": "response for bar"
}

0 comments on commit a640546

Please sign in to comment.