Skip to content

Commit 394737a

Browse files
committed
Changes request matchers to match on identity instead of patterns.
New requests must match stored requests exactly. This change is necessary because of many misunderstandings why a request is not matched.
1 parent 7bf0daa commit 394737a

File tree

3 files changed

+44
-30
lines changed

3 files changed

+44
-30
lines changed

src/VCR/RequestMatcher.php

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static function matchMethod(Request $first, Request $second)
3030
*/
3131
public static function matchUrl(Request $first, Request $second)
3232
{
33-
return !((null !== $first->getPath()) and ((string) $first->getPath() != (string) $second->getPath()));
33+
return $first->getPath() === $second->getPath();
3434
}
3535

3636
/**
@@ -43,12 +43,7 @@ public static function matchUrl(Request $first, Request $second)
4343
*/
4444
public static function matchHost(Request $first, Request $second)
4545
{
46-
if (null !== $first->getHost()
47-
&& !preg_match('#'.str_replace('#', '\\#', $first->getHost()).'#i', $second->getHost())) {
48-
return false;
49-
}
50-
51-
return true;
46+
return $first->getHost() === $second->getHost();
5247
}
5348

5449
/**
@@ -61,15 +56,9 @@ public static function matchHost(Request $first, Request $second)
6156
*/
6257
public static function matchHeaders(Request $first, Request $second)
6358
{
64-
$firstHeaders = $first->getHeaders();
65-
foreach ($second->getHeaders() as $key => $pattern) {
66-
if (!array_key_exists($key, $firstHeaders)
67-
|| $pattern !== $firstHeaders[$key]) {
68-
return false;
69-
}
70-
}
59+
// Use array_filter to ignore headers which are null.
7160

72-
return true;
61+
return array_filter($first->getHeaders()) === array_filter($second->getHeaders());
7362
}
7463

7564
/**
@@ -82,11 +71,7 @@ public static function matchHeaders(Request $first, Request $second)
8271
*/
8372
public static function matchBody(Request $first, Request $second)
8473
{
85-
if (null !== $first->getBody() && (string) $first->getBody() != (string) $second->getBody() ) {
86-
return false;
87-
}
88-
89-
return true;
74+
return $first->getBody() === $second->getBody();
9075
}
9176

9277
/**
@@ -99,11 +84,7 @@ public static function matchBody(Request $first, Request $second)
9984
*/
10085
public static function matchPostFields(Request $first, Request $second)
10186
{
102-
if (null !== $first->getPostFields() && $first->getPostFields() != $second->getPostFields()) {
103-
return false;
104-
}
105-
106-
return true;
87+
return $first->getPostFields() === $second->getPostFields();
10788
}
10889

10990
/**
@@ -116,9 +97,6 @@ public static function matchPostFields(Request $first, Request $second)
11697
*/
11798
public static function matchQueryString(Request $first, Request $second)
11899
{
119-
if (null !== $first->getQuery() && $first->getQuery() != $second->getQuery()) {
120-
return false;
121-
}
122-
return true;
100+
return $first->getQuery() === $second->getQuery();
123101
}
124102
}

tests/VCR/RequestMatcherTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,19 @@ public function testMatchingHeaders()
6161
$this->assertFalse(RequestMatcher::matchHeaders($first, $second));
6262
}
6363

64+
public function testHeaderMatchingDisallowsMissingHeaders()
65+
{
66+
$first = new Request('GET', 'http://example.com', array('Accept' => 'Everything', 'MyHeader' => 'value'));
67+
$second = new Request('GET', 'http://example.com', array('Accept' => 'Everything'));
68+
69+
$this->assertFalse(RequestMatcher::matchHeaders($first, $second));
70+
71+
$first = new Request('GET', 'http://example.com', array('Accept' => 'Everything'));
72+
$second = new Request('GET', 'http://example.com', array('Accept' => 'Everything', 'MyHeader' => 'value'));
73+
74+
$this->assertFalse(RequestMatcher::matchHeaders($first, $second));
75+
}
76+
6477
public function testHeaderMatchingAllowsEmptyVals()
6578
{
6679
$first = new Request('GET', 'http://example.com', array('Accept' => null, 'Content-Type' => 'application/json'));

tests/fixtures/unittest_soap_test

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
headers:
66
Host: wsf.cdyne.com
77
Content-Type: 'application/soap+xml; charset=utf-8; action="http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP"'
8-
Content-Length: '260'
98
body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ns1=\"http://ws.cdyne.com/WeatherWS/\"><env:Body><ns1:GetCityWeatherByZIP><ns1:ZIP>10013</ns1:ZIP></ns1:GetCityWeatherByZIP></env:Body></env:Envelope>\n"
109
response:
1110
status: 200
@@ -20,3 +19,27 @@
2019
Date: 'Fri, 25 Apr 2014 09:22:59 GMT'
2120
Content-Length: '756'
2221
body: '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/"><GetCityWeatherByZIPResult><Success>true</Success><ResponseText>City Found</ResponseText><State>NY</State><City>New York</City><WeatherStationCity>White Plains</WeatherStationCity><WeatherID>3</WeatherID><Description>Mostly Cloudy</Description><Temperature>1337</Temperature><RelativeHumidity>75</RelativeHumidity><Wind>NE7</Wind><Pressure>30.12F</Pressure><Visibility /><WindChill /><Remarks /></GetCityWeatherByZIPResult></GetCityWeatherByZIPResponse></soap:Body></soap:Envelope>'
22+
-
23+
request:
24+
method: POST
25+
url: 'http://wsf.cdyne.com/WeatherWS/Weather.asmx'
26+
headers:
27+
Host: wsf.cdyne.com
28+
Content-Type: 'application/soap+xml; charset=utf-8; action="http://ws.cdyne.com/WeatherWS/GetCityWeatherByZIP"'
29+
body: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ns1=\"http://ws.cdyne.com/WeatherWS/\"><env:Body><ns1:GetCityWeatherByZIP><ns1:ZIP>10013</ns1:ZIP></ns1:GetCityWeatherByZIP></env:Body></env:Envelope>\n"
30+
response:
31+
status:
32+
http_version: '1.1'
33+
code: '200'
34+
message: OK
35+
headers:
36+
Cache-Control: no-cache
37+
Pragma: no-cache
38+
Content-Type: 'application/soap+xml; charset=utf-8'
39+
Expires: '-1'
40+
Server: Microsoft-IIS/7.5
41+
X-AspNet-Version: 2.0.50727
42+
X-Powered-By: ASP.NET
43+
Date: 'Sat, 21 Feb 2015 14:09:36 GMT'
44+
Content-Length: '753'
45+
body: '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><GetCityWeatherByZIPResponse xmlns="http://ws.cdyne.com/WeatherWS/"><GetCityWeatherByZIPResult><Success>true</Success><ResponseText>City Found</ResponseText><State>NY</State><City>New York</City><WeatherStationCity>White Plains</WeatherStationCity><WeatherID>15</WeatherID><Description>N/A</Description><Temperature>63</Temperature><RelativeHumidity>87</RelativeHumidity><Wind>E7</Wind><Pressure>29.97S</Pressure><Visibility /><WindChill /><Remarks /></GetCityWeatherByZIPResult></GetCityWeatherByZIPResponse></soap:Body></soap:Envelope>'

0 commit comments

Comments
 (0)