Skip to content
This repository has been archived by the owner on Mar 15, 2020. It is now read-only.

Commit

Permalink
Wrap up header support for now
Browse files Browse the repository at this point in the history
  • Loading branch information
padraic committed Mar 8, 2015
1 parent bc2c025 commit 7841b65
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Humbug/FileGetContents.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ public static function setHttpHeaders($context)
$headers = self::getNextRequestHeaders();
if (!empty($headers)) {
$options = stream_context_get_options($context);
$headers = empty($options) ? $headers : array_merge($options['http']['headers'], $headers);
if (!isset($options['http'])) {
$options['http'] = array('header'=>array());
} elseif (!isset($options['http']['header'])) {
$options['http']['header'] = array();
} elseif (is_string($options['http']['header'])) {
$options['http']['header'] = explode("\r\n", $options['http']['header']);
}
$headers = empty($options['http']['headers']) ? $headers : array_merge($options['http']['headers'], $headers);
stream_context_set_option(
$context,
'http',
Expand Down
2 changes: 1 addition & 1 deletion src/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function humbug_get_headers() {

if (!function_exists('humbug_set_headers')) {

function humbug_set_headers($headers) {
function humbug_set_headers(array $headers) {
return FileGetContents::setNextRequestHeaders($headers);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Humbug/Test/FunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,16 @@ public function testCanGetResponseHeaders()
humbug_get_contents('http://padraic.github.io');
$this->assertTrue(count(humbug_get_headers()) > 0);
}

public function testCanSetRequestHeaders()
{
humbug_set_headers(array(
'Accept-Language: da',
'User-Agent: Humbug'
));
$out = humbug_get_contents('http://myhttp.info/');
$this->assertEquals(1, preg_match('%'.preg_quote('<td>Accept language</td><td>da</td>').'%', $out));
$this->assertEquals(1, preg_match('%'.preg_quote('<td>User agent</td><td>Humbug</td>').'%', $out));
}

}

0 comments on commit 7841b65

Please sign in to comment.