-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathWpscParseAcceptHeaderTest.php
More file actions
118 lines (96 loc) · 5.67 KB
/
WpscParseAcceptHeaderTest.php
File metadata and controls
118 lines (96 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* Tests for wpsc_parse_accept_header().
*
* @package automattic/wp-super-cache
*/
// wpsc_parse_accept_header() has no WordPress dependencies — include only the
// containing file. wp_cache_debug() is defined inside wp-cache-phase2.php and
// returns early when its globals are unset, so no stub is needed. apply_filters()
// is a WP core function not defined in the file; stub it for completeness.
if ( ! function_exists( 'apply_filters' ) ) {
function apply_filters( $tag, $value ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement
return $value;
}
}
require_once dirname( __DIR__, 2 ) . '/wp-cache-phase2.php';
use PHPUnit\Framework\TestCase;
/**
* @covers wpsc_parse_accept_header
*/
class WpscParseAcceptHeaderTest extends TestCase {
/** Default JSON types mirroring the wpsc_accept_headers default. */
private array $json_types = array(
'application/json',
'application/activity+json',
'application/ld+json',
);
// ── RFC 7231 acceptance criteria from issue #1045 ─────────────────────────
/**
* New Relic Synthetics header: text/html has implicit q=1.0, JSON is
* deprioritised at q=0.9 — must classify as text/html.
*/
public function test_nr_synthetics_header_classifies_as_html(): void {
$accept = 'text/html,application/xhtml+xml,application/json;q=0.9,application/javascript;q=0.9,text/javascript;q=0.9,application/xml;q=0.9,text/plain;q=0.8,*/*;q=0.7';
$this->assertSame( 'text/html', wpsc_parse_accept_header( $accept, $this->json_types ) );
}
/** Bare JSON-only Accept: application/json should classify as JSON. */
public function test_bare_json_classifies_as_json(): void {
$this->assertSame( 'application/json', wpsc_parse_accept_header( 'application/json', $this->json_types ) );
}
/** Tie (both implicit q=1.0) resolves to text/html (safe default). */
public function test_tie_resolves_to_html(): void {
$this->assertSame( 'text/html', wpsc_parse_accept_header( 'text/html,application/json', $this->json_types ) );
}
/** JSON strictly higher q than text/html must classify as JSON. */
public function test_json_higher_q_classifies_as_json(): void {
$this->assertSame( 'application/json', wpsc_parse_accept_header( 'application/json,text/html;q=0.9', $this->json_types ) );
}
/** Extended JSON type (application/ld+json) via filter participates in comparison. */
public function test_extended_json_type_participates(): void {
$this->assertSame( 'application/json', wpsc_parse_accept_header( 'application/ld+json;q=1.0,text/html;q=0.8', $this->json_types ) );
}
/** Malformed q-value (non-numeric) treated as q=1.0, no warning/fatal. */
public function test_malformed_q_value_treated_as_default(): void {
// application/json;q=bad → treated as q=1.0; text/html is absent → effective html_q from */*=0.7.
$this->assertSame( 'application/json', wpsc_parse_accept_header( 'application/json;q=bad,*/*;q=0.7', $this->json_types ) );
}
/** Out-of-range q-value clamped — q=2 treated as 1.0. */
public function test_out_of_range_q_clamped(): void {
$this->assertSame( 'text/html', wpsc_parse_accept_header( 'text/html;q=2,application/json;q=0.9', $this->json_types ) );
}
// ── Wildcard behaviour ────────────────────────────────────────────────────
// Wildcard (*/*) covers text/html when text/html is not explicitly listed.
public function test_wildcard_covers_html_when_not_explicit(): void {
// */* q=1.0 > json q=0.9 → text/html.
$this->assertSame( 'text/html', wpsc_parse_accept_header( '*/*,application/json;q=0.9', $this->json_types ) );
}
// Explicit text/html;q=0.5 wins over */*;q=1.0 — wildcard does not boost an explicit html q.
public function test_explicit_html_takes_precedence_over_wildcard(): void {
// html explicit q=0.5; wildcard q=1.0; json q=0.8 → 0.8 > 0.5 → application/json.
$this->assertSame( 'application/json', wpsc_parse_accept_header( 'text/html;q=0.5,*/*;q=1.0,application/json;q=0.8', $this->json_types ) );
}
// No text/html and no wildcard, but JSON present — effective html_q = 0.0 → application/json.
public function test_no_html_no_wildcard_json_present(): void {
$this->assertSame( 'application/json', wpsc_parse_accept_header( 'application/json;q=0.5', $this->json_types ) );
}
// ── Edge cases ────────────────────────────────────────────────────────────
/** Whitespace around media types is handled. */
public function test_whitespace_around_media_types(): void {
$this->assertSame( 'text/html', wpsc_parse_accept_header( ' text/html , application/json;q=0.9 ', $this->json_types ) );
}
/** application/activity+json classified as JSON. */
public function test_activity_json_classified_as_json(): void {
$this->assertSame( 'application/json', wpsc_parse_accept_header( 'application/activity+json', $this->json_types ) );
}
/** Custom type added via wpsc_accept_headers filter participates. */
public function test_custom_json_type_via_filter_participates(): void {
$extended = array_merge( $this->json_types, array( 'application/vnd.api+json' ) );
$this->assertSame( 'application/json', wpsc_parse_accept_header( 'application/vnd.api+json;q=1.0,text/html;q=0.8', $extended ) );
}
/** Typical browser Accept header classifies as text/html. */
public function test_typical_browser_accept_header(): void {
$accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8';
$this->assertSame( 'text/html', wpsc_parse_accept_header( $accept, $this->json_types ) );
}
}