Skip to content

Commit a2b5979

Browse files
committed
Tests: Cover wp_is_json_request().
Verify JSON request detection through the `Accept` and `Content-Type` headers, including absent and non-JSON header values. Developed in: #14419 Props shailu25, rejaulalomkhan. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@64514 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 9607bf4 commit a2b5979

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
/**
4+
* Tests for wp_is_json_request().
5+
*
6+
* @group load
7+
*
8+
* @covers ::wp_is_json_request
9+
*/
10+
class Tests_Load_WpIsJsonRequest extends WP_UnitTestCase {
11+
12+
/**
13+
* Original HTTP_ACCEPT value.
14+
*
15+
* @var string|null
16+
*/
17+
private $original_http_accept;
18+
19+
/**
20+
* Original CONTENT_TYPE value.
21+
*
22+
* @var string|null
23+
*/
24+
private $original_content_type;
25+
26+
public function set_up() {
27+
parent::set_up();
28+
29+
$this->original_http_accept = isset( $_SERVER['HTTP_ACCEPT'] ) ? $_SERVER['HTTP_ACCEPT'] : null;
30+
$this->original_content_type = isset( $_SERVER['CONTENT_TYPE'] ) ? $_SERVER['CONTENT_TYPE'] : null;
31+
}
32+
33+
public function tear_down() {
34+
if ( null === $this->original_http_accept ) {
35+
unset( $_SERVER['HTTP_ACCEPT'] );
36+
} else {
37+
$_SERVER['HTTP_ACCEPT'] = $this->original_http_accept;
38+
}
39+
40+
if ( null === $this->original_content_type ) {
41+
unset( $_SERVER['CONTENT_TYPE'] );
42+
} else {
43+
$_SERVER['CONTENT_TYPE'] = $this->original_content_type;
44+
}
45+
46+
parent::tear_down();
47+
}
48+
49+
/**
50+
* Tests that non-JSON Accept and Content-Type headers are not JSON requests.
51+
*
52+
* @ticket 65819
53+
*/
54+
public function test_returns_false_for_non_json_headers() {
55+
unset( $_SERVER['HTTP_ACCEPT'], $_SERVER['CONTENT_TYPE'] );
56+
57+
$this->assertFalse( wp_is_json_request() );
58+
59+
$_SERVER['HTTP_ACCEPT'] = 'text/html';
60+
$this->assertFalse( wp_is_json_request() );
61+
62+
unset( $_SERVER['HTTP_ACCEPT'] );
63+
$_SERVER['CONTENT_TYPE'] = 'text/plain';
64+
$this->assertFalse( wp_is_json_request() );
65+
}
66+
67+
/**
68+
* Tests JSON detection via the Accept header.
69+
*
70+
* @ticket 65819
71+
*
72+
* @dataProvider data_json_accept_headers
73+
*
74+
* @param string $accept Accept header value.
75+
*/
76+
public function test_returns_true_for_json_accept_header( $accept ) {
77+
unset( $_SERVER['CONTENT_TYPE'] );
78+
$_SERVER['HTTP_ACCEPT'] = $accept;
79+
80+
$this->assertTrue( wp_is_json_request() );
81+
}
82+
83+
/**
84+
* Data provider for JSON Accept headers.
85+
*
86+
* @return array[]
87+
*/
88+
public static function data_json_accept_headers() {
89+
return array(
90+
'application/json' => array( 'application/json' ),
91+
'vendor json' => array( 'application/vnd.api+json' ),
92+
'json oembed' => array( 'application/json+oembed' ),
93+
'accept with quality' => array( 'application/json; q=0.9' ),
94+
'accept among other types' => array( 'text/html, application/json, */*' ),
95+
);
96+
}
97+
98+
/**
99+
* Tests JSON detection via the Content-Type header.
100+
*
101+
* @ticket 65819
102+
*
103+
* @dataProvider data_json_content_types
104+
*
105+
* @param string $content_type Content-Type header value.
106+
*/
107+
public function test_returns_true_for_json_content_type( $content_type ) {
108+
unset( $_SERVER['HTTP_ACCEPT'] );
109+
$_SERVER['CONTENT_TYPE'] = $content_type;
110+
111+
$this->assertTrue( wp_is_json_request() );
112+
}
113+
114+
/**
115+
* Data provider for JSON Content-Type headers.
116+
*
117+
* @return array[]
118+
*/
119+
public static function data_json_content_types() {
120+
return array(
121+
'application/json' => array( 'application/json' ),
122+
'vendor json' => array( 'application/vnd.api+json' ),
123+
'with charset' => array( 'application/json; charset=utf-8' ),
124+
);
125+
}
126+
}

0 commit comments

Comments
 (0)