Skip to content

Commit cd8f63a

Browse files
committed
implement adding Content-Length header
- derived from PR f#22
1 parent 31f6fac commit cd8f63a

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

graphql.js

+4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@
116116
}
117117
}
118118

119+
if (cleaned.body) {
120+
headers['Content-Length'] = cleaned.body.length;
121+
}
122+
119123
__doRequest(
120124
method,
121125
cleaned.url,

test/test.js

+14
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ fragment auth_error on Error {messages}`;
211211
expect(xhr.open).toHaveBeenCalledWith(method, expect.stringMatching(url), true)
212212
expect(xhr.open).toHaveBeenCalledWith(method, expect.stringMatching(/\?query=.+&variables=/), true)
213213
});
214+
215+
it('does not send the content-length header', () => {
216+
let xhr = mockXHR(200, {});
217+
xhr.send = jest.fn();
218+
fetchPost({id: 123});
219+
expect(xhr.setRequestHeader).not.toHaveBeenCalledWith('Content-Length', expect.anything);
220+
});
214221
});
215222

216223
describe('when executing the queries normally', () => {
@@ -221,6 +228,13 @@ fragment auth_error on Error {messages}`;
221228
expect(xhr.send).toHaveBeenCalled();
222229
});
223230

231+
it('sends the content-length header', () => {
232+
let xhr = mockXHR(200, {});
233+
xhr.send = jest.fn();
234+
fetchPost({id: 123});
235+
expect(xhr.setRequestHeader).toHaveBeenCalledWith('Content-Length', 99);
236+
});
237+
224238
it('resolves the response in the promise', () => {
225239
let data = {post: {id: 123, title: 'title', text: 'text'}};
226240
mockXHR(200, data);

0 commit comments

Comments
 (0)