Skip to content

Commit

Permalink
tests(http): covers http client/server request/response. (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs authored Jul 29, 2020
1 parent 3486acc commit 0e7826a
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/Zipkin/Instrumentation/Http/Client/Psr18/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function getMethod(): string
*/
public function getPath(): ?string
{
return $this->delegate->getUri()->getPath();
return $this->delegate->getUri()->getPath() ?: '/';
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Zipkin/Instrumentation/Http/Client/Psr18/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

namespace Zipkin\Instrumentation\Http\Client\Psr18;

use Zipkin\Instrumentation\Http\Request as HttpRequest;
use Zipkin\Instrumentation\Http\Client\Response as ClientResponse;
use Zipkin\Instrumentation\Http\Client\Request as ClientRequest;
use Psr\Http\Message\ResponseInterface;

final class Response extends ClientResponse
Expand All @@ -31,7 +31,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function getRequest(): ?HttpRequest
public function getRequest(): ?ClientRequest
{
return $this->request;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Zipkin/Instrumentation/Http/Client/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Zipkin\Instrumentation\Http\Request as HttpRequest;

/**
* {@inheritdoc}
*/
abstract class Request extends HttpRequest
{
}
10 changes: 10 additions & 0 deletions src/Zipkin/Instrumentation/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

use Zipkin\Instrumentation\Http\Response as HttpResponse;

/**
* {@inheritdoc}
*/
abstract class Response extends HttpResponse
{
/**
* {@inheritdoc}
*/
public function getRequest(): ?Request
{
return null;
}
}
2 changes: 2 additions & 0 deletions src/Zipkin/Instrumentation/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

/**
* Abstract request type used for parsing and sampling of http clients and servers.
*
* @internal
*/
abstract class Request
{
Expand Down
13 changes: 9 additions & 4 deletions src/Zipkin/Instrumentation/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@

namespace Zipkin\Instrumentation\Http;

/**
* Abstract response type used for parsing and sampling of http clients and servers.
*
* @internal
*/
abstract class Response
{
/**
* The request that initiated this response or {@code null} if unknown.
*
* @return Request|null not declared on purpose so client/server responses can
* declare its own type.
*/
public function getRequest(): ?Request
{
return null;
}
abstract public function getRequest();

/**
* The HTTP status code or zero if unreadable.
Expand Down
5 changes: 4 additions & 1 deletion src/Zipkin/Instrumentation/Http/Server/Psr15/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Zipkin\Instrumentation\Http\Server\Request as ServerRequest;
use Psr\Http\Message\RequestInterface;

/**
* {@inheritdoc}
*/
final class Request extends ServerRequest
{
/**
Expand All @@ -32,7 +35,7 @@ public function getMethod(): string
*/
public function getPath(): ?string
{
return $this->delegate->getUri()->getPath();
return $this->delegate->getUri()->getPath() ?: '/';
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Zipkin/Instrumentation/Http/Server/Psr15/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Zipkin\Instrumentation\Http\Server\Psr15;

use Zipkin\Instrumentation\Http\Server\Response as ServerResponse;
use Zipkin\Instrumentation\Http\Request;
use Zipkin\Instrumentation\Http\Server\Request as ServerRequest;
use Psr\Http\Message\ResponseInterface;

/**
* {@inheritdoc}
*/
final class Response extends ServerResponse
{
/**
Expand All @@ -31,7 +34,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function getRequest(): ?Request
public function getRequest(): ?ServerRequest
{
return $this->request;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Zipkin/Instrumentation/Http/Server/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Zipkin\Instrumentation\Http\Request as HttpRequest;

/**
* {@inheritdoc}
*/
abstract class Request extends HttpRequest
{
/**
Expand Down
10 changes: 10 additions & 0 deletions src/Zipkin/Instrumentation/Http/Server/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

use Zipkin\Instrumentation\Http\Response as HttpResponse;

/**
* {@inheritdoc}
*/
abstract class Response extends HttpResponse
{
/**
* {@inheritdoc}
*/
public function getRequest(): ?Request
{
return null;
}
}
41 changes: 41 additions & 0 deletions tests/Unit/Instrumentation/Http/Client/Psr18/RequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace ZipkinTests\Unit\Instrumentation\Http\Client\Psr18;

use Zipkin\Instrumentation\Http\Client\Psr18\Request;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Psr7\Request as Psr7Request;

final class RequestTest extends TestCase
{
public function testRequestIsCreatedSuccessfully()
{
$delegateRequest = new Psr7Request('GET', 'http://test.com/path', ['test_key' => 'test_value']);
$request = new Request($delegateRequest);
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/path', $request->getPath());
$this->assertNull($request->getHeader('test_missing_key'));
$this->assertEquals('test_value', $request->getHeader('test_key'));
$this->assertSame($delegateRequest, $request->unwrap());
}

/**
* @dataProvider emptyPaths
*/
public function testRequestIsNormalizesEmptyPath(string $path)
{
$delegateRequest = new Psr7Request('GET', $path, ['test_key' => 'test_value']);
$request = new Request($delegateRequest);
$this->assertEquals('/', $request->getPath());
}

public function emptyPaths(): array
{
return [
['http://test.com'],
['http://test.com/'],
];
}
}
34 changes: 34 additions & 0 deletions tests/Unit/Instrumentation/Http/Client/Psr18/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace ZipkinTests\Unit\Instrumentation\Http\Client\Psr18;

use Zipkin\Instrumentation\Http\Client\Psr18\Response as Psr18Response;
use Zipkin\Instrumentation\Http\Client\Psr18\Request;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request as Psr7Request;

final class ResponseTest extends TestCase
{
/**
* @dataProvider delegateRequests
*/
public function testResponseIsCreatedSuccessfully($request)
{
$delegateResponse = new Response(202);
$response = new Psr18Response($delegateResponse, $request);
$this->assertEquals(202, $response->getStatusCode());
$this->assertSame($request, $response->getRequest());
$this->assertSame($delegateResponse, $response->unwrap());
}

public function delegateRequests(): array
{
return [
[null],
[new Request(new Psr7Request('GET', 'http://test.com/path'))],
];
}
}
41 changes: 41 additions & 0 deletions tests/Unit/Instrumentation/Http/Server/Psr15/RequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace ZipkinTests\Unit\Instrumentation\Http\Server\Psr15;

use Zipkin\Instrumentation\Http\Server\Psr15\Request;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Psr7\Request as Psr7Request;

final class RequestTest extends TestCase
{
public function testRequestIsCreatedSuccessfully()
{
$delegateRequest = new Psr7Request('GET', 'http://test.com/path', ['test_key' => 'test_value']);
$request = new Request($delegateRequest);
$this->assertEquals('GET', $request->getMethod());
$this->assertEquals('/path', $request->getPath());
$this->assertNull($request->getHeader('test_missing_key'));
$this->assertEquals('test_value', $request->getHeader('test_key'));
$this->assertSame($delegateRequest, $request->unwrap());
}

/**
* @dataProvider emptyPaths
*/
public function testRequestIsNormalizesEmptyPath(string $path)
{
$delegateRequest = new Psr7Request('GET', 'http://test.com', ['test_key' => 'test_value']);
$request = new Request($delegateRequest);
$this->assertEquals('/', $request->getPath());
}

public function emptyPaths(): array
{
return [
['http://test.com'],
['http://test.com/'],
];
}
}
34 changes: 34 additions & 0 deletions tests/Unit/Instrumentation/Http/Server/Psr15/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace ZipkinTests\Unit\Instrumentation\Http\Server\Psr15;

use Zipkin\Instrumentation\Http\Server\Psr15\Response as Psr15Response;
use Zipkin\Instrumentation\Http\Server\Psr15\Request;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Request as Psr7Request;

final class ResponseTest extends TestCase
{
/**
* @dataProvider delegateRequests
*/
public function testResponseIsCreatedSuccessfully($request)
{
$delegateResponse = new Response(202);
$response = new Psr15Response($delegateResponse, $request);
$this->assertEquals(202, $response->getStatusCode());
$this->assertSame($request, $response->getRequest());
$this->assertSame($delegateResponse, $response->unwrap());
}

public function delegateRequests(): array
{
return [
[null],
[new Request(new Psr7Request('GET', 'http://test.com/path'))],
];
}
}

0 comments on commit 0e7826a

Please sign in to comment.