diff --git a/src/mantle/http/routing/class-url-generator.php b/src/mantle/http/routing/class-url-generator.php index e3819dfef..b9a89689a 100644 --- a/src/mantle/http/routing/class-url-generator.php +++ b/src/mantle/http/routing/class-url-generator.php @@ -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; } diff --git a/src/mantle/testing/class-test-response.php b/src/mantle/testing/class-test-response.php index c94efaeac..f3d9aaa8f 100644 --- a/src/mantle/testing/class-test-response.php +++ b/src/mantle/testing/class-test-response.php @@ -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; } @@ -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. @@ -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 ); } @@ -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; @@ -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, @@ -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, diff --git a/tests/testing/concerns/test-makes-http-requests.php b/tests/testing/concerns/test-makes-http-requests.php index 8ddff4f50..1fb7e5cf4 100644 --- a/tests/testing/concerns/test-makes-http-requests.php +++ b/tests/testing/concerns/test-makes-http-requests.php @@ -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 ) {