From 7125719979b7de1257bbf699ff1d3bff3125b228 Mon Sep 17 00:00:00 2001 From: Jeppe Knockaert Date: Thu, 18 Aug 2022 11:26:03 +0200 Subject: [PATCH] Prevent double request interception on POST --- bin/browser.js | 33 ++++++++++++++++----------------- tests/BrowsershotTest.php | 8 ++++++++ 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/bin/browser.js b/bin/browser.js index 0ed9816e..5df5ff52 100644 --- a/bin/browser.js +++ b/bin/browser.js @@ -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; @@ -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 }); }); diff --git a/tests/BrowsershotTest.php b/tests/BrowsershotTest.php index ea0f6050..33a7a21d 100644 --- a/tests/BrowsershotTest.php +++ b/tests/BrowsershotTest.php @@ -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')