22
33namespace 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 ;
812use Behat \Gherkin \Node \PyStringNode ;
913
1014class 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