Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent double request interception on POST #664

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions bin/browser.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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