Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[Zend\Http\Client] dupplicate header keys in prepareHeaders #5065

Closed
wants to merge 4 commits into from

2 participants

ClemensSahs Matthew Weier O'Phinney
ClemensSahs

in my mind I found a bug in the Http Component. In my gist I write a TestCase to show the error.

if we you a toString-Methode from \Zend\Http\Header\ContentType

https://gist.github.com/ClemensSahs/6421612

The provenance from this issues is in the method \Zend\Http\Client::prepareHeaders here we use not the lowercase string for the header keys like the \Zend\Http\Header\* So we get this.

// 1st ( store )
array(6) {
  ["Host"]=>
  string(14) "127.0.0.1:5984"
  ["Connection"]=>
  string(5) "close"
  ["Accept-Encoding"]=>
  string(13) "gzip, deflate"
  ["User-Agent"]=>
  string(16) "Zend\Http\Client"
  ["Content-Length"]=>
  int(13)
  ["content-type"]=>
  string(16) "application/json"
}

// 2nd ( get )
array(5) {
  ["Host"]=>
  string(14) "127.0.0.1:5984"
  ["Connection"]=>
  string(5) "close"
  ["Accept-Encoding"]=>
  string(13) "gzip, deflate"
  ["User-Agent"]=>
  string(16) "Zend\Http\Client"
  ["Content-Type"]=>
  string(16) "application/json"
}
// 3rd ( store )
array(7) {
  ["Host"]=>
  string(14) "127.0.0.1:5984"
  ["Connection"]=>
  string(5) "close"
  ["Accept-Encoding"]=>
  string(13) "gzip, deflate"
  ["User-Agent"]=>
  string(16) "Zend\Http\Client"
  ["Content-Type"]=>
  string(16) "application/json"
  ["Content-Length"]=>
  int(13)
  ["content-type"]=>
  string(16) "application/json"
}

best regards

Matthew Weier O'Phinney

Please add a unit test as well to document the why of the change. Thanks in advance!

ClemensSahs

what kind of document you meen, I think you mean inline doc right?

Matthew Weier O'Phinney

@ClemensSahs I meant that the test will document why the change is being made, not that you need to write documentation. :)

ClemensSahs

@weierophinney ok the test control tow possible way for the same error case... currently I have no alternative error case for this isseus...

from my side we are fine, any feedback?

Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/5065' into develop
Forward port #5065
2b83769
Matthew Weier O'Phinney weierophinney closed this pull request from a commit
ClemensSahs ClemensSahs fix #5065 3c71496
Matthew Weier O'Phinney weierophinney referenced this pull request from a commit
Matthew Weier O'Phinney weierophinney Merge branch 'hotfix/5065'
Close #5065
2a33a10
Matthew Weier O'Phinney weierophinney was assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 31 additions and 3 deletions.
  1. +4 −3 library/Zend/Http/Client.php
  2. +27 −0 tests/ZendTest/Http/ClientTest.php
7 library/Zend/Http/Client.php
View
@@ -1154,9 +1154,10 @@ protected function prepareHeaders($body, $uri)
}
// Merge the headers of the request (if any)
- $requestHeaders = $this->getRequest()->getHeaders()->toArray();
- foreach ($requestHeaders as $key => $value) {
- $headers[$key] = $value;
+ // here we need right 'http field' and not lowercase letters
+ $requestHeaders = $this->getRequest()->getHeaders();
+ foreach ($requestHeaders as $requestHeaderElement) {
+ $headers[$requestHeaderElement->getFieldName()] = $requestHeaderElement->getFieldValue();
}
return $headers;
}
27 tests/ZendTest/Http/ClientTest.php
View
@@ -11,6 +11,7 @@
namespace ZendTest\Http;
use ReflectionClass;
+use Zend\Uri\Http;
use Zend\Http\Client;
use Zend\Http\Cookies;
use Zend\Http\Exception;
@@ -349,4 +350,30 @@ public function testAdapterAlwaysReachableIfSpecified()
$this->assertSame($testAdapter, $client->getAdapter());
}
+
+ public function testPrepareHeadersCreateRightHttpField()
+ {
+ $body = json_encode(array('foofoo'=>'barbar'));
+
+ $client = new Client();
+ $prepareHeadersReflection = new \ReflectionMethod($client, 'prepareHeaders');
+ $prepareHeadersReflection->setAccessible(true);
+
+ $request= new Request();
+ $request->getHeaders()->addHeaderLine('content-type','application/json');
+ $request->getHeaders()->addHeaderLine('content-length',strlen($body));
+ $client->setRequest($request);
+
+ $client->setEncType('application/json');
+
+ $this->assertSame($client->getRequest(),$request);
+
+ $headers = $prepareHeadersReflection->invoke($client,$body,new Http('http://localhost:5984'));
+
+ $this->assertArrayNotHasKey('content-type', $headers);
+ $this->assertArrayHasKey('Content-Type', $headers);
+
+ $this->assertArrayNotHasKey('content-length', $headers);
+ $this->assertArrayHasKey('Content-Length', $headers);
+ }
}
Something went wrong with that request. Please try again.