Changeset 42343 for trunk/tests/phpunit/tests/rest-api/rest-server.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/rest-api/rest-server.php
r42228 r42343 30 30 31 31 public function test_envelope() { 32 $data = array(32 $data = array( 33 33 'amount of arbitrary data' => 'alot', 34 34 ); 35 $status = 987;35 $status = 987; 36 36 $headers = array( 37 37 'Arbitrary-Header' => 'value', 38 'Multiple' => 'maybe, yes',38 'Multiple' => 'maybe, yes', 39 39 ); 40 40 … … 56 56 $enveloped = $envelope_response->get_data(); 57 57 58 $this->assertEquals( $data, 59 $this->assertEquals( $status, 58 $this->assertEquals( $data, $enveloped['body'] ); 59 $this->assertEquals( $status, $enveloped['status'] ); 60 60 $this->assertEquals( $headers, $enveloped['headers'] ); 61 61 } … … 63 63 public function test_default_param() { 64 64 65 register_rest_route( 'test-ns', '/test', array( 66 'methods' => array( 'GET' ), 67 'callback' => '__return_null', 68 'args' => array( 69 'foo' => array( 70 'default' => 'bar', 71 ), 72 ), 73 ) ); 74 75 $request = new WP_REST_Request( 'GET', '/test-ns/test' ); 65 register_rest_route( 66 'test-ns', '/test', array( 67 'methods' => array( 'GET' ), 68 'callback' => '__return_null', 69 'args' => array( 70 'foo' => array( 71 'default' => 'bar', 72 ), 73 ), 74 ) 75 ); 76 77 $request = new WP_REST_Request( 'GET', '/test-ns/test' ); 76 78 $response = $this->server->dispatch( $request ); 77 79 … … 81 83 public function test_default_param_is_overridden() { 82 84 83 register_rest_route( 'test-ns', '/test', array( 84 'methods' => array( 'GET' ), 85 'callback' => '__return_null', 86 'args' => array( 87 'foo' => array( 88 'default' => 'bar', 89 ), 90 ), 91 ) ); 85 register_rest_route( 86 'test-ns', '/test', array( 87 'methods' => array( 'GET' ), 88 'callback' => '__return_null', 89 'args' => array( 90 'foo' => array( 91 'default' => 'bar', 92 ), 93 ), 94 ) 95 ); 92 96 93 97 $request = new WP_REST_Request( 'GET', '/test-ns/test' ); … … 99 103 100 104 public function test_optional_param() { 101 register_rest_route( 'optional', '/test', array( 102 'methods' => array( 'GET' ), 103 'callback' => '__return_null', 104 'args' => array( 105 'foo' => array(), 106 ), 107 ) ); 105 register_rest_route( 106 'optional', '/test', array( 107 'methods' => array( 'GET' ), 108 'callback' => '__return_null', 109 'args' => array( 110 'foo' => array(), 111 ), 112 ) 113 ); 108 114 109 115 $request = new WP_REST_Request( 'GET', '/optional/test' ); … … 116 122 117 123 public function test_no_zero_param() { 118 register_rest_route( 'no-zero', '/test', array( 119 'methods' => array( 'GET' ), 120 'callback' => '__return_null', 121 'args' => array( 122 'foo' => array( 123 'default' => 'bar', 124 ), 125 ), 126 ) ); 124 register_rest_route( 125 'no-zero', '/test', array( 126 'methods' => array( 'GET' ), 127 'callback' => '__return_null', 128 'args' => array( 129 'foo' => array( 130 'default' => 'bar', 131 ), 132 ), 133 ) 134 ); 127 135 $request = new WP_REST_Request( 'GET', '/no-zero/test' ); 128 136 $this->server->dispatch( $request ); … … 131 139 132 140 public function test_head_request_handled_by_get() { 133 register_rest_route( 'head-request', '/test', array( 134 'methods' => array( 'GET' ), 135 'callback' => '__return_true', 136 ) ); 137 $request = new WP_REST_Request( 'HEAD', '/head-request/test' ); 141 register_rest_route( 142 'head-request', '/test', array( 143 'methods' => array( 'GET' ), 144 'callback' => '__return_true', 145 ) 146 ); 147 $request = new WP_REST_Request( 'HEAD', '/head-request/test' ); 138 148 $response = $this->server->dispatch( $request ); 139 149 $this->assertEquals( 200, $response->get_status() ); … … 147 157 */ 148 158 public function test_explicit_head_callback() { 149 register_rest_route( 'head-request', '/test', array( 150 array( 151 'methods' => array( 'HEAD' ), 152 'callback' => '__return_true', 153 ), 154 array( 155 'methods' => array( 'GET' ), 156 'callback' => '__return_false', 157 'permission_callback' => array( $this, 'permission_denied' ), 158 ), 159 )); 160 $request = new WP_REST_Request( 'HEAD', '/head-request/test' ); 159 register_rest_route( 160 'head-request', '/test', array( 161 array( 162 'methods' => array( 'HEAD' ), 163 'callback' => '__return_true', 164 ), 165 array( 166 'methods' => array( 'GET' ), 167 'callback' => '__return_false', 168 'permission_callback' => array( $this, 'permission_denied' ), 169 ), 170 ) 171 ); 172 $request = new WP_REST_Request( 'HEAD', '/head-request/test' ); 161 173 $response = $this->server->dispatch( $request ); 162 174 $this->assertEquals( 200, $response->get_status() ); … … 165 177 public function test_url_params_no_numeric_keys() { 166 178 167 $this->server->register_route( 'test', '/test/(?P<data>.*)', array( 168 array( 169 'methods' => WP_REST_Server::READABLE, 170 'callback' => '__return_false', 171 'args' => array( 172 'data' => array(), 173 ), 174 ), 175 ) ); 179 $this->server->register_route( 180 'test', '/test/(?P<data>.*)', array( 181 array( 182 'methods' => WP_REST_Server::READABLE, 183 'callback' => '__return_false', 184 'args' => array( 185 'data' => array(), 186 ), 187 ), 188 ) 189 ); 176 190 177 191 $request = new WP_REST_Request( 'GET', '/test/some-value' ); … … 185 199 */ 186 200 function test_rest_route_capability_authorization_fails() { 187 register_rest_route( 'test-ns', '/test', array( 188 'methods' => 'GET', 189 'callback' => '__return_null', 190 'should_exist' => false, 191 'permission_callback' => array( $this, 'permission_denied' ), 192 ) ); 201 register_rest_route( 202 'test-ns', '/test', array( 203 'methods' => 'GET', 204 'callback' => '__return_null', 205 'should_exist' => false, 206 'permission_callback' => array( $this, 'permission_denied' ), 207 ) 208 ); 193 209 194 210 $request = new WP_REST_Request( 'GET', '/test-ns/test', array() ); 195 $result = $this->server->dispatch( $request );211 $result = $this->server->dispatch( $request ); 196 212 197 213 $this->assertEquals( 403, $result->get_status() ); … … 203 219 */ 204 220 function test_rest_route_capability_authorization() { 205 register_rest_route( 'test-ns', '/test', array( 206 'methods' => 'GET', 207 'callback' => '__return_null', 208 'should_exist' => false, 209 'permission_callback' => '__return_true', 210 ) ); 221 register_rest_route( 222 'test-ns', '/test', array( 223 'methods' => 'GET', 224 'callback' => '__return_null', 225 'should_exist' => false, 226 'permission_callback' => '__return_true', 227 ) 228 ); 211 229 212 230 $editor = self::factory()->user->create( array( 'role' => 'editor' ) ); … … 227 245 function test_allow_header_sent() { 228 246 229 register_rest_route( 'test-ns', '/test', array( 230 'methods' => 'GET', 231 'callback' => '__return_null', 232 'should_exist' => false, 233 ) ); 247 register_rest_route( 248 'test-ns', '/test', array( 249 'methods' => 'GET', 250 'callback' => '__return_null', 251 'should_exist' => false, 252 ) 253 ); 234 254 235 255 $request = new WP_REST_Request( 'GET', '/test-ns/test', array() ); … … 250 270 function test_allow_header_sent_with_multiple_methods() { 251 271 252 register_rest_route( 'test-ns', '/test', array( 253 'methods' => 'GET', 254 'callback' => '__return_null', 255 'should_exist' => false, 256 ) ); 257 258 register_rest_route( 'test-ns', '/test', array( 259 'methods' => 'POST', 260 'callback' => '__return_null', 261 'should_exist' => false, 262 ) ); 272 register_rest_route( 273 'test-ns', '/test', array( 274 'methods' => 'GET', 275 'callback' => '__return_null', 276 'should_exist' => false, 277 ) 278 ); 279 280 register_rest_route( 281 'test-ns', '/test', array( 282 'methods' => 'POST', 283 'callback' => '__return_null', 284 'should_exist' => false, 285 ) 286 ); 263 287 264 288 $request = new WP_REST_Request( 'GET', '/test-ns/test', array() ); … … 280 304 function test_allow_header_send_only_permitted_methods() { 281 305 282 register_rest_route( 'test-ns', '/test', array( 283 'methods' => 'GET', 284 'callback' => '__return_null', 285 'should_exist' => false, 286 'permission_callback' => array( $this, 'permission_denied' ), 287 ) ); 288 289 register_rest_route( 'test-ns', '/test', array( 290 'methods' => 'POST', 291 'callback' => '__return_null', 292 'should_exist' => false, 293 ) ); 306 register_rest_route( 307 'test-ns', '/test', array( 308 'methods' => 'GET', 309 'callback' => '__return_null', 310 'should_exist' => false, 311 'permission_callback' => array( $this, 'permission_denied' ), 312 ) 313 ); 314 315 register_rest_route( 316 'test-ns', '/test', array( 317 'methods' => 'POST', 318 'callback' => '__return_null', 319 'should_exist' => false, 320 ) 321 ); 294 322 295 323 $request = new WP_REST_Request( 'GET', '/test-ns/test', array() ); … … 305 333 306 334 public function test_allow_header_sent_on_options_request() { 307 register_rest_route( 'test-ns', '/test', array( 308 array( 309 'methods' => array( 'GET' ), 310 'callback' => '__return_null', 311 ), 312 array( 313 'methods' => array( 'POST' ), 314 'callback' => '__return_null', 315 'permission_callback' => '__return_null', 316 ), 317 ) ); 318 319 $request = new WP_REST_Request( 'OPTIONS', '/test-ns/test' ); 335 register_rest_route( 336 'test-ns', '/test', array( 337 array( 338 'methods' => array( 'GET' ), 339 'callback' => '__return_null', 340 ), 341 array( 342 'methods' => array( 'POST' ), 343 'callback' => '__return_null', 344 'permission_callback' => '__return_null', 345 ), 346 ) 347 ); 348 349 $request = new WP_REST_Request( 'OPTIONS', '/test-ns/test' ); 320 350 $response = $this->server->dispatch( $request ); 321 351 … … 344 374 $data = $response->get_data(); 345 375 346 $this->assertEquals( $code, 376 $this->assertEquals( $code, $data['code'] ); 347 377 $this->assertEquals( $message, $data['message'] ); 348 378 } … … 360 390 $data = $response->get_data(); 361 391 362 $this->assertEquals( $code, 392 $this->assertEquals( $code, $data['code'] ); 363 393 $this->assertEquals( $message, $data['message'] ); 364 394 } … … 369 399 $code2 = 'wp-api-test-error-2'; 370 400 $message2 = 'Another test message'; 371 $error = new WP_Error( $code, $message, array( 'status' => 400 ) );401 $error = new WP_Error( $code, $message, array( 'status' => 400 ) ); 372 402 $error->add( $code2, $message2, array( 'status' => 403 ) ); 373 403 … … 386 416 387 417 public function test_rest_error() { 388 $data = array(418 $data = array( 389 419 'code' => 'wp-api-test-error', 390 420 'message' => 'Message text', … … 405 435 ->with( $this->equalTo( 400 ) ); 406 436 407 $data = array(437 $data = array( 408 438 'code' => 'wp-api-test-error', 409 439 'message' => 'Message text', … … 438 468 public function test_link_embedding() { 439 469 // Register our testing route. 440 $this->server->register_route( 'test', '/test/embeddable', array( 441 'methods' => 'GET', 442 'callback' => array( $this, 'embedded_response_callback' ), 443 ) ); 470 $this->server->register_route( 471 'test', '/test/embeddable', array( 472 'methods' => 'GET', 473 'callback' => array( $this, 'embedded_response_callback' ), 474 ) 475 ); 444 476 $response = new WP_REST_Response(); 445 477 … … 469 501 $response->add_link( 'https://api.w.org/term', 'http://example.com/' ); 470 502 471 $data = $this->server->response_to_data( $response, false );503 $data = $this->server->response_to_data( $response, false ); 472 504 $links = $data['_links']; 473 505 … … 482 514 add_filter( 'rest_response_link_curies', array( $this, 'add_custom_curie' ) ); 483 515 484 $data = $this->server->response_to_data( $response, false );516 $data = $this->server->response_to_data( $response, false ); 485 517 $links = $data['_links']; 486 518 … … 509 541 public function test_link_embedding_self() { 510 542 // Register our testing route. 511 $this->server->register_route( 'test', '/test/embeddable', array( 512 'methods' => 'GET', 513 'callback' => array( $this, 'embedded_response_callback' ), 514 ) ); 543 $this->server->register_route( 544 'test', '/test/embeddable', array( 545 'methods' => 'GET', 546 'callback' => array( $this, 'embedded_response_callback' ), 547 ) 548 ); 515 549 $response = new WP_REST_Response(); 516 550 … … 528 562 public function test_link_embedding_params() { 529 563 // Register our testing route. 530 $this->server->register_route( 'test', '/test/embeddable', array( 531 'methods' => 'GET', 532 'callback' => array( $this, 'embedded_response_callback' ), 533 ) ); 564 $this->server->register_route( 565 'test', '/test/embeddable', array( 566 'methods' => 'GET', 567 'callback' => array( $this, 'embedded_response_callback' ), 568 ) 569 ); 534 570 535 571 $response = new WP_REST_Response(); 536 $url = rest_url( '/test/embeddable' );537 $url = add_query_arg( 'parsed_params', 'yes', $url );572 $url = rest_url( '/test/embeddable' ); 573 $url = add_query_arg( 'parsed_params', 'yes', $url ); 538 574 $response->add_link( 'alternate', $url, array( 'embeddable' => true ) ); 539 575 … … 552 588 public function test_link_embedding_error() { 553 589 // Register our testing route. 554 $this->server->register_route( 'test', '/test/embeddable', array( 555 'methods' => 'GET', 556 'callback' => array( $this, 'embedded_response_callback' ), 557 ) ); 590 $this->server->register_route( 591 'test', '/test/embeddable', array( 592 'methods' => 'GET', 593 'callback' => array( $this, 'embedded_response_callback' ), 594 ) 595 ); 558 596 559 597 $response = new WP_REST_Response(); 560 $url = rest_url( '/test/embeddable' );561 $url = add_query_arg( 'error', '1', $url );598 $url = rest_url( '/test/embeddable' ); 599 $url = add_query_arg( 'error', '1', $url ); 562 600 $response->add_link( 'up', $url, array( 'embeddable' => true ) ); 563 601 … … 573 611 $up_data = $up[0]; 574 612 $this->assertEquals( 'wp-api-test-error', $up_data['code'] ); 575 $this->assertEquals( 'Test message', 613 $this->assertEquals( 'Test message', $up_data['message'] ); 576 614 $this->assertEquals( 403, $up_data['data']['status'] ); 577 615 } … … 581 619 */ 582 620 public function test_link_embedding_without_links() { 583 $data = array(621 $data = array( 584 622 'untouched' => 'data', 585 623 ); … … 599 637 600 638 $data = array( 601 'hello' => true,639 'hello' => true, 602 640 'parameters' => $params, 603 641 ); … … 645 683 public function test_get_index() { 646 684 $server = new WP_REST_Server(); 647 $server->register_route( 'test/example', '/test/example/some-route', array( 648 array( 649 'methods' => WP_REST_Server::READABLE, 650 'callback' => '__return_true', 651 ), 652 array( 653 'methods' => WP_REST_Server::DELETABLE, 654 'callback' => '__return_true', 655 ), 656 ) ); 685 $server->register_route( 686 'test/example', '/test/example/some-route', array( 687 array( 688 'methods' => WP_REST_Server::READABLE, 689 'callback' => '__return_true', 690 ), 691 array( 692 'methods' => WP_REST_Server::DELETABLE, 693 'callback' => '__return_true', 694 ), 695 ) 696 ); 657 697 658 698 $request = new WP_REST_Request( 'GET', '/' ); 659 $index = $server->dispatch( $request );660 $data = $index->get_data();699 $index = $server->dispatch( $request ); 700 $data = $index->get_data(); 661 701 662 702 $this->assertArrayHasKey( 'name', $data ); … … 686 726 public function test_get_namespace_index() { 687 727 $server = new WP_REST_Server(); 688 $server->register_route( 'test/example', '/test/example/some-route', array( 689 array( 690 'methods' => WP_REST_Server::READABLE, 691 'callback' => '__return_true', 692 ), 693 array( 694 'methods' => WP_REST_Server::DELETABLE, 695 'callback' => '__return_true', 696 ), 697 ) ); 698 $server->register_route( 'test/another', '/test/another/route', array( 699 array( 700 'methods' => WP_REST_Server::READABLE, 701 'callback' => '__return_false', 702 ), 703 ) ); 728 $server->register_route( 729 'test/example', '/test/example/some-route', array( 730 array( 731 'methods' => WP_REST_Server::READABLE, 732 'callback' => '__return_true', 733 ), 734 array( 735 'methods' => WP_REST_Server::DELETABLE, 736 'callback' => '__return_true', 737 ), 738 ) 739 ); 740 $server->register_route( 741 'test/another', '/test/another/route', array( 742 array( 743 'methods' => WP_REST_Server::READABLE, 744 'callback' => '__return_false', 745 ), 746 ) 747 ); 704 748 705 749 $request = new WP_REST_Request(); 706 750 $request->set_param( 'namespace', 'test/example' ); 707 751 $index = rest_ensure_response( $server->get_namespace_index( $request ) ); 708 $data = $index->get_data();752 $data = $index->get_data(); 709 753 710 754 // Check top-level. … … 721 765 public function test_get_namespaces() { 722 766 $server = new WP_REST_Server(); 723 $server->register_route( 'test/example', '/test/example/some-route', array( 724 array( 725 'methods' => WP_REST_Server::READABLE, 726 'callback' => '__return_true', 727 ), 728 ) ); 729 $server->register_route( 'test/another', '/test/another/route', array( 730 array( 731 'methods' => WP_REST_Server::READABLE, 732 'callback' => '__return_false', 733 ), 734 ) ); 767 $server->register_route( 768 'test/example', '/test/example/some-route', array( 769 array( 770 'methods' => WP_REST_Server::READABLE, 771 'callback' => '__return_true', 772 ), 773 ) 774 ); 775 $server->register_route( 776 'test/another', '/test/another/route', array( 777 array( 778 'methods' => WP_REST_Server::READABLE, 779 'callback' => '__return_false', 780 ), 781 ) 782 ); 735 783 736 784 $namespaces = $server->get_namespaces(); … … 742 790 $request = new WP_REST_Request( 'GET', '/', array() ); 743 791 744 $result = $this->server->serve_request('/');792 $result = $this->server->serve_request( '/' ); 745 793 $headers = $this->server->sent_headers; 746 794 … … 767 815 $request = new WP_REST_Request( 'GET', '/', array() ); 768 816 769 $result = $this->server->serve_request('/');817 $result = $this->server->serve_request( '/' ); 770 818 $headers = $this->server->sent_headers; 771 819 … … 774 822 775 823 public function test_nocache_headers_on_authenticated_requests() { 776 $editor = self::factory()->user->create( array( 'role' => 'editor' ) );824 $editor = self::factory()->user->create( array( 'role' => 'editor' ) ); 777 825 $request = new WP_REST_Request( 'GET', '/', array() ); 778 826 wp_set_current_user( $editor ); 779 827 780 $result = $this->server->serve_request('/');828 $result = $this->server->serve_request( '/' ); 781 829 $headers = $this->server->sent_headers; 782 830 … … 795 843 796 844 public function test_no_nocache_headers_on_unauthenticated_requests() { 797 $editor = self::factory()->user->create( array( 'role' => 'editor' ) );845 $editor = self::factory()->user->create( array( 'role' => 'editor' ) ); 798 846 $request = new WP_REST_Request( 'GET', '/', array() ); 799 847 800 $result = $this->server->serve_request('/');848 $result = $this->server->serve_request( '/' ); 801 849 $headers = $this->server->sent_headers; 802 850 … … 808 856 public function test_serve_request_url_params_are_unslashed() { 809 857 810 $this->server->register_route( 'test', '/test/(?P<data>.*)', array( 811 array( 812 'methods' => WP_REST_Server::READABLE, 813 'callback' => '__return_false', 814 'args' => array( 815 'data' => array(), 816 ), 817 ), 818 ) ); 819 820 $result = $this->server->serve_request( '/test/data\\with\\slashes' ); 858 $this->server->register_route( 859 'test', '/test/(?P<data>.*)', array( 860 array( 861 'methods' => WP_REST_Server::READABLE, 862 'callback' => '__return_false', 863 'args' => array( 864 'data' => array(), 865 ), 866 ), 867 ) 868 ); 869 870 $result = $this->server->serve_request( '/test/data\\with\\slashes' ); 821 871 $url_params = $this->server->last_request->get_url_params(); 822 872 $this->assertEquals( 'data\\with\\slashes', $url_params['data'] ); … … 825 875 public function test_serve_request_query_params_are_unslashed() { 826 876 827 $this->server->register_route( 'test', '/test', array( 877 $this->server->register_route( 878 'test', '/test', array( 879 array( 880 'methods' => WP_REST_Server::READABLE, 881 'callback' => '__return_false', 882 'args' => array( 883 'data' => array(), 884 ), 885 ), 886 ) 887 ); 888 889 // WordPress internally will slash the superglobals on bootstrap 890 $_GET = wp_slash( 828 891 array( 829 'methods' => WP_REST_Server::READABLE, 830 'callback' => '__return_false', 831 'args' => array( 832 'data' => array(), 833 ), 834 ), 835 ) ); 836 837 // WordPress internally will slash the superglobals on bootstrap 838 $_GET = wp_slash( array( 839 'data' => 'data\\with\\slashes', 840 ) ); 841 842 $result = $this->server->serve_request( '/test' ); 892 'data' => 'data\\with\\slashes', 893 ) 894 ); 895 896 $result = $this->server->serve_request( '/test' ); 843 897 $query_params = $this->server->last_request->get_query_params(); 844 898 $this->assertEquals( 'data\\with\\slashes', $query_params['data'] ); … … 847 901 public function test_serve_request_body_params_are_unslashed() { 848 902 849 $this->server->register_route( 'test', '/test', array( 903 $this->server->register_route( 904 'test', '/test', array( 905 array( 906 'methods' => WP_REST_Server::READABLE, 907 'callback' => '__return_false', 908 'args' => array( 909 'data' => array(), 910 ), 911 ), 912 ) 913 ); 914 915 // WordPress internally will slash the superglobals on bootstrap 916 $_POST = wp_slash( 850 917 array( 851 'methods' => WP_REST_Server::READABLE, 852 'callback' => '__return_false', 853 'args' => array( 854 'data' => array(), 855 ), 856 ), 857 ) ); 858 859 // WordPress internally will slash the superglobals on bootstrap 860 $_POST = wp_slash( array( 861 'data' => 'data\\with\\slashes', 862 ) ); 918 'data' => 'data\\with\\slashes', 919 ) 920 ); 863 921 864 922 $result = $this->server->serve_request( '/test/data' ); … … 870 928 public function test_serve_request_json_params_are_unslashed() { 871 929 872 $this->server->register_route( 'test', '/test', array( 930 $this->server->register_route( 931 'test', '/test', array( 932 array( 933 'methods' => WP_REST_Server::READABLE, 934 'callback' => '__return_false', 935 'args' => array( 936 'data' => array(), 937 ), 938 ), 939 ) 940 ); 941 942 $_SERVER['HTTP_CONTENT_TYPE'] = 'application/json'; 943 $GLOBALS['HTTP_RAW_POST_DATA'] = json_encode( 873 944 array( 874 'methods' => WP_REST_Server::READABLE, 875 'callback' => '__return_false', 876 'args' => array( 877 'data' => array(), 878 ), 879 ), 880 ) ); 881 882 $_SERVER['HTTP_CONTENT_TYPE'] = 'application/json'; 883 $GLOBALS['HTTP_RAW_POST_DATA'] = json_encode( array( 884 'data' => 'data\\with\\slashes', 885 ) ); 886 887 $result = $this->server->serve_request( '/test' ); 945 'data' => 'data\\with\\slashes', 946 ) 947 ); 948 949 $result = $this->server->serve_request( '/test' ); 888 950 $json_params = $this->server->last_request->get_json_params(); 889 951 $this->assertEquals( 'data\\with\\slashes', $json_params['data'] ); … … 892 954 public function test_serve_request_file_params_are_unslashed() { 893 955 894 $this->server->register_route( 'test', '/test', array( 895 array( 896 'methods' => WP_REST_Server::READABLE, 897 'callback' => '__return_false', 898 'args' => array( 899 'data' => array(), 900 ), 901 ), 902 ) ); 956 $this->server->register_route( 957 'test', '/test', array( 958 array( 959 'methods' => WP_REST_Server::READABLE, 960 'callback' => '__return_false', 961 'args' => array( 962 'data' => array(), 963 ), 964 ), 965 ) 966 ); 903 967 904 968 // WordPress internally will slash the superglobals on bootstrap … … 909 973 ); 910 974 911 $result = $this->server->serve_request( '/test/data\\with\\slashes' );975 $result = $this->server->serve_request( '/test/data\\with\\slashes' ); 912 976 $file_params = $this->server->last_request->get_file_params(); 913 977 $this->assertEquals( 'data\\with\\slashes', $file_params['data']['name'] ); … … 916 980 public function test_serve_request_headers_are_unslashed() { 917 981 918 $this->server->register_route( 'test', '/test', array( 919 array( 920 'methods' => WP_REST_Server::READABLE, 921 'callback' => '__return_false', 922 'args' => array( 923 'data' => array(), 924 ), 925 ), 926 ) ); 982 $this->server->register_route( 983 'test', '/test', array( 984 array( 985 'methods' => WP_REST_Server::READABLE, 986 'callback' => '__return_false', 987 'args' => array( 988 'data' => array(), 989 ), 990 ), 991 ) 992 ); 927 993 928 994 // WordPress internally will slash the superglobals on bootstrap … … 930 996 931 997 $result = $this->server->serve_request( '/test/data\\with\\slashes' ); 932 $this->assertEquals( 'data\\with\\slashes', $this->server->last_request->get_header( 'x_my_header' ) );998 $this->assertEquals( 'data\\with\\slashes', $this->server->last_request->get_header( 'x_my_header' ) ); 933 999 } 934 1000 … … 989 1055 */ 990 1056 public function test_rest_validate_before_sanitization() { 991 register_rest_route( 'test-ns', '/test', array( 992 'methods' => array( 'GET' ), 993 'callback' => '__return_null', 994 'args' => array( 995 'someinteger' => array( 996 'validate_callback' => array( $this, '_validate_as_integer_123' ), 997 'sanitize_callback' => 'absint', 998 ), 999 'somestring' => array( 1000 'validate_callback' => array( $this, '_validate_as_string_foo' ), 1001 'sanitize_callback' => 'absint', 1002 ), 1003 ), 1004 ) ); 1057 register_rest_route( 1058 'test-ns', '/test', array( 1059 'methods' => array( 'GET' ), 1060 'callback' => '__return_null', 1061 'args' => array( 1062 'someinteger' => array( 1063 'validate_callback' => array( $this, '_validate_as_integer_123' ), 1064 'sanitize_callback' => 'absint', 1065 ), 1066 'somestring' => array( 1067 'validate_callback' => array( $this, '_validate_as_string_foo' ), 1068 'sanitize_callback' => 'absint', 1069 ), 1070 ), 1071 ) 1072 ); 1005 1073 1006 1074 $request = new WP_REST_Request( 'GET', '/test-ns/test' ); 1007 $request->set_query_params( array( 'someinteger' => 123, 'somestring' => 'foo' ) ); 1075 $request->set_query_params( 1076 array( 1077 'someinteger' => 123, 1078 'somestring' => 'foo', 1079 ) 1080 ); 1008 1081 $response = $this->server->dispatch( $request ); 1009 1082
Note: See TracChangeset
for help on using the changeset viewer.