1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Bip70\Test\Client\GuzzleClient; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
use Bip70\Client\GuzzleHttpClient; |
10
|
|
|
use Bip70\X509\RequestValidation; |
11
|
|
|
use Bip70\X509\TrustStoreLoader; |
12
|
|
|
use GuzzleHttp\Handler\MockHandler; |
13
|
|
|
use GuzzleHttp\HandlerStack; |
14
|
|
|
use GuzzleHttp\Middleware; |
15
|
|
|
use GuzzleHttp\Psr7\Response; |
16
|
|
|
use X509\CertificationPath\PathValidation\PathValidationConfig; |
17
|
|
|
|
18
|
|
|
class ClientHeadersTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testMissingContentType() |
21
|
|
|
{ |
22
|
|
|
$container = []; |
23
|
|
|
$history = Middleware::history($container); |
24
|
|
|
|
25
|
|
|
$handler = new MockHandler([ |
26
|
|
|
// response to our GET PAYMENTREQUEST request |
27
|
|
|
new Response(200, [], '{"result":true}') |
28
|
|
|
]); |
29
|
|
|
|
30
|
|
|
$stack = HandlerStack::create($handler); |
31
|
|
|
$stack->push($history); |
32
|
|
|
|
33
|
|
|
$mockClient = new \GuzzleHttp\Client([ |
34
|
|
|
'handler' => $stack, |
35
|
|
|
]); |
36
|
|
|
|
37
|
|
|
$client = new GuzzleHttpClient($mockClient); |
38
|
|
|
|
39
|
|
|
$requestUrl = "https://development.bip70.local/invoice/1"; |
40
|
|
|
|
41
|
|
|
// 10/12/2017 ish |
42
|
|
|
$now = new \DateTimeImmutable(); |
43
|
|
|
$now = $now->setTimestamp(1509692666); |
44
|
|
|
|
45
|
|
|
$validationConfig = new PathValidationConfig($now, 10); |
|
|
|
|
46
|
|
|
$validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem()); |
47
|
|
|
|
48
|
|
|
$this->expectException(\RuntimeException::class); |
49
|
|
|
$this->expectExceptionMessage("Missing content-type header"); |
50
|
|
|
|
51
|
|
|
$client->getRequest($requestUrl, $validator); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testInvalidContentType() |
55
|
|
|
{ |
56
|
|
|
$container = []; |
57
|
|
|
$history = Middleware::history($container); |
58
|
|
|
|
59
|
|
|
$handler = new MockHandler([ |
60
|
|
|
// response to our GET PAYMENTREQUEST request |
61
|
|
|
new Response(200, [ |
62
|
|
|
'Content-Type' => [ |
63
|
|
|
'application/json', |
64
|
|
|
] |
65
|
|
|
], '{"result":true}') |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$stack = HandlerStack::create($handler); |
69
|
|
|
$stack->push($history); |
70
|
|
|
|
71
|
|
|
$mockClient = new \GuzzleHttp\Client([ |
72
|
|
|
'handler' => $stack, |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$client = new GuzzleHttpClient($mockClient); |
76
|
|
|
|
77
|
|
|
$requestUrl = "https://development.bip70.local/invoice/1"; |
78
|
|
|
|
79
|
|
|
// 10/12/2017 ish |
80
|
|
|
$now = new \DateTimeImmutable(); |
81
|
|
|
$now = $now->setTimestamp(1509692666); |
82
|
|
|
|
83
|
|
|
$validationConfig = new PathValidationConfig($now, 10); |
|
|
|
|
84
|
|
|
$validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem()); |
85
|
|
|
|
86
|
|
|
$this->expectException(\RuntimeException::class); |
87
|
|
|
$this->expectExceptionMessage("Content-type was not application/bitcoin-paymentrequest"); |
88
|
|
|
|
89
|
|
|
$client->getRequest($requestUrl, $validator); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.