Skip to content

Commit

Permalink
fix(hariko-parser): support case on document has attributes #20
Browse files Browse the repository at this point in the history
  • Loading branch information
rymizuki committed Sep 6, 2019
1 parent 1a8e2cc commit a89b99f
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "gulp",
"build": "npm run lint && tsc",
"test": "npm run lint && npm run test:js && npm run test:ts",
"test": "npm run lint && npm run test:js && npm run test:ts && npm run test:cases",
"test:js": "mocha --ui bdd --reporter spec test/**/*.js",
"test:ts": "TS_NODE_FILES=true mocha --ui bdd --reporter spec --require espower-typescript/guess src/**/*.test.ts",
"test:cases": "TS_NODE_FILES=true mocha --ui bdd --reporter spec --require espower-typescript/guess test/case/**/*.test.ts",
Expand Down
11 changes: 11 additions & 0 deletions src/@types/protagonist.d.ts
Expand Up @@ -112,6 +112,17 @@ declare module 'protagonist' {
}
content: {
element: 'asset'
meta?: {
classes: {
element: 'array'
content: [
{
element: 'string'
content: 'messageBody' | 'messageBodySchema'
}
]
}
}
attributes: {
contentType: {
element: 'string'
Expand Down
3 changes: 2 additions & 1 deletion src/hariko-parser/conversion/builders/response-builder.ts
Expand Up @@ -18,7 +18,8 @@ export class ResponseBuilder {

get data() {
if (this.http_response.isJsonResponse()) {
return JSON.parse(this.body)
// remove zero width space
return JSON.parse(this.body.replace(/[\u200B-\u200D\uFEFF]/g, ''))
}
return null
}
Expand Down
23 changes: 22 additions & 1 deletion src/hariko-parser/structure/http-response.ts
Expand Up @@ -38,9 +38,30 @@ export class HttpResponse {
http_transaction: HttpTransaction,
data: ProtagonistHttpResponse
) {
const status = data.attributes ? data.attributes.statusCode.content : 200
const messageBody = data.content.find((content) => {
return (
content.element === 'asset' &&
content.meta &&
content.meta.classes.content[0].content === 'messageBody'
)
})

if (messageBody) {
const headers = new HttpResponseHeaders()
headers.set('Content-Type', messageBody.attributes.contentType.content)

return new HttpResponse(
http_transaction,
status,
headers,
messageBody.content
)
}

return new HttpResponse(
http_transaction,
data.attributes ? data.attributes.statusCode.content : 200,
status,
HttpResponseHeaders.create(data),
data.content.length ? data.content[0].content : ''
)
Expand Down
35 changes: 35 additions & 0 deletions test/case/have-attributes/docs.apib
@@ -0,0 +1,35 @@
# Case 3 [POST /case/03]
+ Relation: self
+ Request basic (application/json)
+ Attributes
+ name :'tarou' (string) - user name
+ age :19 (number) - user age
+ Body
{
name: 'tarou',
age: 19
}
+ Response 200 (application/json; charset=utf8)
+ Headers
+ Attributes
+ id : 1 (number) - user's id
+ name: 'tarou' (string) - user's name
+ created_at: '2019-09-09 01:01:01' (string) - create datetime
+ Body
{
id: 1,
name: 'tarou',
created_at: '2019-09-09 01:01:01'
}
68 changes: 68 additions & 0 deletions test/case/have-attributes/index.test.ts
@@ -0,0 +1,68 @@
/* eslint-env mocha */
import assert = require('assert')
import * as path from 'path'
import * as HarikoParser from '../../../src/hariko-parser'
import { load } from '../../utils/fixture'

describe('case: have a some attributes', () => {
it('should be enable parse', () => {
const content = load(path.join(__dirname, './docs.apib'))
const result = HarikoParser.parse(content)
assert.deepEqual(result, {
entries: [
{
file: 'basic-POST.json',
request: {
method: 'POST',
uri: {
path: 'basic',
queries: [],
template: 'basic'
}
},
response: {
body:
"​\n {\n id: 1,\n name: 'tarou',\n created_at: '2019-09-09 01:01:01'\n }\n",
data: {
created_at: '2019-09-09 01:01:01',
id: 1,
name: 'tarou'
},
headers: [
{
name: 'Content-Type',
value: 'application/json; charset=utf8'
}
],
statusCode: 200
}
},
{
request: {
method: 'POST',
uri: {
path: '/case/03',
template: '/case/03',
queries: []
}
},
response: {
statusCode: 200,
headers: [
{ name: 'Content-Type', value: 'application/json; charset=utf8' }
],
body:
"​\n {\n id: 1,\n name: 'tarou',\n created_at: '2019-09-09 01:01:01'\n }\n",
data: {
created_at: '2019-09-09 01:01:01',
id: 1,
name: 'tarou'
}
},
file: 'case/03-POST.json'
}
],
warnings: []
})
})
})

0 comments on commit a89b99f

Please sign in to comment.