Skip to content

Commit 67aa63a

Browse files
committed
[Bug] NormalizeArrayListener should not run on FormRequest
Closes #62
2 parents 5cc25de + 195a1eb commit 67aa63a

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

lib/Bitbucket/API/Http/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ public function delete($endpoint, $params = array(), $headers = array())
113113
*/
114114
public function request($endpoint, $params = array(), $method = 'GET', array $headers = array())
115115
{
116-
$request = $this->createRequest($method, $endpoint);
116+
//$request = $this->createRequest($method, $endpoint);
117+
$request = ($this->requestObj !== null) ? $this->requestObj : $this->createRequest($method, $endpoint);
117118

118119
// add a default content-type if none was set
119120
if (empty($headers['Content-Type']) && in_array(strtoupper($method), array('POST', 'PUT'), true)) {

lib/Bitbucket/API/Http/Listener/NormalizeArrayListener.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Bitbucket\API\Http\Listener;
1313

14+
use Buzz\Message\Form\FormRequestInterface;
1415
use Buzz\Message\MessageInterface;
1516
use Buzz\Message\RequestInterface;
1617

@@ -39,6 +40,10 @@ public function getName()
3940
*/
4041
public function preSend(RequestInterface $request)
4142
{
43+
if ($request instanceof FormRequestInterface) {
44+
return;
45+
}
46+
4247
$request->setContent(
4348
// Transform: "foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy"
4449
preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getContent())

test/Bitbucket/Tests/API/Http/Listener/NormalizeArrayListenerTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Bitbucket\Tests\API\Http\Listener;
44

5+
use Bitbucket\API\Api;
56
use Bitbucket\API\Http\Listener\NormalizeArrayListener;
67
use Bitbucket\Tests\API as Tests;
8+
use Buzz\Message\Form\FormRequest;
79
use Buzz\Message\Request;
810

911
/**
@@ -35,4 +37,34 @@ public function testPHPArrayToApiArrayConversion()
3537

3638
$this->assertEquals('branch=master&exclude=aaa&exclude=ccc&include=bbb', $request->getContent());
3739
}
40+
41+
/**
42+
* @ticket 62
43+
*/
44+
public function testShouldNotRunOnFormData()
45+
{
46+
$api = new Api(array(), $this->getHttpClient());
47+
$this->assertInstanceOf('\Bitbucket\API\Api', $api);
48+
49+
$api->getClient()
50+
->setApiVersion('2.0')
51+
;
52+
53+
$headers = array('Content-Type' => 'multipart/form-data');
54+
$request = new FormRequest();
55+
$request->setField('issue_id', 4);
56+
$request->setHeaders($headers);
57+
$api->getClient()->setRequest($request);
58+
59+
$this->assertTrue($api->getClient()->isListener('normalize_array'));
60+
61+
$response = $api->getClient()->request(
62+
sprintf('repositories/%s/%s/issues/%d/attachments', 'gentlero', 'eof', 4),
63+
array(),
64+
'POST',
65+
$headers
66+
);
67+
68+
$this->assertInstanceOf('\Buzz\Message\MessageInterface', $response);
69+
}
3870
}

0 commit comments

Comments
 (0)