Skip to content

⚒️ Declare JsonRestfulPpiResponseFlusher #377

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import BaseRestfulApiResponseFlusher from '../BaseRestfulApiResponseFlusher.js'

/**
* JSON RESTful API response flusher.
*/
export default class JsonRestfulApiResponseFlusher extends BaseRestfulApiResponseFlusher {
/** @override */
static get contentType () {
return 'application/json'
}

/** @override */
flushResponseBody () {
const json = this.generateJsonBody()

this.expressResponse.json(json)
}

/**
* Generate JSON body.
*
* @returns {{
* content: * | null
* error: RestfulApiType.Error | null
* }} - An value hash of JSON body.
*/
generateJsonBody () {
return {
content: this.renderResponse.content,
error: this.renderResponse.error,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import JsonRestfulApiResponseFlusher from '../../../../../../lib/server/restfulapi/flushers/concretes/JsonRestfulApiResponseFlusher.js'

import BaseRestfulApiResponseFlusher from '../../../../../../lib/server/restfulapi/flushers/BaseRestfulApiResponseFlusher.js'
import RestfulApiResponse from '../../../../../../lib/server/restfulapi/interfaces/RestfulApiResponse.js'
import RenchanRestfulApiError from '../../../../../../lib/server/restfulapi/errors/RenchanRestfulApiError.js'

describe('JsonRestfulApiResponseFlusher', () => {
describe('super class', () => {
test('to be BaseRestfulApiResponseFlusher', () => {
const actual = JsonRestfulApiResponseFlusher.prototype

expect(actual)
.toBeInstanceOf(BaseRestfulApiResponseFlusher)
})
})
})

describe('JsonRestfulApiResponseFlusher', () => {
describe('.get:contentType', () => {
test('to be fixed value', () => {
const expected = 'application/json'

const actual = JsonRestfulApiResponseFlusher.contentType

expect(actual)
.toBe(expected)
})
})
})

describe('JsonRestfulApiResponseFlusher', () => {
describe('#flushResponseBody', () => {
/** @type {ExpressType.Response} */
const expressResponseMock = /** @type {*} */ ({
json (args) {
return this
},
status (args) {
return this
},
set (args) {
return this
},
})

const cases = [
{
params: {
renderResponse: new RestfulApiResponse({
statusCode: 200,
headers: {
first: 'alpha value',
},
content: {
alpha: 'alpha value',
beta: 'beta value',
},
error: null,
}),
},
expected: {
content: {
alpha: 'alpha value',
beta: 'beta value',
},
error: null,
},
},
{
params: {
renderResponse: new RestfulApiResponse({
statusCode: 500,
headers: {
second: 'beta value',
},
content: null,
error: RenchanRestfulApiError.create({
code: '500',
}),
}),
},
expected: {
content: null,
error: RenchanRestfulApiError.create({
code: '500',
}),
},
},
]

test.each(cases)('renderResponse: $params.renderResponse', ({ params, expected }) => {
const flusher = new JsonRestfulApiResponseFlusher({
expressResponse: expressResponseMock,
renderResponse: params.renderResponse,
})

const generateJsonBodySpy = jest.spyOn(flusher, 'generateJsonBody')
const jsonSpy = jest.spyOn(expressResponseMock, 'json')

flusher.flushResponseBody()

expect(generateJsonBodySpy)
.toHaveBeenCalledWith()
expect(jsonSpy)
.toHaveBeenCalledWith(expected)
})
})
})

describe('JsonRestfulApiResponseFlusher', () => {
describe('#generateJsonBody', () => {
/** @type {ExpressType.Response} */
const expressResponseMock = /** @type {*} */ ({
status (args) {
return this
},
set (args) {
return this
},
})

const cases = [
{
params: {
renderResponse: new RestfulApiResponse({
statusCode: 200,
headers: {
first: 'alpha value',
},
content: {
alpha: 'alpha value',
beta: 'beta value',
},
error: null,
}),
},
expected: {
content: {
alpha: 'alpha value',
beta: 'beta value',
},
error: null,
},
},
{
params: {
renderResponse: new RestfulApiResponse({
statusCode: 500,
headers: {
second: 'beta value',
},
content: null,
error: RenchanRestfulApiError.create({
code: '500',
}),
}),
},
expected: {
content: null,
error: RenchanRestfulApiError.create({
code: '500',
}),
},
},
]

test.each(cases)('renderResponse: $params.renderResponse', ({ params, expected }) => {
const flusher = new JsonRestfulApiResponseFlusher({
expressResponse: expressResponseMock,
renderResponse: params.renderResponse,
})

const actual = flusher.generateJsonBody()

expect(actual)
.toEqual(expected)
})
})
})