|
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'];
|
@@ -87,17 +92,17 @@ protected function jsonp( $req ) {
|
87 | 92 | } else {
|
88 | 93 | $callback = $_POST['callback'];
|
89 | 94 | }
|
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} })'; |
94 |
| - } |
| 95 | + $json = isset( $req->query['array'] ) ? |
| 96 | + '[ { "name": "John", "age": 21 }, { "name": "Peter", "age": 25 } ]' : |
| 97 | + '{ "data": { "lang": "en", "length": 25 } }'; |
| 98 | + echo cleanCallback( $callback ) . '(' . $json . ')'; |
95 | 99 | }
|
96 | 100 |
|
97 | 101 | protected function xmlOverJsonp( $req ) {
|
98 | 102 | $callback = $_REQUEST['callback'];
|
| 103 | + $cleanCallback = cleanCallback( $callback ); |
99 | 104 | $text = json_encode( file_get_contents( __DIR__ . '/with_fries.xml' ) );
|
100 |
| - echo "$callback($text)\n"; |
| 105 | + echo "$cleanCallback($text)\n"; |
101 | 106 | }
|
102 | 107 |
|
103 | 108 | protected function error( $req ) {
|
@@ -223,7 +228,7 @@ protected function errorWithScript( $req ) {
|
223 | 228 | }
|
224 | 229 | if ( isset( $req->query['callback'] ) ) {
|
225 | 230 | $callback = $req->query['callback'];
|
226 |
| - echo $callback . '( {"status": 404, "msg": "Not Found"} )'; |
| 231 | + echo cleanCallback( $callback ) . '( {"status": 404, "msg": "Not Found"} )'; |
227 | 232 | } else {
|
228 | 233 | echo 'QUnit.assert.ok( false, "Mock return erroneously executed" );';
|
229 | 234 | }
|
|
0 commit comments