Skip to content

Commit

Permalink
Read redirect history
Browse files Browse the repository at this point in the history
  • Loading branch information
bessone authored and freekmurze committed Jun 30, 2023
1 parent b167e3f commit 93708dd
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bin/browser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const request = args[0].startsWith('-f ')

const requestsList = [];

const redirectHistory = [];

const consoleMessages = [];

const failedRequests = [];
Expand All @@ -29,6 +31,12 @@ const getOutput = async (page, request) => {
return output;
}

if (request.action == 'redirectHistory') {
output = JSON.stringify(redirectHistory);

return output;
}

if (request.action == 'consoleMessages') {
output = JSON.stringify(consoleMessages);

Expand Down Expand Up @@ -118,6 +126,15 @@ const callChrome = async pup => {
}));

page.on('response', function (response) {
if (response.request().isNavigationRequest() && response.request().frame().parentFrame() === null) {
redirectHistory.push({
url: response.request().url(),
status: response.status(),
reason: response.statusText(),
headers: response.headers()
})
}

if (response.status() >= 200 && response.status() <= 399) {
return;
}
Expand Down
16 changes: 16 additions & 0 deletions src/Browsershot.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,13 @@ public function triggeredRequests(): array
return json_decode($this->callBrowser($command), true);
}

public function redirectHistory(): array
{
$command = $this->createRedirectHistoryCommand();

return json_decode($this->callBrowser($command), true);
}

/**
* @return array{type: string, message: string, location:array}
*/
Expand Down Expand Up @@ -769,6 +776,15 @@ public function createTriggeredRequestsListCommand(): array
return $this->createCommand($url, 'requestsList');
}

public function createRedirectHistoryCommand(): array
{
$url = $this->html
? $this->createTemporaryHtmlFile()
: $this->url;

return $this->createCommand($url, 'redirectHistory');
}

public function createConsoleMessagesCommand(): array
{
$url = $this->html
Expand Down
27 changes: 27 additions & 0 deletions tests/BrowsershotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,33 @@
);
});

it('can get the redirect history', function () {
$list = Browsershot::url('http://www.spatie.be')
->redirectHistory();

$list = array_map(function($item) {unset($item['headers']); return $item;}, $list);

expect($list)->toHaveCount(3);

$this->assertEquals([
[
'url' => 'http://www.spatie.be/',
'status' => 301,
'reason' => 'Moved Permanently'
],
[
'url' => 'https://www.spatie.be/',
'status' => 301,
'reason' => ''
],
[
'url' => 'https://spatie.be/',
'status' => 200,
'reason' => ''
],
], $list);
});

it('will not allow a file url', function () {
Browsershot::url('file://test');
})->throws(FileUrlNotAllowed::class);
Expand Down

0 comments on commit 93708dd

Please sign in to comment.