Skip to content

Commit

Permalink
Merge pull request #92 from edreesjalili/additional-response-ext-tests
Browse files Browse the repository at this point in the history
Additional response ext tests
  • Loading branch information
jkyberneees authored May 28, 2020
2 parents b7edc8d + 69bf339 commit 15485d8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
6 changes: 3 additions & 3 deletions libs/response-extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const TYPE_OCTET = 'application/octet-stream'

const NOOP = () => {}

const stringify = (obj) => {
const stringify = obj => {
// ToDo: fast json stringify ?
return JSON.stringify(obj)
}
Expand All @@ -21,9 +21,9 @@ const preEnd = (res, contentType, statusCode) => {
res.statusCode = statusCode
}

const parseErr = (error) => {
const parseErr = error => {
const errorCode = error.status || error.code || error.statusCode
const statusCode = typeof errorCode === 'number' ? parseInt(errorCode) : 500
const statusCode = typeof errorCode === 'number' ? errorCode : 500

return {
statusCode,
Expand Down
61 changes: 60 additions & 1 deletion specs/send.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const request = require('supertest')
const { createReadStream, readFileSync } = require('fs')
const path = require('path')
const stream = require('stream')

describe('All Responses', () => {
let server
Expand All @@ -13,24 +14,51 @@ describe('All Responses', () => {
res.send('Hello World!')
})

service.get('/html-string', (req, res) => {
res.setHeader('content-type', 'text/html; charset=utf-8')
res.send('<p>Hello World!</p>')
})

service.get('/buffer', (req, res) => {
res.send(Buffer.from('Hello World!'))
})

service.get('/buffer-string', (req, res) => {
res.setHeader('content-type', 'text/plan; charset=utf-8')
res.setHeader('content-type', 'text/plain; charset=utf-8')
res.send(Buffer.from('Hello World!'))
})

service.get('/json', (req, res) => {
res.send({ id: 'restana' })
})

service.get('/json-with-content-type', (req, res) => {
res.setHeader('content-type', 'application/json')
res.send({ id: 'restana' })
})

service.get('/stream', (req, res) => {
res.setHeader('content-type', 'text/html; charset=utf-8')
res.send(createReadStream(path.resolve(__dirname, '../demos/static/src/index.html'), { encoding: 'utf8' }))
})

service.get('/stream-octet', (req, res) => {
res.send(
stream.Readable.from(
(async function * generateTinyStream () {
yield 'Hello '
yield 'World!'
})()
)
)
})

service.get('/invalid-body', (req, res) => {
res.body = true
res.setHeader('content-type', 'text/plain; charset=utf-8')
res.send()
})

service.get('/error', (req, res) => {
const err = new Error('Test')
err.code = 501
Expand All @@ -45,9 +73,18 @@ describe('All Responses', () => {
await request(server)
.get('/string')
.expect(200)
.expect('content-type', 'text/plain; charset=utf-8')
.expect('Hello World!')
})

it('should GET 200 and html content on /html-string', async () => {
await request(server)
.get('/html-string')
.expect(200)
.expect('content-type', 'text/html; charset=utf-8')
.expect('<p>Hello World!</p>')
})

it('should GET 200 and buffer content on /buffer', async () => {
await request(server)
.get('/buffer')
Expand All @@ -60,6 +97,7 @@ describe('All Responses', () => {
await request(server)
.get('/buffer-string')
.expect(200)
.expect('content-type', 'text/plain; charset=utf-8')
.expect('Hello World!')
})

Expand All @@ -71,6 +109,14 @@ describe('All Responses', () => {
.expect({ id: 'restana' })
})

it('should GET 200 and json content on /json-with-content-type', async () => {
await request(server)
.get('/json-with-content-type')
.expect(200)
.expect('content-type', 'application/json')
.expect({ id: 'restana' })
})

it('should GET 200 and buffer content on /stream', async () => {
await request(server)
.get('/stream')
Expand All @@ -79,6 +125,19 @@ describe('All Responses', () => {
.expect(readFileSync(path.resolve(__dirname, '../demos/static/src/index.html'), 'utf8'))
})

it('should GET 200 and buffer content on /stream-octet', async () => {
await request(server)
.get('/stream-octet')
.expect(200)
.expect('content-type', 'application/octet-stream')
})

it('should GET 500 and buffer content on /invalid-body', async () => {
await request(server)
.get('/invalid-body')
.expect(500)
})

it('should GET 501 and json content on /error', async () => {
await request(server)
.get('/error')
Expand Down

0 comments on commit 15485d8

Please sign in to comment.