Make WordPress Core

Ticket #13841: 13841-unit-test.diff

File 13841-unit-test.diff, 2.2 KB (added by kurtpayne, 13 years ago)

Alternate unit test for http timeouts

  • wp-testcase/test_http.php

     
    144144
    145145                unlink($res['filename']); // Remove the temporary file
    146146        }
     147       
     148        /**
     149         * Test that the timeouts are functioning as inteded
     150         * @return void
     151         */
     152        public function test_timeout() {
     153                $this->knownWPBug( 13841 );
     154               
     155                // Create a fake server to make requests from
     156                $server = new SimpleSocketServer();
     157                try {
     158                        $server->start( '127.0.0.1', 8112 );
     159                } catch ( Exception $e ) {
     160                        $this->markTestIncomplete( "Could not start socket server: " . $e->getMessage() );
     161                }
     162               
     163                // Make the request and time out
     164                $url = 'http://127.0.0.1:8112/';
     165                $start = microtime( true );
     166                $res = wp_remote_request( $url, array( 'timeout' => 1 ) );
     167                $stop = microtime( true );
     168                $server->stop();
     169
     170                // Make sure it's a timeout
     171                $this->assertTrue( is_wp_error( $res ) );
     172                $this->assertEquals( 'http_request_failed', $res->get_error_code() );
     173
     174                // Make sure the runtime was between .9 and 1.1 seconds
     175                $this->assertGreaterThanOrEqual( .9, ($stop - $start) );
     176                $this->assertLessThanOrEqual( 1.1, ($stop - $start) );
     177        }
    147178}
    148179
    149180// Stubs to test each transport
     
    155186}
    156187class WPHTTP_fsockopen extends _WPHTTP {
    157188        var $transport = 'fsockopen';
    158 }
    159  No newline at end of file
     189}
     190
     191/**
     192 * Simple socket server
     193 *
     194 * Create a server which never responds.  This is
     195 * fairly useless, unless you're testing timeouts.
     196 *
     197 * @package    WordPress
     198 * @subpackage Unit Tests
     199 * @since      3.4.0
     200 */
     201class SimpleSocketServer {
     202
     203        /**
     204         * Socket
     205         * @var mixed
     206         */
     207        protected $_sock = null;
     208
     209        /**
     210         * Start listening
     211         * @param string $ip
     212         * @param int $port
     213         * @throws Exception
     214         * @return void
     215         */
     216        public function start( $ip, $port ) {           
     217                $this->_sock = stream_socket_server( "tcp://$ip:$port", $errno, $errstr );
     218                stream_set_blocking( $this->_sock, 0 );
     219        }
     220
     221        /**
     222         * Stop listening
     223         * @return void
     224         */
     225        public function stop() {
     226                if ( null !== $this->_sock ) {
     227                        fclose( $this->_sock );
     228                }
     229        }
     230       
     231        /**
     232         * Destructor
     233         * @return void
     234         */
     235        public function __destruct() {
     236                $this->stop;
     237        }
     238}
     239