|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | /**
|
3 | 4 | * Keep in sync with /test/middleware-mockserver.js
|
4 | 5 | */
|
| 6 | +function cleanCallback( $callback ) { |
| 7 | + return preg_replace( '/[^a-z0-9_]/i', '', $callback ); |
| 8 | +} |
| 9 | + |
5 | 10 | class MockServer {
|
6 | 11 | protected function contentType( $req ) {
|
7 | 12 | $type = $req->query['contentType'];
|
@@ -65,7 +70,8 @@ protected function script( $req ) {
|
65 | 70 | array_values( $req->headers )
|
66 | 71 | );
|
67 | 72 |
|
68 |
| - echo $req->query['callback'] . "(" . json_encode( [ 'headers' => $headers ] ) . ")"; |
| 73 | + echo cleanCallback( $req->query['callback'] ) . |
| 74 | + "(" . json_encode( [ 'headers' => $headers ] ) . ")"; |
69 | 75 | } else {
|
70 | 76 | echo 'QUnit.assert.ok( true, "mock executed" );';
|
71 | 77 | }
|
@@ -105,17 +111,17 @@ protected function jsonp( $req ) {
|
105 | 111 | } else {
|
106 | 112 | $callback = $_POST['callback'];
|
107 | 113 | }
|
108 |
| - if ( isset( $req->query['array'] ) ) { |
109 |
| - echo $callback . '([ {"name": "John", "age": 21}, {"name": "Peter", "age": 25 } ])'; |
110 |
| - } else { |
111 |
| - echo $callback . '({ "data": {"lang": "en", "length": 25} })'; |
112 |
| - } |
| 114 | + $json = isset( $req->query['array'] ) ? |
| 115 | + '[ { "name": "John", "age": 21 }, { "name": "Peter", "age": 25 } ]' : |
| 116 | + '{ "data": { "lang": "en", "length": 25 } }'; |
| 117 | + echo cleanCallback( $callback ) . '(' . $json . ')'; |
113 | 118 | }
|
114 | 119 |
|
115 | 120 | protected function xmlOverJsonp( $req ) {
|
116 | 121 | $callback = $_REQUEST['callback'];
|
| 122 | + $cleanCallback = cleanCallback( $callback ); |
117 | 123 | $text = json_encode( file_get_contents( __DIR__ . '/with_fries.xml' ) );
|
118 |
| - echo "$callback($text)\n"; |
| 124 | + echo "$cleanCallback($text)\n"; |
119 | 125 | }
|
120 | 126 |
|
121 | 127 | protected function error( $req ) {
|
@@ -243,7 +249,7 @@ protected function errorWithScript( $req ) {
|
243 | 249 | }
|
244 | 250 | if ( isset( $req->query['callback'] ) ) {
|
245 | 251 | $callback = $req->query['callback'];
|
246 |
| - echo $callback . '( {"status": 404, "msg": "Not Found"} )'; |
| 252 | + echo cleanCallback( $callback ) . '( {"status": 404, "msg": "Not Found"} )'; |
247 | 253 | } else {
|
248 | 254 | echo 'QUnit.assert.ok( false, "Mock return erroneously executed" );';
|
249 | 255 | }
|
|
0 commit comments