Skip to content

Commit f9b1497

Browse files
committed
Merge pull request #636 from stof/header-assertions
Add support for header assertions
2 parents 87a18d7 + e5a3632 commit f9b1497

4 files changed

Lines changed: 270 additions & 0 deletions

File tree

src/Session.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,27 @@ public function getResponseHeaders()
175175
return $this->driver->getResponseHeaders();
176176
}
177177

178+
/**
179+
* Returns specific response header.
180+
*
181+
* @param string $name
182+
*
183+
* @return string|null
184+
*/
185+
public function getResponseHeader($name)
186+
{
187+
$headers = $this->driver->getResponseHeaders();
188+
189+
$name = strtolower($name);
190+
$headers = array_change_key_case($headers, CASE_LOWER);
191+
192+
if (!isset($headers[$name])) {
193+
return null;
194+
}
195+
196+
return is_array($headers[$name]) ? $headers[$name][0] : $headers[$name];
197+
}
198+
178199
/**
179200
* Sets cookie.
180201
*

src/WebAssert.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,106 @@ public function statusCodeNotEquals($code)
145145
$this->assert(intval($code) !== intval($actual), $message);
146146
}
147147

148+
/**
149+
* Checks that current response header equals value.
150+
*
151+
* @param string $name
152+
* @param string $value
153+
*
154+
* @throws ExpectationException
155+
*/
156+
public function responseHeaderEquals($name, $value)
157+
{
158+
$actual = $this->session->getResponseHeader($name);
159+
$message = sprintf('Current response header "%s" is "%s", but "%s" expected.', $name, $actual, $value);
160+
161+
$this->assert($value === $actual, $message);
162+
}
163+
164+
/**
165+
* Checks that current response header does not equal value.
166+
*
167+
* @param string $name
168+
* @param string $value
169+
*
170+
* @throws ExpectationException
171+
*/
172+
public function responseHeaderNotEquals($name, $value)
173+
{
174+
$actual = $this->session->getResponseHeader($name);
175+
$message = sprintf('Current response header "%s" is "%s", but should not be.', $name, $actual, $value);
176+
177+
$this->assert($value !== $actual, $message);
178+
}
179+
180+
/**
181+
* Checks that current response header contains value.
182+
*
183+
* @param string $name
184+
* @param string $value
185+
*
186+
* @throws ExpectationException
187+
*/
188+
public function responseHeaderContains($name, $value)
189+
{
190+
$actual = $this->session->getResponseHeader($name);
191+
$message = sprintf('The text "%s" was not found anywhere in the "%s" response header.', $value, $name);
192+
193+
$this->assert(false !== stripos($actual, $value), $message);
194+
}
195+
196+
/**
197+
* Checks that current response header does not contain value.
198+
*
199+
* @param string $name
200+
* @param string $value
201+
*
202+
* @throws ExpectationException
203+
*/
204+
public function responseHeaderNotContains($name, $value)
205+
{
206+
$actual = $this->session->getResponseHeader($name);
207+
$message = sprintf('The text "%s" was found in the "%s" response header, but it should not.', $value, $name);
208+
209+
$this->assert(false === stripos($actual, $value), $message);
210+
}
211+
212+
/**
213+
* Checks that current response header matches regex.
214+
*
215+
* @param string $name
216+
* @param string $regex
217+
*
218+
* @throws ExpectationException
219+
*/
220+
public function responseHeaderMatches($name, $regex)
221+
{
222+
$actual = $this->session->getResponseHeader($name);
223+
$message = sprintf('The pattern "%s" was not found anywhere in the "%s" response header.', $regex, $name);
224+
225+
$this->assert((bool) preg_match($regex, $actual), $message);
226+
}
227+
228+
/**
229+
* Checks that current response header does not match regex.
230+
*
231+
* @param string $name
232+
* @param string $regex
233+
*
234+
* @throws ExpectationException
235+
*/
236+
public function responseHeaderNotMatches($name, $regex)
237+
{
238+
$actual = $this->session->getResponseHeader($name);
239+
$message = sprintf(
240+
'The pattern "%s" was found in the text of the "%s" response header, but it should not.',
241+
$regex,
242+
$name
243+
);
244+
245+
$this->assert(!preg_match($regex, $actual), $message);
246+
}
247+
148248
/**
149249
* Checks that current page contains text.
150250
*

tests/SessionTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,29 @@ public function testGetResponseHeaders()
130130
$this->assertEquals($ret, $this->session->getResponseHeaders());
131131
}
132132

133+
/**
134+
* @dataProvider provideResponseHeader
135+
*/
136+
public function testGetResponseHeader($expected, $name, array $headers)
137+
{
138+
$this->driver->expects($this->once())
139+
->method('getResponseHeaders')
140+
->willReturn($headers);
141+
142+
$this->assertSame($expected, $this->session->getResponseHeader($name));
143+
}
144+
145+
public function provideResponseHeader()
146+
{
147+
return array(
148+
array('test', 'Mink', array('Mink' => 'test')),
149+
array('test', 'mink', array('Mink' => 'test')),
150+
array('test', 'Mink', array('mink' => 'test')),
151+
array('test', 'Mink', array('Mink' => array('test', 'test2'))),
152+
array(null, 'other', array('Mink' => 'test')),
153+
);
154+
}
155+
133156
public function testSetCookie()
134157
{
135158
$this->driver->expects($this->once())

tests/WebAssertTest.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,132 @@ public function testStatusCodeNotEquals()
162162
);
163163
}
164164

165+
public function testResponseHeaderEquals()
166+
{
167+
$this->session
168+
->expects($this->any())
169+
->method('getResponseHeader')
170+
->will($this->returnValueMap(
171+
array(
172+
array('foo', 'bar'),
173+
array('bar', 'baz'),
174+
)
175+
));
176+
177+
$this->assertCorrectAssertion('responseHeaderEquals', array('foo', 'bar'));
178+
$this->assertWrongAssertion(
179+
'responseHeaderEquals',
180+
array('bar', 'foo'),
181+
'Behat\\Mink\\Exception\\ExpectationException',
182+
'Current response header "bar" is "baz", but "foo" expected.'
183+
);
184+
}
185+
186+
public function testResponseHeaderNotEquals()
187+
{
188+
$this->session
189+
->expects($this->any())
190+
->method('getResponseHeader')
191+
->will($this->returnValueMap(
192+
array(
193+
array('foo', 'bar'),
194+
array('bar', 'baz'),
195+
)
196+
));
197+
198+
$this->assertCorrectAssertion('responseHeaderNotEquals', array('foo', 'baz'));
199+
$this->assertWrongAssertion(
200+
'responseHeaderNotEquals',
201+
array('bar', 'baz'),
202+
'Behat\\Mink\\Exception\\ExpectationException',
203+
'Current response header "bar" is "baz", but should not be.'
204+
);
205+
}
206+
207+
public function testResponseHeaderContains()
208+
{
209+
$this->session
210+
->expects($this->any())
211+
->method('getResponseHeader')
212+
->will($this->returnValueMap(
213+
array(
214+
array('foo', 'bar'),
215+
array('bar', 'baz'),
216+
)
217+
));
218+
219+
$this->assertCorrectAssertion('responseHeaderContains', array('foo', 'ba'));
220+
$this->assertWrongAssertion(
221+
'responseHeaderContains',
222+
array('bar', 'bz'),
223+
'Behat\\Mink\\Exception\\ExpectationException',
224+
'The text "bz" was not found anywhere in the "bar" response header.'
225+
);
226+
}
227+
228+
public function testResponseHeaderNotContains()
229+
{
230+
$this->session
231+
->expects($this->any())
232+
->method('getResponseHeader')
233+
->will($this->returnValueMap(
234+
array(
235+
array('foo', 'bar'),
236+
array('bar', 'baz'),
237+
)
238+
));
239+
240+
$this->assertCorrectAssertion('responseHeaderNotContains', array('foo', 'bz'));
241+
$this->assertWrongAssertion(
242+
'responseHeaderNotContains',
243+
array('bar', 'ba'),
244+
'Behat\\Mink\\Exception\\ExpectationException',
245+
'The text "ba" was found in the "bar" response header, but it should not.'
246+
);
247+
}
248+
249+
public function testResponseHeaderMatches()
250+
{
251+
$this->session
252+
->expects($this->any())
253+
->method('getResponseHeader')
254+
->will($this->returnValueMap(
255+
array(
256+
array('foo', 'bar'),
257+
array('bar', 'baz'),
258+
)
259+
));
260+
261+
$this->assertCorrectAssertion('responseHeaderMatches', array('foo', '/ba(.*)/'));
262+
$this->assertWrongAssertion(
263+
'responseHeaderMatches',
264+
array('bar', '/b[^a]/'),
265+
'Behat\\Mink\\Exception\\ExpectationException',
266+
'The pattern "/b[^a]/" was not found anywhere in the "bar" response header.'
267+
);
268+
}
269+
270+
public function testResponseHeaderNotMatches()
271+
{
272+
$this->session
273+
->expects($this->any())
274+
->method('getResponseHeader')
275+
->will($this->returnValueMap(
276+
array(
277+
array('foo', 'bar'),
278+
array('bar', 'baz'),
279+
)
280+
));
281+
282+
$this->assertCorrectAssertion('responseHeaderNotMatches', array('foo', '/bz/'));
283+
$this->assertWrongAssertion(
284+
'responseHeaderNotMatches',
285+
array('bar', '/b[ab]z/'),
286+
'Behat\\Mink\\Exception\\ExpectationException',
287+
'The pattern "/b[ab]z/" was found in the text of the "bar" response header, but it should not.'
288+
);
289+
}
290+
165291
public function testPageTextContains()
166292
{
167293
$page = $this->getMockBuilder('Behat\\Mink\\Element\\DocumentElement')

0 commit comments

Comments
 (0)