Skip to content

Commit

Permalink
feat(hariko-parser): support body content is zero case #20
Browse files Browse the repository at this point in the history
  • Loading branch information
rymizuki committed Sep 9, 2019
1 parent e3c4571 commit a2de647
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class ResponseBuilder {
}
get data() {
if (this.http_response.isJsonResponse()) {
if (!this.body.length) {
return {};
}
// remove zero width space
return json5_1.default.parse(this.body.replace(/[\u200B-\u200D\uFEFF]/g, ''));
}
Expand Down
7 changes: 6 additions & 1 deletion lib/dist/hariko-parser/structure/http-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class HttpResponseHeaders {
const header = this.rows[index];
return header ? header.value : null;
}
hasContentType() {
return this.indexOf('Content-Type') >= 0;
}
indexOf(name) {
for (let i = 0; i < this.rows.length; i += 1) {
if (this.rows[i].name === name)
Expand All @@ -80,7 +83,9 @@ class HttpResponseHeaders {
data.attributes.headers.content.forEach((member) => {
headers.set(member.content.key.content, member.content.value.content);
});
headers.set('Content-Type', data.content[0].attributes.contentType.content);
if (!headers.hasContentType()) {
headers.set('Content-Type', data.content[0].attributes.contentType.content);
}
return headers;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/hariko-parser/conversion/builders/response-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export class ResponseBuilder {

get data() {
if (this.http_response.isJsonResponse()) {
if (!this.body.length) {
return {}
}
// remove zero width space
return JSON.parse(this.body.replace(/[\u200B-\u200D\uFEFF]/g, ''))
}
Expand Down
11 changes: 10 additions & 1 deletion src/hariko-parser/structure/http-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ export class HttpResponseHeaders {
return header ? header.value : null
}

hasContentType() {
return this.indexOf('Content-Type') >= 0
}

indexOf(name: string) {
for (let i = 0; i < this.rows.length; i += 1) {
if (this.rows[i].name === name) return i
Expand All @@ -111,7 +115,12 @@ export class HttpResponseHeaders {
data.attributes.headers.content.forEach((member) => {
headers.set(member.content.key.content, member.content.value.content)
})
headers.set('Content-Type', data.content[0].attributes.contentType.content)
if (!headers.hasContentType()) {
headers.set(
'Content-Type',
data.content[0].attributes.contentType.content
)
}
return headers
}
}
6 changes: 6 additions & 0 deletions test/case/havent-body/docs.apib
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Group Haven't body contents

# send post [POST /case/04]

+ Response 200 (application/json)
+ Body
39 changes: 39 additions & 0 deletions test/case/havent-body/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* 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 not body contents', () => {
it('should be enable parse', () => {
const content = load(path.join(__dirname, './docs.apib'))
const result = HarikoParser.parse(content)
assert.deepEqual(result, {
entries: [
{
file: 'case/04-POST.json',
request: {
method: 'POST',
uri: {
path: '/case/04',
queries: [],
template: '/case/04'
}
},
response: {
body: '',
data: {},
headers: [
{
name: 'Content-Type',
value: 'application/json'
}
],
statusCode: 200
}
}
],
warnings: []
})
})
})

0 comments on commit a2de647

Please sign in to comment.