Skip to content

Commit

Permalink
Add information about testing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JDGrimes committed Aug 9, 2014
1 parent 102427c commit d75ba25
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ PHPUnit testcase for testing code that uses WordPress's `WP_Http` class.

# Usage

## Mocking Responses

If you use `wp_remote_request()` or other wrappers for `WP_Http` methods in your
code, this makes it difficult to test, especially if the remote server may not be
reachable from your testing environment. This testcase solves this by letting you
Expand Down Expand Up @@ -36,3 +38,29 @@ protected function mock_server_response( $request, $url ) {
For a full list of the `$request` and response arguments, see
[`WP_Http::request()`](http://developer.wordpress.org/reference/classes/wp_http/
request/#source-code)

## Testing Requests

You may also wish to test that your code is making requests as expected. You can do
this by checking the value of `$this->http_requests`, which is an array of requests.
Each entry in the array stores the request arguments (`'request'` key) and URL
(`'url'` key).

To check that a request was made, you could do something like this:

```php
$this->assertCount( 1, $this->http_requests );
$this->assertEquals( 'http://example.com/', $this->http_requests[0]['url'] );
```

When you just want to test the request and don't care about the response, you can
short-circuit the request before it is made, by setting the response mocker to be
`__return_true()`:

```php
$this->http_responder = '__return_true';

my_prefix_make_request();

$this->assertCount( 1, $this->http_requests );
```

0 comments on commit d75ba25

Please sign in to comment.