forked from pavlokomarov/roach-php-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestTest.php
More file actions
113 lines (86 loc) · 3.13 KB
/
RequestTest.php
File metadata and controls
113 lines (86 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
/**
* Copyright (c) 2021 Kai Sassnowski
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @see https://github.com/roach-php/roach
*/
namespace RoachPHP\Tests\Http;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use PHPUnit\Framework\TestCase;
use RoachPHP\Http\Response;
use RoachPHP\Spider\ParseResult;
use RoachPHP\Support\DroppableInterface;
use RoachPHP\Tests\InteractsWithRequestsAndResponses;
use RoachPHP\Tests\Support\DroppableTest;
/**
* @group http
*
* @internal
*/
final class RequestTest extends TestCase
{
use InteractsWithRequestsAndResponses;
use DroppableTest;
public function testCanAccessTheRequestUri(): void
{
$request = $this->makeRequest('::request-uri::');
self::assertSame('::request-uri::', $request->getUri());
}
public function testCanAccessTheRequestUriPath(): void
{
$request = $this->makeRequest('https://::request-uri::/::path::');
self::assertSame('/::path::', $request->getPath());
}
public function testCanAddHeader(): void
{
$request = $this->makeRequest();
self::assertFalse($request->hasHeader('X-Custom-Header'));
$newRequest = $request->addHeader('X-Custom-Header', '::value::');
self::assertFalse($request->hasHeader('X-Custom-Header'));
self::assertTrue($newRequest->hasHeader('X-Custom-Header'));
self::assertSame(['::value::'], $newRequest->getHeader('X-Custom-Header'));
}
public function testCanManipulateUnderlyingGuzzleRequest(): void
{
$request = $this->makeRequest();
self::assertFalse($request->hasHeader('X-Custom-Header'));
$request->withPsrRequest(static function (Request $guzzleRequest) {
return $guzzleRequest->withHeader('X-Custom-Header', '::value::');
});
self::assertTrue($request->hasHeader('X-Custom-Header'));
self::assertSame(['::value::'], $request->getHeader('X-Custom-Header'));
}
public function testCanCallParseCallback(): void
{
$called = false;
$request = $this->makeRequest(callback: static function (Response $response) use (&$called) {
$called = true;
yield ParseResult::item(['::item::']);
});
$request->callback(
new Response(new GuzzleResponse(), $request),
)->next();
self::assertTrue($called);
}
public function testCanAddMetaDataToRequest(): void
{
$request = $this->makeRequest();
self::assertNull($request->getMeta('::meta-key::'));
$request = $request->withMeta('::meta-key::', '::meta-value::');
self::assertSame('::meta-value::', $request->getMeta('::meta-key::'));
}
public function testReturnsUnderlyingGuzzleRequest(): void
{
$request = $this->makeRequest('::request-uri::');
self::assertSame('::request-uri::', (string) $request->getPsrRequest()->getUri());
}
protected function createDroppable(): DroppableInterface
{
return $this->makeRequest();
}
}