Skip to content

Commit

Permalink
Merge pull request #280 from alleyinteractive/hotfix/redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher authored Jun 10, 2022
2 parents 1961d11 + ecbccc3 commit 34fbd16
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/mantle/http/routing/class-url-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ public function previous( string $fallback = null ): string {
* @return string
*/
public function to( string $path, array $extra = [], bool $secure = null ) {
// First we will check if the URL is already a valid URL. If it is we will not
// try to generate a new one but will simply return the URL as is, which is
// convenient since developers do not always have to check if it's valid.
if ( $this->is_valid_url( $path ) ) {
return $path;
}
Expand Down
25 changes: 17 additions & 8 deletions src/mantle/testing/class-test-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function get_content() {
* @return $this
*/
public function set_headers( array $headers ): object {
$this->headers = $headers;
$this->headers = array_change_key_case( $headers, CASE_LOWER );

return $this;
}
Expand All @@ -135,6 +135,9 @@ public function get_headers() {
* @return string|null
*/
public function get_header( string $key, string $default = null ): ?string {
// Enforce a lowercase header name.
$key = strtolower( $key );

// If the header is set and not null, return the string value.
if ( isset( $this->headers[ $key ] ) ) {
// Account for multiple headers with the same key.
Expand Down Expand Up @@ -245,15 +248,15 @@ public function assertUnauthorized() {
* Assert whether the response is redirecting to a given URI.
*
* @param string|null $uri URI to assert redirection to.
* @return $this
* @return static
*/
public function assertRedirect( $uri = null ) {
public function assertRedirect( ?string $uri = null ) {
PHPUnit::assertTrue(
$this->is_redirect(),
'Response status code [' . $this->get_status_code() . '] is not a redirect status code.'
);

if ( ! is_null( $uri ) ) {
if ( $uri ) {
$this->assertLocation( $uri );
}

Expand All @@ -275,12 +278,12 @@ public function is_redirect( string $location = null ): bool {
* Assert that the current location header matches the given URI.
*
* @param string $uri URI to assert that the location header is set to.
* @return $this
* @return static
*/
public function assertLocation( $uri ) {
PHPUnit::assertEquals(
trailingslashit( home_url( $uri ) ),
trailingslashit( home_url( $this->get_header( 'Location' ) ) )
app( 'url' )->to( $uri ),
app( 'url' )->to( $this->get_header( 'location' ) ),
);

return $this;
Expand All @@ -292,9 +295,12 @@ public function assertLocation( $uri ) {
*
* @param string $header_name Header name (key) to assert.
* @param mixed $value Header value to assert.
* @return $this
* @return static
*/
public function assertHeader( $header_name, $value = null ) {
// Enforce a lowercase header name.
$header_name = strtolower( $header_name );

PHPUnit::assertArrayHasKey(
$header_name,
$this->headers,
Expand All @@ -321,6 +327,9 @@ public function assertHeader( $header_name, $value = null ) {
* @return $this
*/
public function assertHeaderMissing( $header_name ) {
// Enforce a lowercase header name.
$header_name = strtolower( $header_name );

PHPUnit::assertArrayNotHasKey(
$header_name,
$this->headers,
Expand Down
13 changes: 13 additions & 0 deletions tests/testing/concerns/test-makes-http-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ public function test_rest_api_route_error() {
->assertStatus( 404 );
}

public function test_redirect_response() {
$this->app['router']->get(
'/route-to-redirect/',
fn () => redirect()->to( '/redirected', 302, [ 'Other-Header' => '123' ] ),
);

$this->get( '/route-to-redirect/' )
->assertHeader( 'location', home_url( '/redirected' ) )
->assertHeader( 'Location', home_url( '/redirected' ) )
->assertRedirect( '/redirected' )
->assertHeader( 'Other-Header', '123' );
}

public function test_multiple_requests() {
// Re-run all test methods on this class in a single pass.
foreach ( get_class_methods( $this ) as $method ) {
Expand Down

0 comments on commit 34fbd16

Please sign in to comment.