Skip to content

Commit

Permalink
Prevent double request interception on POST
Browse files Browse the repository at this point in the history
  • Loading branch information
JeppeKnockaert authored and freekmurze committed Aug 19, 2022
1 parent 9e93f0c commit 7125719
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
33 changes: 16 additions & 17 deletions bin/browser.js
Expand Up @@ -101,23 +101,6 @@ const callChrome = async pup => {

await page.setRequestInterception(true);

if (request.postParams) {
const postParamsArray = request.postParams;
const queryString = Object.keys(postParamsArray)
.map(key => `${key}=${postParamsArray[key]}`)
.join('&');
page.once("request", interceptedRequest => {
interceptedRequest.continue({
method: "POST",
postData: queryString,
headers: {
...interceptedRequest.headers(),
"Content-Type": "application/x-www-form-urlencoded"
}
});
});
}

const contentUrl = request.options.contentUrl;
const parsedContentUrl = contentUrl ? contentUrl.replace(/\/$/, "") : undefined;
let pageContent;
Expand Down Expand Up @@ -195,6 +178,22 @@ const callChrome = async pup => {
}
}

if (request.postParams) {
const postParamsArray = request.postParams;
const queryString = Object.keys(postParamsArray)
.map(key => `${key}=${postParamsArray[key]}`)
.join('&');
interceptedRequest.continue({
method: "POST",
postData: queryString,
headers: {
...interceptedRequest.headers(),
"Content-Type": "application/x-www-form-urlencoded"
}
});
return;
}

interceptedRequest.continue({ headers });
});

Expand Down
8 changes: 8 additions & 0 deletions tests/BrowsershotTest.php
Expand Up @@ -980,6 +980,14 @@
], $command);
});

it('can process post request', function () {
$html = Browsershot::url('https://httpbin.org/post')
->post(['foo' => 'bar'])
->bodyHtml();

expect($html)->toContain('"foo": "bar"');
});

it('can click on the page', function () {
$command = Browsershot::url('https://example.com')
->click('#selector1')
Expand Down

0 comments on commit 7125719

Please sign in to comment.