-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix up regex, fails test for #79(!) #94
base: master
Are you sure you want to change the base?
Changes from all commits
ce84616
36427bc
66cc077
18f5039
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var cannedUtils = require('./utils'); | ||
var requestMatch = /\/\/!.*(?:body|params|header):\s+([\w {}":\[\]\-\+\%,@.\/]*)/g; | ||
|
||
var RequestParser = function() {} | ||
|
||
function parseRequestOptions(line) { | ||
var match, | ||
requestItems = {}; | ||
|
||
while (match = requestMatch.exec(line)) { | ||
try { | ||
cannedUtils.recursiveMerge(requestItems, JSON.parse(match[1])); | ||
} catch (e) { | ||
console.log(e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so far the coding style is (largely) without ; so for consistency reasons I would stick with that. |
||
//@todo some logging | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you want todo logging pass the logger down from canned, so it stays configurable. |
||
} | ||
} | ||
|
||
return requestItems; | ||
} | ||
|
||
function parseEntry(lines) { | ||
var result = {}; | ||
lines.split('\n').forEach(function(line) { | ||
cannedUtils.recursiveMerge(result, parseRequestOptions(line)); | ||
}); | ||
return result; | ||
} | ||
|
||
RequestParser.prototype.parse = parseRequestOptions; | ||
RequestParser.prototype.parseEntry = parseEntry; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need to export parseEntry? Shouldn't everybody use parse as the public api? Also than you don't need to have a constructor, just module.exports = parse is fine. |
||
|
||
module.exports = RequestParser; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
var cannedUtils = require('./utils'); | ||
|
||
var responseMatch = /\/\/!.*(?:statusCode|contentType|customHeaders)/g; | ||
|
||
/** | ||
* the ResponseParser is responsible for collecting the intended return | ||
* status code, content type and header declarations. | ||
*/ | ||
function ResponseParser() {} | ||
|
||
/** | ||
* _parseresponse takes a single line from a file and extracts | ||
* JSON data if possible. Returns an object. | ||
*/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comments are not really needed I think it is fairly obvious :) |
||
function parseLine(line) { | ||
var match, | ||
response = {}; | ||
|
||
while (responseMatch.exec(line)) { | ||
try { | ||
// drop the magix comment | ||
line = line.replace("//!", '').trim(); | ||
|
||
var content = line.split(',').map(function (s) { | ||
var parts = s.split(':'); | ||
if (parts[0].trim()[-1] !== '"') { | ||
parts[0] = '"' + parts[0].trim() + '"' | ||
} | ||
return parts.join(':') | ||
}).join(',') | ||
|
||
response = cannedUtils.recursiveMerge(response, JSON.parse('{' + content + '}')) | ||
} catch(e) { | ||
console.log(e); | ||
//@todo pass in log and get cracking | ||
} | ||
} | ||
|
||
return response; | ||
} | ||
|
||
function parseEntry(lines) { | ||
var result = {}; | ||
lines.split('\n').forEach(function(line) { | ||
cannedUtils.recursiveMerge(result, parseLine(line)); | ||
}); | ||
return result; | ||
} | ||
|
||
ResponseParser.prototype.parse = parseLine; | ||
ResponseParser.prototype.parseEntry = parseEntry; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as request parser, no need to export the single version just the one that parses all the lines is fine, also no constructor needed. |
||
|
||
module.exports = ResponseParser; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -556,15 +556,104 @@ describe('canned', function () { | |
expect(parsedMeta).toEqual({ | ||
request: { | ||
serialkey: 'abc' | ||
}, | ||
params: { | ||
serialkey: '12121' | ||
} | ||
}); | ||
done(); | ||
}) | ||
}) | ||
|
||
describe("parsing metadata", function() { | ||
var Canned, can; | ||
|
||
beforeEach(function() { | ||
Canned = require('../lib/canned') | ||
can = new Canned('./spec/test_responses', {}); | ||
}) | ||
|
||
it("Should accept statusCode", function(done) { | ||
var mock_text = '//! statusCode: 418'; | ||
var parsedMeta = can.parseMetaData(mock_text); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the tests, should we maybe create a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had intended to move the parsing code out into a module in future, I think it'll help hide away some of the gnarly stuff and also let us test it in isolation more neatly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes that is what I was thinking, what I think is that all the fileParsing canned does kind of belongs together that is why I was thinking of a ResponseFileParser... but feel free to experiment. |
||
|
||
expect(parsedMeta).toEqual({ | ||
statusCode: 418 | ||
}); | ||
done(); | ||
}) | ||
|
||
it("Should accept customHeaders", function(done) { | ||
var mock_text = '//! statusCode: 418\n' + | ||
'//! customHeaders: {"Authorization": "Bearer xyz"}'; | ||
var parsedMeta = can.parseMetaData(mock_text); | ||
|
||
expect(parsedMeta).toEqual({ | ||
statusCode: 418, | ||
customHeaders: { | ||
Authorization: 'Bearer xyz' | ||
} | ||
}); | ||
done(); | ||
}) | ||
|
||
it("Should accept request body", function(done) { | ||
var mock_text = '//! statusCode: 418\n' + | ||
'//! customHeaders: {"Authorization": "Bearer xyz"}\n' + | ||
'//! customHeaders: {"Location": "Wimbledon Common"}\n' + | ||
'//! body: {"colour": "green"}'; | ||
var parsedMeta = can.parseMetaData(mock_text); | ||
|
||
expect(parsedMeta).toEqual({ | ||
statusCode: 418, | ||
customHeaders: { | ||
Authorization: 'Bearer xyz', | ||
Location: 'Wimbledon Common' | ||
}, | ||
request: { | ||
colour: 'green' | ||
} | ||
}); | ||
done(); | ||
}) | ||
|
||
it("Should accept request body", function(done) { | ||
var Canned = require('../lib/canned') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need to require in the function, just require once and use it. |
||
var can = new Canned('./spec/test_responses', {}); | ||
var mock_text = '//! statusCode: 418\n' + | ||
'//! customHeaders: {"Authorization": "Bearer xyz"}\n' + | ||
'//! body: {"colour": "green"}'; | ||
var parsedMeta = can.parseMetaData(mock_text); | ||
|
||
expect(parsedMeta).toEqual({ | ||
statusCode: 418, | ||
customHeaders: { | ||
Authorization: 'Bearer xyz' | ||
}, | ||
request: { | ||
colour: 'green' | ||
} | ||
}); | ||
done(); | ||
}) | ||
|
||
it("Should apply the latest request params, body or header specified", function(done) { | ||
var mock_text = '//! statusCode: 418\n' + | ||
'//! customHeaders: {"Authorization": "Bearer xyz"}\n' + | ||
'//! body: {"colour": "green"}\n' + | ||
'//! params: {"count": 126}'; | ||
var parsedMeta = can.parseMetaData(mock_text); | ||
|
||
expect(parsedMeta).toEqual({ | ||
statusCode: 418, | ||
customHeaders: { | ||
Authorization: 'Bearer xyz' | ||
}, | ||
request: { | ||
count: '126', | ||
} | ||
}); | ||
done(); | ||
}) | ||
}) | ||
|
||
describe("variable POST responses", function() { | ||
var req, data | ||
beforeEach(function() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use underscore
_
in the filename-
is a pain sometimes :(