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

Support for custom HTTP headers #10

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/EventSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,13 @@ class EventSource extends EventEmitter
private $request;
private $timer;
private $reconnectTime = 3.0;
private $headers;
private $defaultHeaders = [
'Accept' => 'text/event-stream',
'Cache-Control' => 'no-cache'
];

public function __construct($url, LoopInterface $loop, Browser $browser = null)
public function __construct($url, LoopInterface $loop, Browser $browser = null, array $headers = [])
{
$parts = parse_url($url);
if (!isset($parts['scheme'], $parts['host']) || !in_array($parts['scheme'], array('http', 'https'))) {
Expand All @@ -91,17 +96,18 @@ public function __construct($url, LoopInterface $loop, Browser $browser = null)
$this->browser = $browser->withOptions(array('streaming' => true, 'obeySuccessCode' => false));
$this->loop = $loop;
$this->url = $url;
$this->headers = array_merge(
$headers,
$this->defaultHeaders
);

$this->readyState = self::CONNECTING;
$this->request();
}

private function request()
{
$headers = array(
'Accept' => 'text/event-stream',
'Cache-Control' => 'no-cache'
);
$headers = $this->headers;
if ($this->lastEventId !== '') {
$headers['Last-Event-ID'] = $this->lastEventId;
}
Expand Down
37 changes: 37 additions & 0 deletions tests/EventSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,43 @@ public function testConstructorCanBeCalledWithoutBrowser()
$this->assertInstanceOf('Clue\React\Buzz\Browser', $browser);
}


public function testConstructorCanBeCalledWithoutCustomHeaders()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$es = new EventSource('http://example.valid', $loop);

$ref = new ReflectionProperty($es, 'headers');
$ref->setAccessible(true);
$headers = $ref->getValue($es);

$ref = new ReflectionProperty($es, 'defaultHeaders');
$ref->setAccessible(true);
$defaultHeaders = $ref->getValue($es);

$this->assertEquals($defaultHeaders, $headers);
}

public function testConstructorCanBeCalledWithCustomHeaders()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();

$es = new EventSource('http://example.valid', $loop, null, ['x-custom' => '1234']);

$ref = new ReflectionProperty($es, 'headers');
$ref->setAccessible(true);
$headers = $ref->getValue($es);

// Could have used the defaultHeaders property on EventSource,
// but this ensures the defaults are not altered by hardcoding their values in this test
$this->assertEquals(array(
'Accept' => 'text/event-stream',
'Cache-Control' => 'no-cache',
'x-custom' => '1234'
), $headers);
}

public function testConstructorWillSendGetRequestThroughGivenBrowser()
{
$loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
Expand Down