Skip to content

Commit

Permalink
fix(service): Handle responses with no headers (#438)
Browse files Browse the repository at this point in the history
* Handle responses with no headers

* Address PR suggestions

* Update changelog
  • Loading branch information
dragosMC91 authored Dec 21, 2022
1 parent 6e62164 commit 974aea9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
## wdio-intercept-service changelog

### [ [>](https://github.com/webdriverio-community/wdio-intercept-service/tree/v.next) ] v.next / <DATE>

- Handle responses with no headers (thanks @dragosMC91)

### [ [>](https://github.com/webdriverio-community/wdio-intercept-service/tree/v4.3.0) ] 4.3.0 / 17.10.2022
- Fix header-parsing code to be RFC-compliant (thanks @jbebe)
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,11 @@ class WebdriverAjax {
body: parseBody(req.body),
statusCode: req.statusCode,
};
if (!req.headers) {
console.warn(
`${transformed.method} request to ${req.url} (HTTP ${req.statusCode}) had no response headers!`
);
}
}
return transformed;
}
Expand All @@ -344,6 +349,9 @@ class WebdriverAjax {
// (best effort compliance with RFC)
function parseResponseHeaders(rawHeader) {
const headers = {};
if (!rawHeader) {
return headers;
}
const lines = rawHeader.trim().split(/(?:\r?\n)+/);
for (const line of lines) {
const parts = line.split(/(?<=^[^:]*):/);
Expand Down
21 changes: 21 additions & 0 deletions test/spec/plugin_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,27 @@ describe('webdriverajax', function testSuite() {
});
}

it('can parse responses with empty headers', async function () {
// test is disabled on firefox since the mock service only works on chrome
if (browser.capabilities.browserName === 'firefox') {
this.skip();
}
await browser.url('/post.html');
const mock = await browser.mock('**' + '/post*');
mock.respond(
{},
{
fetchResponse: false,
headers: () => ({}),
}
);
await browser.setupInterceptor();
await $('#buttonform').click();
mock.restore();
const requests = await browser.getRequests();
assert.deepEqual(requests[0].response.headers, {});
});

it('can get initialised inside an iframe', async function () {
await browser.url('/frame.html');
await browser.setupInterceptor();
Expand Down

0 comments on commit 974aea9

Please sign in to comment.