Make WordPress Core


Ignore:
Timestamp:
12/04/2015 11:35:54 PM (9 years ago)
Author:
wonderboymusic
Message:

REST API: Core typically sends nocache headers on all auth'ed responses, as in wp, admin-ajax, etc. Because the REST API infrastructure is hooked in pre-wp, we should be setting this ourselves.

Adds unit tests.

Props joehoyle.
Fixes #34832.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r35758 r35773  
    620620        $this->assertContains( 'test/another', $namespaces );
    621621    }
     622
     623    public function test_nocache_headers_on_authenticated_requests() {
     624        $editor = self::factory()->user->create( array( 'role' => 'editor' ) );
     625        $request = new WP_REST_Request( 'GET', '/', array() );
     626        wp_set_current_user( $editor );
     627
     628        $result = $this->server->serve_request('/');
     629        $headers = $this->server->sent_headers;
     630
     631        foreach ( wp_get_nocache_headers() as $header => $value ) {
     632            $this->assertTrue( isset( $headers[ $header ] ), sprintf( 'Header %s is not present in the response.', $header ) );
     633            $this->assertEquals( $value, $headers[ $header ] );
     634        }
     635    }
     636
     637    public function test_no_nocache_headers_on_unauthenticated_requests() {
     638        $editor = self::factory()->user->create( array( 'role' => 'editor' ) );
     639        $request = new WP_REST_Request( 'GET', '/', array() );
     640
     641        $result = $this->server->serve_request('/');
     642        $headers = $this->server->sent_headers;
     643
     644        foreach ( wp_get_nocache_headers() as $header => $value ) {
     645            $this->assertFalse( isset( $headers[ $header ] ) && $headers[ $header ] === $value, sprintf( 'Header %s is set to nocache.', $header ) );
     646        }
     647    }
    622648}
Note: See TracChangeset for help on using the changeset viewer.