Re: Re: HTTP_Request2

From: Date: Wed, 29 Oct 2008 08:25:48 +0000
Subject: Re: Re: HTTP_Request2
References: 1 2 3 4 5 6 7  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-50964@lists.php.net to get a copy of this message
Daniel O'Connor a écrit :
        Don't forget that if there was a mock http adapter which can provide
        canned/simulated responses, loads of packages in PEAR just got a
        lot more
        easy to unit test.
    yes but...
    what theses responses will contain ? static text ?
    Are you going to implement a mock HTTP server to build relevant
    responses ?
    But you have a point, client/sever applications are hard to
    unit-test, this is the cruel real world, there are many, differents,
    erratic http servers in the interweb...
Absolutely :) Think about testing packages like XML_RPC2 - it currently relies on live servers and there's network latency - slow tests, if the server falls over you spend time thinking "arggh, I broke something" when its not your fault. XML_RPC2 doesn't care about the thousands of little edge cases that HTTP_Request2 can handle (and presumably have unit tested also :P). It only cares about class MockErroredHTTPRequest extends MockHTTPRequestDriver {
    public function get() {
        throw new HTTP_Request2_Exception("BLARGH");
    }
} class MockSuccessHTTPRequest extends MockHTTPRequestDriver {
    public function get() {
        return file_get_contents('my_canned_response.xml');
    }
} *snip* public function testShouldGracefullyFailWhenHTTPProblemsExists() {
    $client = new XML_RPC2();
    $client->setDriver(new HTTP_Request2(new MockErroredHTTPRequest());
    try {
        $client->doSomething();
    } catch (HTTP_Request2_Exception $e) {
        $this->fail("Allowed an exception to bubble up too far!");
    }
} .. or similar
Sure... but actually this is already possible with the implementation I proposed: you would just do: class MockConnection extends HTTP_Connection {
    protected function open() {
        // whatever you want here
    }
    protected function send(HTTP_Request2 $request)  {
        // whatever you want here
    }
    protected function receive()  {
        $response = new HTTP_Response2();
        $response->code = 200;
        $response->body = file_get_contents('my_canned_response.xml');
        // whatever you want here
        return $response;
    }
    protected function close() {
        // whatever you want here
    }
} $request = new HTTP_Request2(); $request->connection = new MockConnection(); ... see: http://code.google.com/p/izi-sandbox/source/browse/trunk/HTTP_Connection/HTTP/Connection.php -- David

« previous php.pear.dev (#50964) next »