diff --git a/libraries/joomla/application/web.php b/libraries/joomla/application/web.php index 3e1bf6428d03b..a8b6ce9116fda 100644 --- a/libraries/joomla/application/web.php +++ b/libraries/joomla/application/web.php @@ -565,8 +565,8 @@ public function redirect($url, $status = 303) } // All other cases use the more efficient HTTP header for redirection. - $this->header($this->responseMap[$status]); - $this->header('Location: ' . $url); + $this->setHeader('Status', $status, true); + $this->setHeader('Location', $url, true); } } @@ -719,7 +719,7 @@ public function sendHeaders() if ('status' == strtolower($header['name'])) { // 'status' headers indicate an HTTP status, and need to be handled slightly differently - $this->header('HTTP/1.1 ' . $header['value'], null, (int) $header['value']); + $this->header('HTTP/1.1 ' . (int) $header['value'], true); } else { diff --git a/tests/unit/suites/libraries/cms/application/JApplicationCmsTest.php b/tests/unit/suites/libraries/cms/application/JApplicationCmsTest.php index 918d7c4ddd355..2db2648ba4379 100644 --- a/tests/unit/suites/libraries/cms/application/JApplicationCmsTest.php +++ b/tests/unit/suites/libraries/cms/application/JApplicationCmsTest.php @@ -432,7 +432,7 @@ public function testRedirect() $this->class->redirect($url, false); $this->assertEquals( - array('HTTP/1.1 303 See other', true, null), + array('HTTP/1.1 303', true, null), $this->class->headers[0] ); @@ -501,7 +501,7 @@ public function testRedirectLegacy() ); $this->assertEquals( - array('HTTP/1.1 303 See other', true, null), + array('HTTP/1.1 303', true, null), $this->class->headers[0] ); @@ -566,7 +566,7 @@ public function testRedirectLegacyWithEmptyMessageAndEmptyStatus() // The redirect gives a 303 error code $this->assertEquals( - array('HTTP/1.1 303 See other', true, null), + array('HTTP/1.1 303', true, null), $this->class->headers[0] ); @@ -681,7 +681,7 @@ public function testRedirectWithMoved() $this->class->redirect($url, true); $this->assertEquals( - array('HTTP/1.1 301 Moved Permanently', true, null), + array('HTTP/1.1 301', true, null), $this->class->headers[0] ); diff --git a/tests/unit/suites/libraries/joomla/application/JApplicationWebTest.php b/tests/unit/suites/libraries/joomla/application/JApplicationWebTest.php index f8916e238b6a9..1275a161ebb25 100644 --- a/tests/unit/suites/libraries/joomla/application/JApplicationWebTest.php +++ b/tests/unit/suites/libraries/joomla/application/JApplicationWebTest.php @@ -1086,7 +1086,137 @@ public function testRedirect() $this->class->redirect($url, false); $this->assertEquals( - array('HTTP/1.1 303 See other', true, null), + array('HTTP/1.1 303', true, null), + $this->class->headers[0] + ); + + $this->assertEquals( + array('Location: ' . $base . $url, true, null), + $this->class->headers[1] + ); + + $this->assertEquals( + array('Content-Type: text/html; charset=utf-8', true, null), + $this->class->headers[2] + ); + + $this->assertRegexp('/Expires/',$this->class->headers[3][0]); + + $this->assertRegexp('/Last-Modified/',$this->class->headers[4][0]); + + $this->assertEquals( + array('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0', true, null), + $this->class->headers[5] + ); + + $this->assertEquals( + array('Pragma: no-cache', true, null), + $this->class->headers[6] + ); + } + + /** + * Tests the JApplicationWeb::redirect method. + * + * @return void + * + * @since 11.3 + */ + public function testRedirectWithExistingStatusCode1() + { + // Case Sensitive: status + $this->class->setHeader('status', 201); + + $base = 'http://mydomain.com/'; + $url = 'index.php'; + + // Inject the client information. + TestReflection::setValue( + $this->class, + 'client', + (object) array( + 'engine' => JApplicationWebClient::GECKO, + ) + ); + + // Inject the internal configuration. + $config = new Registry; + $config->set('uri.base.full', $base); + + TestReflection::setValue($this->class, 'config', $config); + + $this->class->redirect($url, false); + + // It has two statuses, but the second status will be the final status + $this->assertEquals( + array('HTTP/1.1 201', true, null), + $this->class->headers[0] + ); + + $this->assertEquals( + array('HTTP/1.1 303', true, null), + $this->class->headers[1] + ); + + $this->assertEquals( + array('Location: ' . $base . $url, true, null), + $this->class->headers[2] + ); + + $this->assertEquals( + array('Content-Type: text/html; charset=utf-8', true, null), + $this->class->headers[3] + ); + + $this->assertRegexp('/Expires/',$this->class->headers[4][0]); + + $this->assertRegexp('/Last-Modified/',$this->class->headers[5][0]); + + $this->assertEquals( + array('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0', true, null), + $this->class->headers[6] + ); + + $this->assertEquals( + array('Pragma: no-cache', true, null), + $this->class->headers[7] + ); + } + + /** + * Tests the JApplicationWeb::redirect method. + * + * @return void + * + * @since 11.3 + */ + public function testRedirectWithExistingStatusCode2() + { + // Case Sensitive: Status + $this->class->setHeader('Status', 201); + + $base = 'http://mydomain.com/'; + $url = 'index.php'; + + // Inject the client information. + TestReflection::setValue( + $this->class, + 'client', + (object) array( + 'engine' => JApplicationWebClient::GECKO, + ) + ); + + // Inject the internal configuration. + $config = new Registry; + $config->set('uri.base.full', $base); + + TestReflection::setValue($this->class, 'config', $config); + + $this->class->redirect($url, false); + + $this->assertEquals( + array('HTTP/1.1 303', true, null), $this->class->headers[0] ); @@ -1198,7 +1328,7 @@ public function testRedirectWithMoved() $this->class->redirect($url, true); $this->assertEquals( - array('HTTP/1.1 301 Moved Permanently', true, null), + array('HTTP/1.1 301', true, null), $this->class->headers[0] ); @@ -1323,7 +1453,7 @@ public function testSendHeaders() $this->assertEquals( array( - array('HTTP/1.1 200', null, 200), + array('HTTP/1.1 200', true, null), array('X-JWeb-SendHeaders: foo', true, null), ), $this->class->headers