Make WordPress Core

Ticket #24987: 24987.diff

File 24987.diff, 3.1 KB (added by dd32, 12 years ago)

Requires 21182.3.diff​ includes Changes and Unit Tests

  • tests/tests/http/base.php

     
    259259
    260260        }
    261261
     262        /**
     263         * Test HTTP Cookie handling
     264         *
     265         * @ticket 21182
     266         */
     267        function test_cookie_handling() {
     268                $url = 'http://api.wordpress.org/core/tests/1.0/redirection.php?cookie-test=1';
     269
     270                $res = wp_remote_get( $url );
     271                $this->assertEquals( 'PASS', wp_remote_retrieve_body( $res ) );
     272        }
    262273}
  • tests/data/WPHTTP-testcase-redirection-script.php

     
    9696        exit;
    9797}
    9898
     99if ( isset( $_GET['cookie-test'] ) ) {
     100        if ( 'test-cookie' != $_GET['cookie-test'] ) {
     101                setcookie( 'api_test_cookie', 'value', time() + 365*24*60*60, '/core/tests/1.0/', 'api.wordpress.org' );
     102                setcookie( 'api_test_cookie_minimal', 'value'  );
     103                setcookie( 'api_test_cookie_wrong_host', 'value', time() + 365*24*60*60, '/', 'example.com' );
     104                setcookie( 'api_test_wildcard_domain', 'value', time() + 365*24*60*60, '/', '.wordpress.org' );
     105                setcookie( 'api_test_cookie_expired', 'value', time() - 365*24*60*60, '/', '.wordpress.org' );
     106                header( "Location: $url?cookie-test=test-cookie" );
     107                exit;
     108        }
     109
     110        if ( empty( $_COOKIE['api_test_cookie'] ) || 'value' != $_COOKIE['api_test_cookie'] )
     111                die( 'FAIL_NO_COOKIE' );
     112        if ( empty( $_COOKIE['api_test_cookie_minimal'] ) )
     113                die( 'FAIL_NO_MINIMAL' );
     114        if ( isset( $_COOKIE['api_test_cookie_wrong_host'] ) )
     115                die( 'FAIL_WRONG_HOST' );
     116        if ( empty( $_COOKIE['api_test_wildcard_domain'] ) )
     117                die( 'FAIL_NO_WILDCARD' );
     118        if ( isset( $_COOKIE['api_test_cookie_expired'] ) )
     119                die( 'FAIL_EXPIRED_COOKIE' );
     120
     121        echo 'PASS';
     122        exit;
     123}
     124
     125
    99126$rt = isset($_GET['rt']) ? $_GET['rt'] : 5;
    100127$r = isset($_GET['r']) ? $_GET['r'] : 0;
    101128
  • src/wp-includes/class-http.php

     
    195195                                $r['headers']['Content-Length'] = strlen( $r['body'] );
    196196                }
    197197
    198                 return $this->_dispatch_request($url, $r);
     198                $response = $this->_dispatch_request($url, $r);
     199
     200                // Append cookies that were used in this request to the response
     201                if ( ! empty( $r['cookies'] ) ) {
     202                        $cookies_set = wp_list_pluck( $response['cookies'], 'name' );
     203                        foreach ( $r['cookies'] as $cookie ) {
     204                                if ( ! in_array( $cookie->name, $cookies_set ) && $cookie->test( $url ) ) {
     205                                        $response['cookies'][] = $cookie;
     206                                }
     207                        }
     208                }
     209
     210                return $response;
    199211        }
    200212
    201213        /**
     
    638650                                $args['method'] = 'GET';
    639651                }
    640652
     653                // Include valid cookies in the redirect process
     654                if ( ! empty( $response['cookies'] ) ) {
     655                        foreach ( $response['cookies'] as $cookie ) {
     656                                if ( $cookie->test( $redirect_location ) )
     657                                        $args['cookies'][] = $cookie;
     658                        }
     659                }
     660
    641661                return wp_remote_request( $redirect_location, $args ); 
    642662        }
    643663}