Skip to content

Commit

Permalink
Expose more request data, rename private classes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwathan committed Jun 30, 2017
1 parent 059adce commit 2a3b090
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/Zttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class Zttp
{
static function __callStatic($method, $args)
{
return ZttpRequest::new()->{$method}(...$args);
return PendingZttpRequest::new()->{$method}(...$args);
}
}

class ZttpRequest
class PendingZttpRequest
{
function __construct()
{
Expand Down Expand Up @@ -153,7 +153,7 @@ function buildBeforeSendingHandler()
function runBeforeSendingCallback($request)
{
return tap($request, function ($request) {
($this->beforeSendingCallback)(new PendingZttpRequest($request));
($this->beforeSendingCallback)(new ZttpRequest($request));
});
}

Expand All @@ -170,11 +170,21 @@ function parseQueryParams($url)
}
}

class PendingZttpRequest
class ZttpRequest
{
function __construct($guzzleRequest)
function __construct($request)
{
$this->request = $request;
}

function url()
{
return (string) $this->request->getUri();
}

function method()
{
$this->request = $guzzleRequest;
return $this->request->getMethod();
}

function body()
Expand Down
4 changes: 4 additions & 0 deletions tests/ZttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,10 +392,14 @@ function a_callback_can_be_run_before_sending_the_request()
$state = [];

$response = Zttp::beforeSending(function ($request) use (&$state) {
$state['url'] = $request->url();
$state['method'] = $request->method();
$state['headers'] = $request->headers();
$state['body'] = $request->body();
})->withHeaders(['Z-Status' => 200])->post($this->url('/post'), ['foo' => 'bar']);

$this->assertEquals($this->url('/post'), $state['url']);
$this->assertEquals('POST', $state['method']);
$this->assertArrayHasKey('User-Agent', $state['headers']);
$this->assertEquals(200, $state['headers']['Z-Status']);
$this->assertEquals(json_encode(['foo' => 'bar']), $state['body']);
Expand Down

0 comments on commit 2a3b090

Please sign in to comment.