Skip to content

Commit b6de2c0

Browse files
committed
Merge pull request #29 from tyx/feature/18/leave_guzzle
Replace guzzle3 with egeloen/ivory-http-adapter
2 parents a2a5e4e + 1d908c6 commit b6de2c0

File tree

8 files changed

+197
-114
lines changed

8 files changed

+197
-114
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ default:
1717
rest:
1818
base_url: http://localhost:8888
1919
store_response: true
20+
adaptor_name: curl # Should be one of these adapters : https://github.com/egeloen/ivory-http-adapter/blob/master/doc/adapters.md#factory
2021
suites:
2122
default:
2223
contexts:
2324
- Rezzza\RestApiBehatExtension\RestApiContext
2425
- Rezzza\RestApiBehatExtension\Json\JsonContext
2526
```
2627
28+
Regarding the `adaptor_name` you choose, you will have to install the deps needed on your own.
29+
2730
## Usage
2831
You can use directly the `JsonContext` or `RestApiContext` by loading them in your behat.yml or use the `RestApiBrowser` and `JsonInspector` by adding them in the construct of your own context.
2932

behat.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ default:
1212
rest:
1313
base_url: http://localhost:8888
1414
store_response: true
15+
adaptor_name: curl

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
"php": ">=5.3.2",
2020
"behat/behat": "~3.0",
2121
"atoum/atoum": "~1.0",
22-
"guzzle/http": "~3.9",
2322
"symfony/property-access": "~2.4",
24-
"justinrainbow/json-schema": "~1.3"
23+
"justinrainbow/json-schema": "~1.3",
24+
"egeloen/http-adapter": "^0.8.0"
2525
},
2626
"require-dev": {
2727
"silex/silex": "~1.0",

src/Extension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Extension implements ExtensionInterface
1515
public function load(ContainerBuilder $container, array $config)
1616
{
1717
$container->setParameter('rezzza.json_api.rest.base_url', $config['rest']['base_url']);
18+
$container->setParameter('rezzza.json_api.rest.adaptor_name', $config['rest']['adaptor_name']);
1819
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/Resources'));
1920
$loader->load('services.xml');
2021

@@ -35,6 +36,7 @@ public function configure(ArrayNodeDefinition $builder)
3536
->scalarNode('base_url')->end()
3637
->booleanNode('store_response')
3738
->defaultTrue()->end()
39+
->scalarNode('adaptor_name')->end()
3840
->end()
3941
->end()
4042
->end()

src/Resources/services.xml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" ?>
2-
<container xmlns="http://symfony.com/schema/dic/services"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://symfony.com/schema/dic/services"
44
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
55
<services>
66
<service id="rezzza.json_api.json.json_storage" class="Rezzza\RestApiBehatExtension\Json\JsonStorage" />
@@ -18,12 +18,9 @@
1818
<tag name="context.argument_resolver" />
1919
</service>
2020

21-
<service id="rezzza.json_api.rest.http_client" class="Guzzle\Http\Client" public="false">
22-
<argument>%rezzza.json_api.rest.base_url%</argument>
23-
</service>
24-
2521
<service id="rezzza.json_api.rest.rest_api_browser" class="Rezzza\RestApiBehatExtension\Rest\RestApiBrowser" public="false">
26-
<argument type="service" id="rezzza.json_api.rest.http_client" />
22+
<argument>%rezzza.json_api.rest.base_url%</argument>
23+
<argument>%rezzza.json_api.rest.adaptor_name%</argument>
2724
</service>
2825

2926
<service id="rezzza.json_api.rest.rest_api_browser.resolver" class="Rezzza\RestApiBehatExtension\Rest\RestApiBrowserResolver">

src/Rest/RestApiBrowser.php

Lines changed: 91 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
namespace Rezzza\RestApiBehatExtension\Rest;
44

5-
use Guzzle\Http\Exception\BadResponseException;
6-
use Guzzle\Http\ClientInterface as HttpClient;
7-
use Guzzle\Http\Message\Response;
5+
use Ivory\HttpAdapter\HttpAdapterFactory;
6+
use Ivory\HttpAdapter\HttpAdapterInterface as HttpClient;
7+
use Ivory\HttpAdapter\HttpAdapterException;
8+
use Ivory\HttpAdapter\Message\Request;
9+
use Zend\Diactoros\Stream;
10+
use Psr\Http\Message\RequestInterface;
11+
use Psr\Http\Message\ResponseInterface;
812
use Behat\Gherkin\Node\PyStringNode;
913

1014
class RestApiBrowser
1115
{
1216
/** @var HttpClient */
1317
private $httpClient;
1418

15-
/** @var array|\Guzzle\Http\Message\RequestInterface */
19+
/** @var RequestInterface */
1620
private $request;
1721

18-
/** @var \Guzzle\Http\Message\Response|array */
22+
/** @var ResponseInterface */
1923
private $response;
2024

2125
/** @var array */
@@ -24,21 +28,50 @@ class RestApiBrowser
2428
/** @var ResponseStorage */
2529
private $responseStorage;
2630

27-
public function __construct(HttpClient $httpClient)
31+
/**
32+
* @param string $base_url
33+
* @param string|null $adaptor_name
34+
* @throws HttpAdapterException
35+
*/
36+
public function __construct($base_url, $adaptor_name, HttpClient $httpClient = null)
2837
{
29-
$this->httpClient = $httpClient;
38+
if (!is_null($httpClient) && $httpClient instanceof HttpClient) {
39+
$this->httpClient = $httpClient;
40+
} else {
41+
if (is_string($adaptor_name) && HttpAdapterFactory::capable($adaptor_name)) {
42+
$this->httpClient = HttpAdapterFactory::create($adaptor_name);
43+
} else {
44+
$this->httpClient = HttpAdapterFactory::guess();
45+
}
46+
$this->httpClient->getConfiguration()->setBaseUri($base_url);
47+
}
3048
}
3149

50+
/**
51+
* @param ResponseStorage $responseStorage
52+
*/
3253
public function enableResponseStorage(ResponseStorage $responseStorage)
3354
{
3455
$this->responseStorage = $responseStorage;
3556
}
3657

58+
59+
/**
60+
* @return ResponseInterface
61+
*/
3762
public function getResponse()
3863
{
3964
return $this->response;
4065
}
4166

67+
/**
68+
* @param ResponseInterface $response
69+
*/
70+
public function setResponse(ResponseInterface $response)
71+
{
72+
$this->response = $response;
73+
}
74+
4275
public function getRequest()
4376
{
4477
return $this->request;
@@ -49,83 +82,76 @@ public function getRequestHeaders()
4982
return $this->requestHeaders;
5083
}
5184

85+
/**
86+
* @return HttpClient
87+
*/
88+
public function getHttpClient()
89+
{
90+
return $this->httpClient;
91+
}
92+
5293
/**
5394
* @param string $method
5495
* @param string $url
55-
* @param PyStringNode $body
56-
* @param array $options
96+
* @param string $body
5797
*/
58-
public function sendRequest($method, $url, $body = null, array $options = array())
98+
public function sendRequest($method, $url, $body = null)
5999
{
60-
$this->createRequest($method, $url, $body, $options);
61-
62100
try {
63-
$this->response = $this->httpClient->send($this->request);
64-
} catch (BadResponseException $e) {
65-
$this->response = $e->getResponse();
101+
$this->send($method, $url, $body);
102+
} catch (HttpAdapterException $e) {
103+
if ($e->hasResponse()) {
104+
$this->response = $e->getResponse();
105+
}
66106

67107
if (null === $this->response) {
68108
throw $e;
69109
}
70110
}
71111

72112
if (null !== $this->responseStorage) {
73-
$this->responseStorage->writeRawContent($this->response->getBody(true));
113+
$this->responseStorage->writeRawContent($this->response->getBody()->getContents());
74114
}
75115
}
76116

77117
/**
78-
* @param string $name
79-
* @param string $value
118+
* @param string $method
119+
* @param string $uri With or without host
120+
* @param string|resource|array $body
80121
*/
81-
public function addRequestHeader($name, $value)
122+
private function send($method, $uri, $body = null)
82123
{
83-
if (isset($this->requestHeaders[$name])) {
84-
if (!is_array($this->requestHeaders[$name])) {
85-
$this->requestHeaders[$name] = array($this->requestHeaders[$name]);
86-
}
87-
$this->requestHeaders[$name][] = $value;
88-
} else {
89-
$this->requestHeaders[$name] = $value;
124+
if (!$this->hasHost($uri)) {
125+
$uri = rtrim($this->httpClient->getConfiguration()->getBaseUri(), '/') . '/' . ltrim($uri, '/');
90126
}
127+
$stream = new Stream('php://memory', 'rw');
128+
if ($body) {
129+
$stream->write($body);
130+
}
131+
$this->request = new Request($uri, $method, $stream, $this->requestHeaders);
132+
$this->response = $this->httpClient->send($uri, $method, $this->requestHeaders, $body);
133+
// Reset headers used for the HTTP request
134+
$this->requestHeaders = array();
91135
}
92136

93137
/**
94-
* @param string $name
95-
* @param string $value
96-
*/
97-
public function setRequestHeader($name, $value)
98-
{
99-
$this->removeRequestHeader($name);
100-
$this->addRequestHeader($name, $value);
101-
}
102-
103-
/**
104-
* @param Response $response
138+
* @param string $uri
139+
*
140+
* @return bool
105141
*/
106-
public function setResponse(Response $response)
142+
private function hasHost($uri)
107143
{
108-
$this->response = $response;
109-
if (null !== $this->responseStorage) {
110-
$this->responseStorage->writeRawContent($this->response->getBody(true));
111-
}
144+
return strpos($uri, '://') !== false;
112145
}
113146

114147
/**
115-
* @param string $method
116-
* @param string $uri With or without host
117-
* @param string|resource|array $body
118-
* @param array $options
148+
* @param string $name
149+
* @param string $value
119150
*/
120-
private function createRequest($method, $uri, $body = null, array $options = array())
151+
public function setRequestHeader($name, $value)
121152
{
122-
if (!$this->hasHost($uri)) {
123-
$uri = rtrim($this->httpClient->getBaseUrl(), '/') . '/' . ltrim($uri, '/');
124-
}
125-
126-
$this->request = $this->httpClient->createRequest($method, $uri, $this->requestHeaders, $body, $options);
127-
// Reset headers used for the HTTP request
128-
$this->requestHeaders = array();
153+
$this->removeRequestHeader($name);
154+
$this->addRequestHeader($name, $value);
129155
}
130156

131157
private function removeRequestHeader($headerName)
@@ -136,12 +162,18 @@ private function removeRequestHeader($headerName)
136162
}
137163

138164
/**
139-
* @param string $uri
140-
*
141-
* @return bool
165+
* @param string $name
166+
* @param string $value
142167
*/
143-
private function hasHost($uri)
168+
public function addRequestHeader($name, $value)
144169
{
145-
return strpos($uri, '://') !== false;
170+
if (isset($this->requestHeaders[$name])) {
171+
if (!is_array($this->requestHeaders[$name])) {
172+
$this->requestHeaders[$name] = array($this->requestHeaders[$name]);
173+
}
174+
$this->requestHeaders[$name][] = $value;
175+
} else {
176+
$this->requestHeaders[$name] = $value;
177+
}
146178
}
147179
}

0 commit comments

Comments
 (0)