| 356 | /** |
| 357 | * Test that server response codes are handled consistently |
| 358 | * |
| 359 | * @ticket 17010 |
| 360 | * |
| 361 | * @dataProvider data_http_status_codes |
| 362 | */ |
| 363 | function test_http_response_code_handling( $code ) { |
| 364 | |
| 365 | $res = wp_remote_head( sprintf( 'http://trunk.wp/status.php?status=%d', $code ) ); |
| 366 | |
| 367 | if ( $code < 200 ) { |
| 368 | |
| 369 | // A 1xx request should result in an error |
| 370 | if ( ! is_wp_error( $res ) ) { |
| 371 | $this->fail( sprintf( '%s (status: %d)', wp_remote_retrieve_response_message( $res ), $code ) ); |
| 372 | } |
| 373 | |
| 374 | $this->assertSame( 'http_request_failed', $res->get_error_code() ); |
| 375 | |
| 376 | } else { |
| 377 | |
| 378 | // Any other request should not result in an error |
| 379 | if ( is_wp_error( $res ) ) { |
| 380 | $this->fail( sprintf( '%s (status: %d)', $res->get_error_message(), $code ) ); |
| 381 | } |
| 382 | |
| 383 | $this->assertSame( $code, wp_remote_retrieve_response_code( $res ) ); |
| 384 | |
| 385 | } |
| 386 | |
| 387 | } |
| 388 | |
| 389 | function data_http_status_codes() { |
| 390 | global $wp_header_to_desc; |
| 391 | |
| 392 | // Hackedy hack! This populates the $wp_header_to_desc global |
| 393 | get_status_header_desc( 200 ); |
| 394 | |
| 395 | $data = array(); |
| 396 | |
| 397 | foreach ( $wp_header_to_desc as $code => $description ) { |
| 398 | $data[] = array( |
| 399 | $code, |
| 400 | ); |
| 401 | } |
| 402 | |
| 403 | return $data; |
| 404 | |
| 405 | } |