express-response-middleware 1.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/jsonAsync.js DELETED
@@ -1,170 +0,0 @@
1
- 'use strict'
2
-
3
- let should = require('should'),
4
- Promise = require('bluebird'),
5
- express = require('express'),
6
- request = require('supertest'),
7
- mung = require('../')
8
-
9
- describe('mung jsonAsync', () => {
10
- function noop(json, req, res) {
11
- return Promise.resolve(json)
12
- }
13
-
14
- function inspect(json, req, res) {
15
- return Promise.resolve(json).then(json => {
16
- json.inspected_by = 'me'
17
- return json
18
- })
19
- }
20
-
21
- function remove(json, req, res) {
22
- return Promise.resolve(json).then(json => null)
23
- }
24
-
25
- function reduce(json, req, res) {
26
- return Promise.resolve(json).then(json => json.a)
27
- }
28
-
29
- function life(json, req, res) {
30
- return Promise.resolve(json).then(json => 42)
31
- }
32
-
33
- function error(json, req, res) {
34
- return Promise.resolve(json).then(json => json.foo.bar.hopefully.fails())
35
- }
36
-
37
- it('should return the munged JSON result', done => {
38
- let server = express()
39
- .use(mung.jsonAsync(inspect))
40
- .get('/', (req, res) => res.status(200).json({ a: 'a' }).end())
41
- request(server)
42
- .get('/')
43
- .expect(200)
44
- .expect(res => {
45
- let expected = { a: 'a', inspected_by: 'me' }
46
- res.body.should.eql(expected)
47
- res.headers['content-length'].should.equal(
48
- JSON.stringify(expected).length.toString()
49
- )
50
- })
51
- .end(done)
52
- })
53
-
54
- it('should not mung an error response (by default)', done => {
55
- let server = express()
56
- .use(mung.jsonAsync(inspect))
57
- .get('/', (req, res) => res.status(404).json({ a: 'a' }).end())
58
- request(server)
59
- .get('/')
60
- .expect(404)
61
- .expect(res => {
62
- res.body.should.not.have.property('inspected_by')
63
- })
64
- .end(done)
65
- })
66
-
67
- it('should mung an error response when told to', done => {
68
- let server = express()
69
- .use(mung.jsonAsync(inspect, { runOnError: true }))
70
- .get('/', (req, res) => res.status(404).json({ a: 'a' }).end())
71
- request(server)
72
- .get('/')
73
- .expect(404)
74
- .expect(res => {
75
- let expected = { a: 'a', inspected_by: 'me' }
76
- res.body.should.eql(expected)
77
- res.headers['content-length'].should.equal(
78
- JSON.stringify(expected).length.toString()
79
- )
80
- })
81
- .end(done)
82
- })
83
-
84
- it('should return 204 on null JSON result', done => {
85
- let server = express()
86
- .use(mung.jsonAsync(remove))
87
- .get('/', (req, res) => res.status(200).json({ a: 'a' }).end())
88
- request(server).get('/').expect(204).end(done)
89
- })
90
-
91
- it('should return a munged scalar result as text/plain', done => {
92
- let server = express()
93
- .use(mung.jsonAsync(reduce))
94
- .get('/', (req, res) => res.status(200).json({ a: 'alpha' }).end())
95
- request(server)
96
- .get('/')
97
- .expect(200)
98
- .expect(res => {
99
- res.text.should.equal('alpha')
100
- res.headers.should.have.property(
101
- 'content-type',
102
- 'text/plain; charset=utf-8'
103
- )
104
- })
105
- .end(done)
106
- })
107
-
108
- it('should return a munged number as text/plain', done => {
109
- let server = express()
110
- .use(mung.jsonAsync(life))
111
- .get('/', (req, res) => res.status(200).json('the meaning of life').end())
112
- request(server)
113
- .get('/')
114
- .expect(200)
115
- .expect(res => {
116
- res.text.should.equal('42')
117
- res.headers.should.have.property(
118
- 'content-type',
119
- 'text/plain; charset=utf-8'
120
- )
121
- })
122
- .end(done)
123
- })
124
-
125
- it('should return a number as application/json', done => {
126
- let server = express()
127
- .use(mung.jsonAsync(noop))
128
- .get('/', (req, res) => res.status(200).json(42).end())
129
- request(server)
130
- .get('/')
131
- .expect(200)
132
- .expect(res => {
133
- res.text.should.equal('42')
134
- res.headers.should.have.property(
135
- 'content-type',
136
- 'application/json; charset=utf-8'
137
- )
138
- })
139
- .end(done)
140
- })
141
-
142
- it('should abort if a response is sent', done => {
143
- function error(json, req, res) {
144
- res.status(403).send('no permissions')
145
- return Promise.resolve(json)
146
- }
147
- let server = express()
148
- .use(mung.jsonAsync(error))
149
- .get('/', (req, res) => res.status(200).json({ a: 'a' }).end())
150
- request(server)
151
- .get('/')
152
- .expect(403)
153
- .expect(res => {
154
- res.text.should.equal('no permissions')
155
- res.headers.should.have.property(
156
- 'content-type',
157
- 'text/html; charset=utf-8'
158
- )
159
- })
160
- .end(done)
161
- })
162
-
163
- it('should 500 on an exception', done => {
164
- let server = express()
165
- .use((err, req, res, next) => res.status(501).send(err.message).end())
166
- .use(mung.jsonAsync(error))
167
- .get('/', (req, res) => res.status(200).json({ a: 'a' }).end())
168
- request(server).get('/').expect(500).end(done)
169
- })
170
- })
package/test/write.js DELETED
@@ -1,160 +0,0 @@
1
- 'use strict'
2
-
3
- let should = require('should'),
4
- express = require('express'),
5
- request = require('supertest'),
6
- mung = require('../')
7
-
8
- describe('mung write', () => {
9
- let originalResponseTextBody
10
- let originalResponseJSONBody
11
- let modifiedResponseTextBody
12
-
13
- beforeEach(() => {
14
- originalResponseTextBody = 'This is the response body'
15
- modifiedResponseTextBody = 'This is the response body with more content'
16
- originalResponseJSONBody = {
17
- a: 'a',
18
- }
19
- })
20
-
21
- function appendText(chunk, encoding, req, res) {
22
- return chunk + ' with more content'
23
- }
24
-
25
- function inspectJson(chunk, encoding, req, res) {
26
- try {
27
- const json = JSON.parse(chunk)
28
- json.inspected_by = 'me'
29
- return JSON.stringify(json)
30
- } catch (e) {
31
- console.log('JSON parse error')
32
- throw e
33
- }
34
- }
35
-
36
- function remove(chunk, encoding, req, res) {
37
- return null
38
- }
39
-
40
- function error(chunk, encoding, req, res) {
41
- chunk.foo.bar.hopefully.fails()
42
- }
43
-
44
- function error403(chunk, encoding, req, res) {
45
- res.status(403).json({ foo: 'bar ' })
46
- }
47
-
48
- it('should return the munged text result', done => {
49
- const server = express()
50
- .use(mung.write(appendText))
51
- .get('/', (req, res) => {
52
- res.status(200).write(originalResponseTextBody)
53
- res.end()
54
- })
55
- request(server)
56
- .get('/')
57
- .expect(200)
58
- .expect(res => {
59
- res.text.should.eql(modifiedResponseTextBody)
60
- })
61
- .end(done)
62
- })
63
-
64
- it('should return a munged `body` when the content type is application/json', done => {
65
- const server = express()
66
- .use(mung.write(inspectJson))
67
- .get('/', (req, res) => {
68
- res
69
- .set('Content-Type', 'application/json')
70
- .status(200)
71
- .write(JSON.stringify(originalResponseJSONBody))
72
- res.end()
73
- })
74
- request(server)
75
- .get('/')
76
- .expect(200)
77
- .expect(res => {
78
- const expectedJSON = { a: 'a', inspected_by: 'me' }
79
- const expectedStringified = JSON.stringify(expectedJSON)
80
- res.body.should.eql(expectedJSON)
81
- })
82
- .end(done)
83
- })
84
-
85
- it('should not mung an error response (by default)', done => {
86
- const server = express()
87
- .use(mung.write(appendText))
88
- .get('/', (req, res) => {
89
- res.status(404).write(originalResponseTextBody)
90
- res.end()
91
- })
92
- request(server)
93
- .get('/')
94
- .expect(404)
95
- .expect(res => {
96
- res.text.should.equal(originalResponseTextBody)
97
- res.body.should.deepEqual({})
98
- })
99
- .end(done)
100
- })
101
-
102
- it('should mung an error response when told to', done => {
103
- const server = express()
104
- .use(mung.write(appendText, { runOnError: true }))
105
- .get('/', (req, res) => {
106
- res.status(404).write(originalResponseTextBody)
107
- res.end()
108
- })
109
- request(server)
110
- .get('/')
111
- .expect(404)
112
- .expect(res => {
113
- res.text.should.eql(modifiedResponseTextBody)
114
- })
115
- .end(done)
116
- })
117
-
118
- it('should abort if a response is sent', done => {
119
- const server = express()
120
- .use(mung.write(error403))
121
- .get('/', (req, res) => {
122
- res
123
- .set('Content-Type', 'application/json')
124
- .status(200)
125
- .write(originalResponseTextBody)
126
- res.end()
127
- })
128
- request(server).get('/').expect(403).end(done)
129
- })
130
-
131
- it('should 500 on a synchronous exception', done => {
132
- const server = express()
133
- .use((err, req, res, next) => res.status(500).send(err.message).end())
134
- .use(mung.write(error))
135
- .get('/', (req, res) => {
136
- res
137
- .set('Content-Type', 'application/json')
138
- .status(200)
139
- .write(originalResponseTextBody)
140
- res.end()
141
- })
142
- request(server).get('/').expect(500).end(done)
143
- })
144
-
145
- it('should 500 on an asynchronous exception', done => {
146
- const server = express()
147
- .use((err, req, res, next) => res.status(500).send(err.message).end())
148
- .use(mung.write(error))
149
- .get('/', (req, res) => {
150
- process.nextTick(() => {
151
- res
152
- .set('Content-Type', 'application/json')
153
- .status(200)
154
- .write(originalResponseTextBody)
155
- res.end()
156
- })
157
- })
158
- request(server).get('/').expect(500).end(done)
159
- })
160
- })