Skip to content

Commit

Permalink
implement adding Content-Length header
Browse files Browse the repository at this point in the history
- derived from PR f#22
  • Loading branch information
urkle committed Aug 20, 2022
1 parent 31f6fac commit cd8f63a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@
}
}

if (cleaned.body) {
headers['Content-Length'] = cleaned.body.length;
}

__doRequest(
method,
cleaned.url,
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,13 @@ fragment auth_error on Error {messages}`;
expect(xhr.open).toHaveBeenCalledWith(method, expect.stringMatching(url), true)
expect(xhr.open).toHaveBeenCalledWith(method, expect.stringMatching(/\?query=.+&variables=/), true)
});

it('does not send the content-length header', () => {
let xhr = mockXHR(200, {});
xhr.send = jest.fn();
fetchPost({id: 123});
expect(xhr.setRequestHeader).not.toHaveBeenCalledWith('Content-Length', expect.anything);
});
});

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

it('sends the content-length header', () => {
let xhr = mockXHR(200, {});
xhr.send = jest.fn();
fetchPost({id: 123});
expect(xhr.setRequestHeader).toHaveBeenCalledWith('Content-Length', 99);
});

it('resolves the response in the promise', () => {
let data = {post: {id: 123, title: 'title', text: 'text'}};
mockXHR(200, data);
Expand Down

0 comments on commit cd8f63a

Please sign in to comment.