Core: Preserve CSP nonce on scripts in DOM manipulation
[jquery.git] / test / data / mock.php
blob7e6aa1becfa5598120ac335cabf6903d1020deb0
1 <?php
2 /**
3 * Keep in sync with /test/middleware-mockserver.js
4 */
5 class MockServer {
6 protected function contentType( $req ) {
7 $type = $req->query['contentType'];
8 header("Content-type: $type");
9 echo $req->query['response'];
12 protected function wait( $req ) {
13 $wait = (int) $req->query['wait'];
14 sleep( $wait );
15 if ( isset( $req->query['script'] ) ) {
16 header( 'Content-type: text/javascript' );
17 } else {
18 header( 'Content-type: text/html' );
19 echo 'ERROR <script>ok( true, "mock executed" );</script>';
23 protected function name( $req ) {
24 if ( $req->query['name'] === 'foo' ) {
25 echo 'bar';
26 } elseif ( $_POST['name'] === 'peter' ) {
27 echo 'pan';
28 } else {
29 echo 'ERROR';
33 protected function xml( $req ) {
34 header( 'Content-type: text/xml' );
35 if ( $req->query['cal'] !== '5-2' && $_POST['cal'] !== '5-2' ) {
36 echo '<error>ERROR</error>';
37 return;
39 echo "<math><calculation>5-2</calculation><result>3</result></math>\n";
42 protected function atom( $req ) {
43 header( 'Content-type: atom+xml' );
44 echo '<root><element /></root>';
47 protected function script( $req ) {
48 if ( isset( $req->query['header'] ) ) {
49 if ( $req->query['header'] === 'ecma' ) {
50 header( 'Content-type: application/ecmascript' );
51 } else {
52 header( 'Content-type: text/javascript' );
54 } else {
55 header( 'Content-type: text/html' );
57 echo 'ok( true, "mock executed" );';
60 // Used to be in test.js, but was renamed to testbar.php
61 // https://github.com/jquery/jquery/commit/d89c278a33#commitcomment-23423165
62 protected function testbar( $req ) {
63 echo 'this.testBar = "bar";
64 jQuery("#ap").html("bar");
65 ok( true, "mock executed");';
68 protected function json( $req ) {
69 if ( isset( $req->query['header'] ) ) {
70 header( 'Content-type: application/json' );
73 if ( isset( $req->query['array'] ) ) {
74 echo '[ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ]';
75 } else {
76 echo '{ "data": {"lang": "en", "length": 25} }';
80 protected function jsonp( $req ) {
81 if ( isset( $req->query['callback'] ) ) {
82 $callback = $req->query['callback'];
83 } elseif ( $req->method === 'GET' ) {
84 // Try REST-like path
85 preg_match( '/\/([^\/?]+)\?.+$/', $req->url, $m );
86 $callback = $m[1];
87 } else {
88 $callback = $_POST['callback'];
90 if ( isset( $req->query['array'] ) ) {
91 echo $callback . '([ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ])';
92 } else {
93 echo $callback . '({ "data": {"lang": "en", "length": 25} })';
97 protected function xmlOverJsonp( $req ) {
98 $callback = $_REQUEST['callback'];
99 $text = json_encode( file_get_contents( __DIR__ . '/with_fries.xml' ) );
100 echo "$callback($text)\n";
103 protected function error( $req ) {
104 header( 'HTTP/1.0 400 Bad Request' );
105 if ( isset( $req->query['json'] ) ) {
106 header( 'Content-Type: application/json' );
107 echo '{ "code": 40, "message": "Bad Request" }';
108 } else {
109 echo 'plain text message';
113 protected function headers( $req ) {
114 header( 'Sample-Header: Hello World' );
115 header( 'Empty-Header: ' );
116 header( 'Sample-Header2: Hello World 2' );
117 header( 'List-Header: Item 1' );
118 header( 'list-header: Item 2', FALSE );
119 header( 'constructor: prototype collision (constructor)' );
121 foreach ( explode( '|' , $req->query[ 'keys' ] ) as $key ) {
122 // Only echo if key exists in the header
123 if ( isset( $req->headers[ strtoupper( $key ) ] ) ) {
124 echo "$key: " . $req->headers[ strtoupper( $key ) ] . "\n";
130 protected function echoData( $req ) {
131 echo file_get_contents('php://input');
134 protected function echoQuery( $req ) {
135 echo $_SERVER['QUERY_STRING'];
138 protected function echoMethod( $req ) {
139 echo $req->method;
142 protected function echoHtml( $req ) {
143 header( 'Content-type: text/html' );
144 echo '<div id="method">' . $req->method . '</div>';
145 echo '<div id="query">' . $_SERVER['QUERY_STRING'] . '</div>';
146 echo '<div id="data">' . file_get_contents('php://input') . '</div>';
149 protected function etag( $req ) {
150 $hash = md5( $req->query['ts'] );
151 $etag = 'W/"' . $hash . '"';
153 $ifNoneMatch = isset( $req->headers['IF-NONE-MATCH'] ) ? $req->headers['IF-NONE-MATCH'] : '';
154 if ($ifNoneMatch === $etag) {
155 header('HTTP/1.0 304 Not Modified');
156 return;
159 header("Etag: $etag");
160 echo "ETag: $etag\n";
161 if ( $ifNoneMatch ) {
162 echo "If-None-Match: $ifNoneMatch\n";
166 protected function ims( $req ) {
167 $ts = $req->query['ts'];
169 $ims = isset( $req->headers['IF-MODIFIED-SINCE'] ) ? $req->headers['IF-MODIFIED-SINCE'] : '';
170 if ($ims === $ts) {
171 header('HTTP/1.0 304 Not Modified');
172 return;
175 header("Last-Modified: $ts");
176 echo "Last-Modified: $ts\n";
177 if ( $ims ) {
178 echo "If-Modified-Since: $ims\n";
182 protected function status( $req ) {
183 header( "HTTP/1.0 {$req->query['code']} {$req->query['text']}" );
186 protected function testHTML( $req ) {
187 header( 'Content-type: text/html' );
188 $html = file_get_contents( __DIR__ . '/test.include.html' );
189 $html = str_replace( '{{baseURL}}', $req->query['baseURL'], $html );
190 echo $html;
193 protected function cspFrame( $req ) {
194 // This is CSP only for browsers with "Content-Security-Policy" header support
195 // i.e. no old WebKit or old Firefox
196 header( "Content-Security-Policy: default-src 'self'; report-uri ./mock.php?action=cspLog" );
197 header( 'Content-type: text/html' );
198 echo file_get_contents( __DIR__ . '/csp.include.html' );
201 protected function cspNonce( $req ) {
202 // This is CSP only for browsers with "Content-Security-Policy" header support
203 // i.e. no old WebKit or old Firefox
204 header( "Content-Security-Policy: script-src 'nonce-jquery+hardcoded+nonce'; report-uri ./mock.php?action=cspLog" );
205 header( 'Content-type: text/html' );
206 echo file_get_contents( __DIR__ . '/csp-nonce.html' );
209 protected function cspLog( $req ) {
210 file_put_contents( $this->cspFile, 'error' );
213 protected function cspClean( $req ) {
214 file_put_contents( $this->cspFile, '' );
215 unlink( $this->cspFile );
218 public function __construct() {
219 $this->cspFile = __DIR__ . '/support/csp.log';
222 public function respond( stdClass $req ) {
223 if ( !isset( $req->query['action'] ) || !method_exists( $this, $req->query['action'] ) ) {
224 header( "HTTP/1.0 400 Bad Request" );
225 echo "Invalid action query.\n";
226 return;
228 $this->{$req->query['action']}( $req );
232 // Don't include PHP errors in http response
233 error_reporting( 0 );
235 // Collect headers
236 $headers = array();
237 foreach ( $_SERVER as $name => $value ) {
238 if ( substr( $name, 0, 5 ) === 'HTTP_' ) {
239 $name = str_replace( '_', '-', substr( $name, 5 ) );
240 $headers[$name] = $value;
241 } elseif ( $name === 'CONTENT_LENGTH' ) {
242 $headers['CONTENT-LENGTH'] = $value;
243 } elseif ( $name === 'CONTENT_TYPE' ) {
244 $headers['CONTENT-TYPE'] = $value;
248 $mock = new MockServer();
249 $req = (object) array(
250 'query' => $_GET,
251 'headers' => $headers,
252 'method' => $_SERVER['REQUEST_METHOD'],
253 'url' => $_SERVER['REQUEST_URI'],
255 $mock->respond( $req );