Skip to content

Commit 46614b5

Browse files
committed
Merge pull request php-vcr#92 from chregu/add-auth
Add auth
2 parents 9478043 + 8f82834 commit 46614b5

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

src/VCR/LibraryHooks/SoapHook.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function __construct(AbstractCodeTransform $codeTransformer, StreamProces
6262
*
6363
* @return string SOAP response.
6464
*/
65-
public function doRequest($request, $location, $action, $version, $one_way = 0)
65+
public function doRequest($request, $location, $action, $version, $one_way = 0, $options = array())
6666
{
6767
if ($this->status === self::DISABLED) {
6868
throw new VCRException('Hook must be enabled.', VCRException::LIBRARY_HOOK_DISABLED);
@@ -73,6 +73,9 @@ public function doRequest($request, $location, $action, $version, $one_way = 0)
7373
$vcrRequest->setHeader('Content-Type', $contentType . '; charset=utf-8; action="' . $action . '"');
7474
$vcrRequest->setBody($request);
7575

76+
if (isset($options['login'])) {
77+
$vcrRequest->setAuth($options['login'], $options['password']);
78+
}
7679
$requestCallback = self::$requestCallback;
7780
/**
7881
* @var \VCR\Response $response

src/VCR/LibraryHooks/StreamWrapperHook.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class StreamWrapperHook implements LibraryHook
3131
*/
3232
protected $response;
3333

34+
public $context;
35+
3436
/**
3537
* @inheritDoc
3638
*/
@@ -81,8 +83,24 @@ public function isEnabled()
8183
public function stream_open($path, $mode, $options, &$opened_path)
8284
{
8385
$requestCallback = self::$requestCallback;
84-
$this->response = $requestCallback(new Request('GET', $path));
8586

87+
if ($this->context) {
88+
$context = stream_context_get_options($this->context);
89+
} else {
90+
$context = array();
91+
}
92+
93+
$headers = array();
94+
95+
//parse headers added to the stream_context
96+
if (isset($context['http']) && isset($context['http']['header']) ) {
97+
$cheaders = $context['http']['header'];
98+
$parsedheaders = array_map(function($x) { return array_map("trim", explode(":", $x, 2)); }, array_filter(array_map("trim", explode("\n", $cheaders))));
99+
foreach ($parsedheaders as $header) {
100+
$headers[$header[0]] = $header[1];
101+
}
102+
}
103+
$this->response = $requestCallback(new Request('GET', $path, $headers));
86104
return true;
87105
}
88106

@@ -153,6 +171,19 @@ public function stream_eof()
153171
* @return array See stat().
154172
*/
155173
public function stream_stat()
174+
{
175+
return array();
176+
}
177+
178+
/**
179+
* Retrieve information about a file resource.
180+
*
181+
* @link http://www.php.net/manual/en/streamwrapper.url-stat.php
182+
*
183+
* @return array See stat().
184+
*/
185+
186+
public function url_stat($path,$flags)
156187
{
157188
return array();
158189
}
@@ -217,6 +248,5 @@ public function stream_metadata($path, $option, $var)
217248
*/
218249
public function __destruct()
219250
{
220-
self::$requestCallback = null;
221251
}
222252
}

src/VCR/Request.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ public function setBody($body)
271271
$this->body = $body;
272272
}
273273

274+
/**
275+
* Sets the auth credentials.
276+
*
277+
* @param string $username
278+
* @param string $password
279+
*/
280+
public function setAuth($username, $password)
281+
{
282+
$this->setHeader('Authorization', 'Basic ' . base64_encode($username . ':' . $password));
283+
}
284+
274285
/**
275286
* @param array $curlOptions
276287
*/

src/VCR/Util/SoapClient.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ class SoapClient extends \SoapClient
1515
*/
1616
protected $soapHook;
1717

18+
protected $options = array();
19+
20+
public function __construct($wsdl, $options = array()) {
21+
$this->options = $options;
22+
parent::__construct($wsdl, $options);
23+
}
24+
1825
/**
1926
* Performs (and may intercepts) SOAP request over HTTP.
2027
*
@@ -33,7 +40,7 @@ public function __doRequest($request, $location, $action, $version, $one_way = 0
3340
$soapHook = $this->getLibraryHook();
3441

3542
if ($soapHook->isEnabled()) {
36-
$response = $soapHook->doRequest($request, $location, $action, $version, $one_way);
43+
$response = $soapHook->doRequest($request, $location, $action, $version, $one_way, $this->options);
3744
} else {
3845
$response = $this->realDoRequest($request, $location, $action, $version, $one_way);
3946
}

0 commit comments

Comments
 (0)