Skip to content

Commit

Permalink
feat(resource/parser): support carriage '\r' in source data
Browse files Browse the repository at this point in the history
  • Loading branch information
rymizuki committed Dec 24, 2017
1 parent ebaf9cb commit 1114bba
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 155 deletions.
6 changes: 6 additions & 0 deletions lib/hariko/resource/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ function toEntry (resource, action, example, request, response) {
exports.parse = function (rawdata) {
var entries = [],
warnings = null;

// the use of carriage return(s) '\r' in source data supported
rawdata = rawdata.replace(/\r/g, '\n')

var data = protagonist.parseSync(rawdata, {type: 'ast'});

if (data.warnings) warnings = data.warnings;

for (var i = 0; i < data.ast.resourceGroups.length; i++) {
var resourceGroup = data.ast.resourceGroups[i];
for (var ii = 0; ii < resourceGroup.resources.length; ii++) {
Expand Down
344 changes: 189 additions & 155 deletions test/hariko/resource/parse_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,181 +12,215 @@ describe('hariko/resource/parser', function () {
parser = null;
});
describe('.parse(markdown_data)', function () {
it('should be parse content', function () {
var data = parser.parse(fs.readFileSync(path.join(path.dirname(__filename), 'fixture.md')).toString());
expect(data).to.be.eql({
entries: [
{
"file": "index-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/",
"template": "/",
"queries": []
describe('basic cases', function () {
it('should be parse content', function () {
var data = parser.parse(fs.readFileSync(path.join(path.dirname(__filename), 'fixture.md')).toString());
expect(data).to.be.eql({
entries: [
{
"file": "index-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/",
"template": "/",
"queries": []
}
},
"response": {
"statusCode": 200,
"headers": [{"name": "Content-Type", "value": "text/plain"}],
"body": "Hello world\n",
"data": null
}
},
"response": {
"statusCode": 200,
"headers": [{"name": "Content-Type", "value": "text/plain"}],
"body": "Hello world\n",
"data": null
}
},
{
"file": "api/app-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/app",
"template": "/api/app",
"queries": []
{
"file": "api/app-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/app",
"template": "/api/app",
"queries": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
{
"file": "api/user?page-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/user",
"template": "/api/user{?page}",
"queries": ['page']
{
"file": "api/user?page-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/user",
"template": "/api/user{?page}",
"queries": ['page']
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
{
"file": "api/user/user_id-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/user/:user_id",
"template": "/api/user/{user_id}",
"queries": []
{
"file": "api/user/user_id-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/user/:user_id",
"template": "/api/user/{user_id}",
"queries": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
{
"file": "api/user-POST.json",
"request": {
"method": "POST",
"uri": {
"path": "/api/user",
"template": "/api/user",
"queries": []
{
"file": "api/user-POST.json",
"request": {
"method": "POST",
"uri": {
"path": "/api/user",
"template": "/api/user",
"queries": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
{
"file": "api/user/user_id-PUT.json",
"request": {
"method": "PUT",
"uri": {
"path": "/api/user/:user_id",
"template": "/api/user/{user_id}",
"queries": []
{
"file": "api/user/user_id-PUT.json",
"request": {
"method": "PUT",
"uri": {
"path": "/api/user/:user_id",
"template": "/api/user/{user_id}",
"queries": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " {}\n",
"data": {},
}
},
{
"file": "api/item/hariko-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/item/hariko",
"template": "/api/item/{item_id}",
"queries": []
{
"file": "api/item/hariko-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/item/hariko",
"template": "/api/item/{item_id}",
"queries": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " []\n",
"data": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " []\n",
"data": []
}
},
{
"file": "api/item/deco-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/item/deco",
"template": "/api/item/{item_id}",
"queries": []
{
"file": "api/item/deco-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/item/deco",
"template": "/api/item/{item_id}",
"queries": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " []\n",
"data": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " []\n",
"data": []
}
},
{
"file": "api/item/item_id-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/item/:item_id",
"template": "/api/item/{item_id}",
"queries": []
{
"file": "api/item/item_id-GET.json",
"request": {
"method": "GET",
"uri": {
"path": "/api/item/:item_id",
"template": "/api/item/{item_id}",
"queries": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " []\n",
"data": []
}
},
"response": {
"statusCode": 200,
"headers": [{name: 'Content-Type', value: 'application/json'}],
"body": " []\n",
"data": []
],
warnings: [
{
"code": 5,
"message": "unexpected header block, expected a group, resource or an action definition, e.g. '# Group <name>', '# <resource name> [<URI>]' or '# <HTTP method> <URI>'",
"location": [
{"index": 686, "length": 7}
]
},
{
"code": 6,
"message": "action is missing a response",
"location": [
{"index":693,"length":26}
]
}
},
],
warnings: [
{
"code": 5,
"message": "unexpected header block, expected a group, resource or an action definition, e.g. '# Group <name>', '# <resource name> [<URI>]' or '# <HTTP method> <URI>'",
"location": [
{"index": 686, "length": 7}
]
},
{
"code": 6,
"message": "action is missing a response",
"location": [
{"index":693,"length":26}
]
}
]
]
});
});
});
describe('when linebreak by \r', function () {
var data;
beforeEach(function () {
data = parser.parse(`\r# GET /api/message\r\r+ Response 200 (application/json)\r\r + Body\r\r {message: \"hello\"}\r\r`);
})
it('should be enable parse', function () {
expect(data).to.be.eql({
entries: [
{
file: "api/message-GET.json",
request: {
method: "GET",
uri: {
path: "/api/message",
queries: [],
template: "/api/message"
}
},
response: {
body: "{message: \"hello\"}\n",
data: {message: "hello"},
headers: [
{ name: "Content-Type", value: "application/json" }
],
statusCode: 200,
}
}
],
warnings: []
})
})
});
});
});

0 comments on commit 1114bba

Please sign in to comment.