Skip to content

Commit

Permalink
BrowserClient implementation + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Athlon1600 committed Jan 10, 2020
1 parent 534f9f4 commit 719bdfd
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/BrowserClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Curl;

class BrowserClient extends Client
{
protected $headers = array(
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Encoding' => 'identity',
'Accept-Language' => 'en-US,en;q=0.5',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0'
);

protected $options = array(
CURLOPT_ENCODING => '', // apparently curl will decode gzip automatically when this is empty
CURLOPT_SSL_VERIFYHOST => 0,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 15
);

protected $cookie_file;

public function __construct()
{
parent::__construct();

$cookie_file = join(DIRECTORY_SEPARATOR, [sys_get_temp_dir(), "BrowserClient"]);
$this->setCookieFile($cookie_file);
}

public function setCookieFile($cookie_file)
{
$this->cookie_file = $cookie_file;

// read & write cookies
$this->options[CURLOPT_COOKIEJAR] = $cookie_file;
$this->options[CURLOPT_COOKIEFILE] = $cookie_file;
}

public function getCookieFile()
{
return $this->cookie_file;
}

public function clearCookies()
{
unlink($this->getCookieFile());
}
}
27 changes: 27 additions & 0 deletions tests/BrowserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Curl\Tests;

use Curl\BrowserClient;
use PHPUnit\Framework\TestCase;

class BrowserTest extends TestCase
{
public function test_cookies()
{
$browser = new BrowserClient();
$browser->clearCookies();

$cookies = array(
'cookie_one' => '111',
'cookie_two' => 222
);

$browser->get('https://httpbin.org/cookies/set', $cookies);

$response = $browser->get('https://httpbin.org/cookies');
$json = json_decode($response->body, true);

$this->assertEquals($cookies, $json['cookies']);
}
}

0 comments on commit 719bdfd

Please sign in to comment.