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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
<?php
/**
* Keep in sync with /test/middleware-mockserver.js
*/
class MockServer {
protected function contentType( $req ) {
$type = $req->query['contentType'];
header("Content-type: $type");
echo $req->query['response'];
}
protected function wait( $req ) {
$wait = (int) $req->query['wait'];
sleep( $wait );
if ( isset( $req->query['script'] ) ) {
header( 'Content-type: text/javascript' );
} else {
header( 'Content-type: text/html' );
echo 'ERROR <script>QUnit.assert.ok( true, "mock executed" );</script>';
}
}
protected function name( $req ) {
if ( $req->query['name'] === 'foo' ) {
echo 'bar';
} elseif ( $_POST['name'] === 'peter' ) {
echo 'pan';
} else {
echo 'ERROR';
}
}
protected function xml( $req ) {
header( 'Content-type: text/xml' );
if ( $req->query['cal'] !== '5-2' && $_POST['cal'] !== '5-2' ) {
echo '<error>ERROR</error>';
return;
}
echo "<math><calculation>5-2</calculation><result>3</result></math>\n";
}
protected function atom( $req ) {
header( 'Content-type: atom+xml' );
echo '<root><element /></root>';
}
protected function script( $req ) {
if ( isset( $req->query['header'] ) ) {
if ( $req->query['header'] === 'ecma' ) {
header( 'Content-type: application/ecmascript' );
} else {
header( 'Content-type: text/javascript' );
}
} else {
header( 'Content-type: text/html' );
}
echo 'QUnit.assert.ok( true, "mock executed" );';
}
// Used to be in test.js, but was renamed to testbar.php
// https://github.com/jquery/jquery/commit/d89c278a33#commitcomment-23423165
protected function testbar( $req ) {
echo 'this.testBar = "bar";
jQuery("#ap").html("bar");
QUnit.assert.ok( true, "mock executed");';
}
protected function json( $req ) {
if ( isset( $req->query['header'] ) ) {
header( 'Content-type: application/json' );
}
if ( isset( $req->query['cors'] ) ) {
header( 'Access-Control-Allow-Origin: *' );
}
if ( isset( $req->query['array'] ) ) {
echo '[ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ]';
} else {
echo '{ "data": {"lang": "en", "length": 25} }';
}
}
protected function jsonp( $req ) {
if ( isset( $req->query['callback'] ) ) {
$callback = $req->query['callback'];
} elseif ( $req->method === 'GET' ) {
// Try REST-like path
preg_match( '/\/([^\/?]+)\?.+$/', $req->url, $m );
$callback = $m[1];
} else {
$callback = $_POST['callback'];
}
if ( isset( $req->query['array'] ) ) {
echo $callback . '([ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ])';
} else {
echo $callback . '({ "data": {"lang": "en", "length": 25} })';
}
}
protected function xmlOverJsonp( $req ) {
$callback = $_REQUEST['callback'];
$text = json_encode( file_get_contents( __DIR__ . '/with_fries.xml' ) );
echo "$callback($text)\n";
}
protected function error( $req ) {
header( 'HTTP/1.0 400 Bad Request' );
if ( isset( $req->query['json'] ) ) {
header( 'Content-Type: application/json' );
echo '{ "code": 40, "message": "Bad Request" }';
} else {
echo 'plain text message';
}
}
protected function headers( $req ) {
header( 'Sample-Header: Hello World' );
header( 'Empty-Header: ' );
header( 'Sample-Header2: Hello World 2' );
header( 'List-Header: Item 1' );
header( 'list-header: Item 2', FALSE );
header( 'constructor: prototype collision (constructor)' );
foreach ( explode( '|' , $req->query[ 'keys' ] ) as $key ) {
// Only echo if key exists in the header
if ( isset( $req->headers[ strtoupper( $key ) ] ) ) {
echo "$key: " . $req->headers[ strtoupper( $key ) ] . "\n";
}
}
}
protected function echoData( $req ) {
echo file_get_contents('php://input');
}
protected function echoQuery( $req ) {
echo $_SERVER['QUERY_STRING'];
}
protected function echoMethod( $req ) {
echo $req->method;
}
protected function echoHtml( $req ) {
header( 'Content-type: text/html' );
echo '<div id="method">' . $req->method . '</div>';
echo '<div id="query">' . $_SERVER['QUERY_STRING'] . '</div>';
echo '<div id="data">' . file_get_contents('php://input') . '</div>';
}
protected function etag( $req ) {
$hash = md5( $req->query['ts'] );
$etag = 'W/"' . $hash . '"';
$ifNoneMatch = isset( $req->headers['IF-NONE-MATCH'] ) ? $req->headers['IF-NONE-MATCH'] : '';
if ($ifNoneMatch === $etag) {
header('HTTP/1.0 304 Not Modified');
return;
}
header("Etag: $etag");
echo "ETag: $etag\n";
if ( $ifNoneMatch ) {
echo "If-None-Match: $ifNoneMatch\n";
}
}
protected function ims( $req ) {
$ts = $req->query['ts'];
$ims = isset( $req->headers['IF-MODIFIED-SINCE'] ) ? $req->headers['IF-MODIFIED-SINCE'] : '';
if ($ims === $ts) {
header('HTTP/1.0 304 Not Modified');
return;
}
header("Last-Modified: $ts");
echo "Last-Modified: $ts\n";
if ( $ims ) {
echo "If-Modified-Since: $ims\n";
}
}
protected function status( $req ) {
header( "HTTP/1.0 {$req->query['code']} {$req->query['text']}" );
}
protected function testHTML( $req ) {
header( 'Content-type: text/html' );
$html = file_get_contents( __DIR__ . '/test.include.html' );
$html = str_replace( '{{baseURL}}', $req->query['baseURL'], $html );
echo $html;
}
protected function cspFrame( $req ) {
header( "Content-Security-Policy: default-src 'self'; report-uri ./mock.php?action=cspLog" );
header( 'Content-type: text/html' );
echo file_get_contents( __DIR__ . '/csp.include.html' );
}
protected function cspNonce( $req ) {
$test = $req->query['test'] ? '-' . $req->query['test'] : '';
header( "Content-Security-Policy: script-src 'nonce-jquery+hardcoded+nonce'; report-uri ./mock.php?action=cspLog" );
header( 'Content-type: text/html' );
echo file_get_contents( __DIR__ . '/csp-nonce' . $test . '.html' );
}
protected function cspAjaxScript( $req ) {
header( "Content-Security-Policy: script-src 'self'; report-uri /base/test/data/mock.php?action=cspLog" );
header( 'Content-type: text/html' );
echo file_get_contents( __DIR__ . '/csp-ajax-script.html' );
}
protected function cspLog( $req ) {
file_put_contents( $this->cspFile, 'error' );
}
protected function cspClean( $req ) {
file_put_contents( $this->cspFile, '' );
unlink( $this->cspFile );
}
protected function errorWithScript( $req ) {
header( 'HTTP/1.0 404 Not Found' );
if ( isset( $req->query['withScriptContentType'] ) ) {
header( 'Content-Type: application/javascript' );
}
if ( isset( $req->query['callback'] ) ) {
$callback = $req->query['callback'];
echo $callback . '( {"status": 404, "msg": "Not Found"} )';
} else {
echo 'QUnit.assert.ok( false, "Mock return erroneously executed" );';
}
}
public function __construct() {
$this->cspFile = __DIR__ . '/support/csp.log';
}
public function respond( stdClass $req ) {
if ( !isset( $req->query['action'] ) || !method_exists( $this, $req->query['action'] ) ) {
header( "HTTP/1.0 400 Bad Request" );
echo "Invalid action query.\n";
return;
}
$this->{$req->query['action']}( $req );
}
}
// Don't include PHP errors in http response
error_reporting( 0 );
// Collect headers
$headers = array();
foreach ( $_SERVER as $name => $value ) {
if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
$name = str_replace( '_', '-', substr( $name, 5 ) );
$headers[$name] = $value;
} elseif ( $name === 'CONTENT_LENGTH' ) {
$headers['CONTENT-LENGTH'] = $value;
} elseif ( $name === 'CONTENT_TYPE' ) {
$headers['CONTENT-TYPE'] = $value;
}
}
$mock = new MockServer();
$req = (object) array(
'query' => $_GET,
'headers' => $headers,
'method' => $_SERVER['REQUEST_METHOD'],
'url' => $_SERVER['REQUEST_URI'],
);
$mock->respond( $req );
|