Skip to content

Commit ab1c8cf

Browse files
authored
Merge pull request #2 from yedaodao/master
fix a bug about json middleware
2 parents 316f14f + fd867be commit ab1c8cf

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

modules/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ export const form = () => request => {
124124
export const json = () => request => {
125125
if (request.options.json) {
126126
request.options.body = JSON.stringify(request.options.json);
127-
request.options.headers.Accept = 'application/json';
128127
request.options.headers['Content-Type'] = 'application/json';
129128
}
129+
request.options.headers.Accept = 'application/json';
130130

131131
return response => {
132132
const contentType = response.headers.get('Content-Type') || '';

test/test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,30 @@ describe('Middleware json', done => {
178178
assert.equal(request.options.headers['Content-Type'], 'application/json');
179179
assert.equal(request.options.headers.Accept, 'application/json');
180180
});
181+
182+
it('should handle request without body and response.', () => {
183+
const request = {
184+
options: {
185+
headers: {},
186+
},
187+
};
188+
189+
const response = {
190+
headers: {
191+
get: key => {
192+
assert.equal(key, 'Content-Type');
193+
return 'application/json';
194+
},
195+
},
196+
json: () => Promise.resolve(({ key: 'value' })),
197+
};
198+
199+
json()(request)(response).then(response => {
200+
assert.equal(response.jsonData, { key: 'value' });
201+
done();
202+
});
203+
assert.equal(request.options.headers.Accept, 'application/json');
204+
});
181205
});
182206

183207

0 commit comments

Comments
 (0)