lithium /tests/cases/action/ResponseTest.php

Language PHP Lines 224
MD5 Hash c74800f00a5b277f9b3aed0ec2e1e67b Estimated Cost $4,178 (why?)
Repository git://github.com/UnionOfRAD/lithium.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
 * Lithium: the most rad php framework
 *
 * @copyright     Copyright 2016, Union of RAD (http://union-of-rad.org)
 * @license       http://opensource.org/licenses/bsd-license.php The BSD License
 */

namespace lithium\tests\cases\action;

use lithium\action\Response;
use lithium\tests\mocks\action\MockResponse;

class ResponseTest extends \lithium\test\Unit {

	public $response = null;

	public function setUp() {
		$this->response = new MockResponse(array('init' => false));
	}

	public function testTypeManipulation() {
		$this->assertEqual('html', $this->response->type());
		$this->assertEqual('html', $this->response->type('html'));
		$this->assertEqual('json', $this->response->type('json'));
		$this->assertEqual('json', $this->response->type());
		$this->assertEqual(false, $this->response->type(false));
		$this->assertEqual(false, $this->response->type());
	}

	public function testResponseRenderString() {
		$this->response->body = 'Document body';

		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$this->assertIdentical('Document body', $result);
		$this->assertIdentical(array('HTTP/1.1 200 OK'), $this->response->testHeaders);
	}

	public function testResponseRenderJson() {
		$this->response->type('json');
		$this->response->body[] = '{"message": "Hello World"}';

		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$this->assertIdentical('{"message": "Hello World"}', $result);
		$this->assertIdentical('HTTP/1.1 200 OK', $this->response->testHeaders[0]);
	}

	public function testResponseRenderWithCookies() {
		$this->response->cookies(array(
			'Name' => array('value' => 'Ali', 'domain' => '.li3.me', 'secure' => true),
			'Destination' => array('value' => 'The Future', 'expires' => 'Oct 21 2015 4:29 PM PDT')
		));
		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$expected = array(
			'HTTP/1.1 200 OK',
			'Set-Cookie: Name=Ali; Domain=.li3.me; Secure',
			'Set-Cookie: Destination=The%20Future; Expires=Wed, 21-Oct-2015 23:29:00 GMT',
		);
		$this->assertIdentical($expected, $this->response->testHeaders);
	}

	public function testResponseToString() {
		$this->response->type(false);
		$this->response->body = 'Document body';

		ob_start();
		echo $this->response;
		$result = ob_get_clean();
		$this->assertIdentical('Document body', $result);
		$this->assertIdentical(array('HTTP/1.1 200 OK'), $this->response->testHeaders);
	}

	public function testResponseCaching() {
		$this->response->body = 'Document body';

		$time = time();
		$expires = strtotime("@{$time} +1 hour");
		$this->response->cache($expires);
		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$headers = array (
			'HTTP/1.1 200 OK',
			'Expires: ' . gmdate('D, d M Y H:i:s', $expires) . ' GMT',
			'Cache-Control: max-age=' . ($expires - $time),
			'Pragma: cache'
		);
		$this->assertIdentical($headers, $this->response->testHeaders);

		$expires = strtotime("@{$time} +2 hours");
		$this->response->cache($expires);
		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$headers = array (
			'HTTP/1.1 200 OK',
			'Expires: ' . gmdate('D, d M Y H:i:s', $expires) . ' GMT',
			'Cache-Control: max-age=' . ($expires - time()),
			'Pragma: cache'
		);
		$this->assertIdentical($headers, $this->response->testHeaders);

		$this->response->body = 'Created';
		$this->response->status(201);
		$this->response->cache(false);

		$result = $this->response->headers();

		$expected = array(
			'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
			'Cache-Control: no-store, no-cache, must-revalidate',
			'Cache-Control: post-check=0, pre-check=0',
			'Cache-Control: max-age=0',
			'Pragma: no-cache'
		);
		$this->assertIdentical($expected, $result);

		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$this->assertIdentical('Created', $result);

		$headers = array (
			'HTTP/1.1 201 Created',
			'Expires: Mon, 26 Jul 1997 05:00:00 GMT',
			'Cache-Control: no-store, no-cache, must-revalidate',
			'Cache-Control: post-check=0, pre-check=0',
			'Cache-Control: max-age=0',
			'Pragma: no-cache'
		);
		$this->assertIdentical($headers, $this->response->testHeaders);
	}

	/**
	 * Tests various methods of specifying HTTP status codes.
	 */
	public function testStatusCodes() {
		$this->response->status('Created');
		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$this->assertEqual(array('HTTP/1.1 201 Created'), $this->response->testHeaders);

		$this->response->status('See Other');
		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$this->assertEqual(array('HTTP/1.1 303 See Other'), $this->response->testHeaders);

		$this->response->status('foobar');
		ob_start();
		$this->response->render();
		$result = ob_get_clean();
		$expected = array('HTTP/1.1 500 Internal Server Error');
		$this->assertEqual($expected, $this->response->testHeaders);
	}

	/**
	 * Tests custom header add-ons, like 'download'.
	 */
	public function testDownloadMagicHeader() {
		$response = $this->response;

		$this->assertException('/deprecated/', function() use ($response) {
			$response->headers('download', 'report.csv');
		});
	}

	/**
	 * Tests location headers.
	 */
	public function testLocationHeader() {
		$this->response = new MockResponse();
		$this->response->status(301);
		$this->response->headers('Location', '/');
		ob_start();
		$this->response->render();
		ob_get_clean();

		$headers = array('HTTP/1.1 301 Moved Permanently', 'Location: /');
		$this->assertEqual($headers, $this->response->testHeaders);

		$this->response = new MockResponse();
		$this->response->headers('Location', '/');
		ob_start();
		$this->response->render();
		ob_get_clean();

		$headers = array('HTTP/1.1 302 Found', 'Location: /');
		$this->assertEqual($headers, $this->response->testHeaders);

		$this->response = new Response(array(
			'classes' => array('router' => __CLASS__),
			'location' => array('controller' => 'foo_bar', 'action' => 'index')
		));
		$this->assertEqual(array('Location: /foo_bar'), $this->response->headers());
	}

	/**
	 * Tests that, when a location is assigned without a status code being set, that the status code
	 * will be automatically set to 302 when the response is rendered.
	 */
	public function testBrowserRedirection() {
		$this->response = new MockResponse(array('location' => '/'));
		ob_start();
		$this->response->render();
		ob_get_clean();
		$this->assertEqual('HTTP/1.1 302 Found', $this->response->status());
	}

	public static function match($url) {
		if ($url === array('controller' => 'foo_bar', 'action' => 'index')) {
			return '/foo_bar';
		}
	}
}

?>
Back to Top