Skip to content

Commit 4dfd0b7

Browse files
committed
Improve HTTP\Request test coverage
1 parent 90eb2e0 commit 4dfd0b7

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

tests/Http/HttpRequestTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ public function tearDown()
99
{
1010
m::close();
1111
}
12+
13+
14+
public function testInstanceMethod()
15+
{
16+
$request = Request::create('', 'GET');
17+
$this->assertTrue($request === $request->instance());
18+
}
19+
20+
21+
public function testRootMethod()
22+
{
23+
$request = Request::create('http://example.com/foo/bar/script.php?test');
24+
$this->assertEquals('http://example.com', $request->root());
25+
}
1226

1327

1428
public function testPathMethod()
@@ -19,6 +33,13 @@ public function testPathMethod()
1933
$request = Request::create('/foo/bar', 'GET');
2034
$this->assertEquals('foo/bar', $request->path());
2135
}
36+
37+
38+
public function testDecodedPathMethod()
39+
{
40+
$request = Request::create('/foo%20bar');
41+
$this->assertEquals('foo bar', $request->decodedPath());
42+
}
2243

2344

2445
public function testSegmentMethod()
@@ -69,11 +90,30 @@ public function testIsMethod()
6990
$this->assertTrue($request->is('foo*'));
7091
$this->assertFalse($request->is('bar*'));
7192
$this->assertTrue($request->is('*bar*'));
93+
$this->assertTrue($request->is('bar*', 'foo*', 'baz'));
7294

7395
$request = Request::create('/', 'GET');
7496

7597
$this->assertTrue($request->is('/'));
7698
}
99+
100+
101+
public function testAjaxMethod()
102+
{
103+
$request = Request::create('/', 'GET');
104+
$this->assertFalse($request->ajax());
105+
$request = Request::create('/', 'GET', array(), array(), array(), array('HTTP_X_REQUESTED_WITH' => 'XMLHttpRequest'), '{}');
106+
$this->assertTrue($request->ajax());
107+
}
108+
109+
110+
public function testSecureMethod()
111+
{
112+
$request = Request::create('http://example.com', 'GET');
113+
$this->assertFalse($request->secure());
114+
$request = Request::create('https://example.com', 'GET');
115+
$this->assertTrue($request->secure());
116+
}
77117

78118

79119
public function testHasMethod()
@@ -122,6 +162,8 @@ public function testQueryMethod()
122162
$request = Request::create('/', 'GET', array('name' => 'Taylor'));
123163
$this->assertEquals('Taylor', $request->query('name'));
124164
$this->assertEquals('Bob', $request->query('foo', 'Bob'));
165+
$all = $request->query(null);
166+
$this->assertEquals('Taylor', $all['name']);
125167
}
126168

127169

@@ -130,6 +172,16 @@ public function testCookieMethod()
130172
$request = Request::create('/', 'GET', array(), array('name' => 'Taylor'));
131173
$this->assertEquals('Taylor', $request->cookie('name'));
132174
$this->assertEquals('Bob', $request->cookie('foo', 'Bob'));
175+
$all = $request->cookie(null);
176+
$this->assertEquals('Taylor', $all['name']);
177+
}
178+
179+
180+
public function testHasCookieMethod()
181+
{
182+
$request = Request::create('/', 'GET', array(), array('foo' => 'bar'));
183+
$this->assertTrue($request->hasCookie('foo'));
184+
$this->assertFalse($request->hasCookie('qu'));
133185
}
134186

135187

@@ -173,6 +225,8 @@ public function testServerMethod()
173225
$request = Request::create('/', 'GET', array(), array(), array(), array('foo' => 'bar'));
174226
$this->assertEquals('bar', $request->server('foo'));
175227
$this->assertEquals('bar', $request->server('foo.doesnt.exist', 'bar'));
228+
$all = $request->server(null);
229+
$this->assertEquals('bar', $all['foo']);
176230
}
177231

178232

@@ -200,6 +254,8 @@ public function testHeaderMethod()
200254
{
201255
$request = Request::create('/', 'GET', array(), array(), array(), array('HTTP_DO_THIS' => 'foo'));
202256
$this->assertEquals('foo', $request->header('do-this'));
257+
$all = $request->header(null);
258+
$this->assertEquals('foo', $all['do-this'][0]);
203259
}
204260

205261

@@ -260,9 +316,23 @@ public function testFormatReturnsAcceptableFormat()
260316
{
261317
$request = Request::create('/', 'GET', array(), array(), array(), array('HTTP_ACCEPT' => 'application/json'));
262318
$this->assertEquals('json', $request->format());
319+
$this->assertTrue($request->wantsJson());
263320

264321
$request = Request::create('/', 'GET', array(), array(), array(), array('HTTP_ACCEPT' => 'application/atom+xml'));
265322
$this->assertEquals('atom', $request->format());
323+
$this->assertFalse($request->wantsJson());
324+
325+
$request = Request::create('/', 'GET', array(), array(), array(), array('HTTP_ACCEPT' => 'is/not/known'));
326+
$this->assertEquals('html', $request->format());
327+
$this->assertEquals('foo', $request->format('foo'));
328+
}
329+
330+
331+
public function testSessionMethod()
332+
{
333+
$this->setExpectedException('RuntimeException');
334+
$request = Request::create('/', 'GET');
335+
$request->session();
266336
}
267337

268338
}

0 commit comments

Comments
 (0)