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

Incorrect Request::toString #200

Open
dicrtarasov opened this issue Feb 8, 2020 · 0 comments
Open

Incorrect Request::toString #200

dicrtarasov opened this issue Feb 8, 2020 · 0 comments
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug

Comments

@dicrtarasov
Copy link

dicrtarasov commented Feb 8, 2020

The code:

$request = (new Client())->get('http://test.site');
$request->cookies->add(new Cookie(['name' => 'name1', 'value' => 'value1']));
echo $request->toString();

Produce next output:

GET http://test.site

There is no Cookie header.

But if add to request any headers:

$request = (new Client())->get('http://test.site');
$request->headers->add('Test-Header', 'Test-Value');
$request->cookies->add(new Cookie(['name' => 'name1', 'value' => 'value1']));
echo $request->toString();

Then output is right:

GET http://test.site
Test-Header: Test-Value
Cookie: name1=value1

This is because Message::toString() check headers existence BEFORE it composing:

if ($this->hasHeaders()) {
    $headers = $this->composeHeaderLines();

Request::composeHeaderLines() overrides Message:composeHeaderLines and compose cookies header after:

public function composeHeaderLines()
{
   $headers = parent::composeHeaderLines();
   if ($this->hasCookies()) {
       $headers[] = $this->composeCookieHeader();
  }

To fix this, we need override Message::hasHeaders in Request also:

public function hasHeaders()
{
    return parent::hasHeaders() || $this-hasCookies();
}
@samdark samdark added status:ready for adoption Feel free to implement this issue. type:bug Bug labels Feb 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready for adoption Feel free to implement this issue. type:bug Bug
Projects
None yet
Development

No branches or pull requests

2 participants