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

Fix headers options #26

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Formatter/CurlFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,15 @@ protected function extractCookiesArgument(RequestInterface $request, array $opti

/**
* @param RequestInterface $request
* @param array $options
*/
protected function extractHeadersArgument(RequestInterface $request)
protected function extractHeadersArgument(RequestInterface $request, array $options)
{
foreach ($request->getHeaders() as $name => $header) {
$headers = array_merge(
$request->getHeaders(),
isset($options['headers']) ? $options['headers'] : []
);
foreach ($headers as $name => $header) {
if ('host' === strtolower($name) && $header[0] === $request->getUri()->getHost()) {
continue;
}
Expand Down Expand Up @@ -219,7 +224,7 @@ protected function extractArguments(RequestInterface $request, array $options)
$this->extractHttpMethodArgument($request);
$this->extractBodyArgument($request);
$this->extractCookiesArgument($request, $options);
$this->extractHeadersArgument($request);
$this->extractHeadersArgument($request, $options);
$this->extractUrlArgument($request);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Formatter/CurlFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ public function testSimpleGETWithMultipleHeader()
$this->assertEquals("curl 'http://example.local' -H 'foo: bar' -H 'Accept-Encoding: gzip,deflate,sdch'", $curl);
}

public function testGetWithHeadersAsOptions()
{
$requestWithHeaders = new Request('GET', 'http://example.local', ['lorem' => 'ipsum']);
$curlWithoutOptions = $this->curlFormatter->format($requestWithHeaders);

$requestWithoutHeaders = new Request('GET', 'http://example.local');
$options = ['headers' => ['lorem' => 'ipsum']];
$curlWithOptions = $this->curlFormatter->format(
$requestWithoutHeaders,
$options
);

$this->assertContains("-H 'lorem: ipsum'", $curlWithoutOptions);
$this->assertContains("-H 'lorem: ipsum'", $curlWithOptions);
$this->assertEquals($curlWithOptions, $curlWithoutOptions);
}

public function testGETWithQueryString()
{
$request = new Request('GET', 'http://example.local?foo=bar');
Expand Down