1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Bip70\Test\Client\GuzzleClient; |
6
|
|
|
|
7
|
|
|
use Bip70\Client\GuzzleHttpClient; |
8
|
|
|
use Bip70\Client\MIMEType; |
9
|
|
|
use Bip70\Client\PaymentRequestInfo; |
10
|
|
|
use Bip70\Protobuf\Proto\PaymentDetails; |
11
|
|
|
use Bip70\X509\PKIType; |
12
|
|
|
use Bip70\X509\RequestSigner; |
13
|
|
|
use Bip70\X509\RequestValidation; |
14
|
|
|
use Bip70\X509\TrustStoreLoader; |
15
|
|
|
use GuzzleHttp\Handler\MockHandler; |
16
|
|
|
use GuzzleHttp\HandlerStack; |
17
|
|
|
use GuzzleHttp\Middleware; |
18
|
|
|
use GuzzleHttp\Psr7\Response; |
19
|
|
|
use PHPUnit\Framework\TestCase; |
20
|
|
|
use Psr\Http\Message\RequestInterface; |
21
|
|
|
use Psr\Http\Message\ResponseInterface; |
22
|
|
|
use Sop\CryptoEncoding\PEM; |
23
|
|
|
use Sop\CryptoEncoding\PEMBundle; |
24
|
|
|
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo; |
25
|
|
|
use X509\Certificate\CertificateBundle; |
26
|
|
|
use X509\CertificationPath\PathValidation\PathValidationConfig; |
27
|
|
|
use X509\CertificationPath\PathValidation\PathValidationResult; |
28
|
|
|
|
29
|
|
|
class GetsSignedRequestTest extends TestCase |
30
|
|
|
{ |
31
|
|
|
public function testGetsSignedRequest() |
32
|
|
|
{ |
33
|
|
|
$keySigned = PrivateKeyInfo::fromPEM(PEM::fromFile(__DIR__ . "/../../../data/testnet-only-cert-not-valid.key")); |
34
|
|
|
$certs = CertificateBundle::fromPEMBundle(PEMBundle::fromFile(__DIR__ . "/../../../data/testnet-only-cert-not-valid.cabundle.pem")); |
35
|
|
|
$certSigned = $certs->all()[0]; |
36
|
|
|
$certBundle = new CertificateBundle(...array_slice($certs->all(), 1)); |
37
|
|
|
|
38
|
|
|
$now = 1509692666; |
39
|
|
|
$details = new PaymentDetails(); |
40
|
|
|
$details->setTime($now); |
41
|
|
|
|
42
|
|
|
$requestSigner = new RequestSigner(); |
43
|
|
|
$request = $requestSigner->sign($details, PKIType::X509_SHA256, $keySigned, $certSigned, $certBundle); |
44
|
|
|
$serializedRequest = $request->serialize(); |
45
|
|
|
|
46
|
|
|
$container = []; |
47
|
|
|
$history = Middleware::history($container); |
48
|
|
|
|
49
|
|
|
$handler = new MockHandler([ |
50
|
|
|
// response to our GET PAYMENTREQUEST request |
51
|
|
|
new Response(200, [ |
52
|
|
|
'Content-Type' => [ |
53
|
|
|
'application/bitcoin-paymentrequest', |
54
|
|
|
] |
55
|
|
|
], $serializedRequest) |
56
|
|
|
]); |
57
|
|
|
|
58
|
|
|
$stack = HandlerStack::create($handler); |
59
|
|
|
$stack->push($history); |
60
|
|
|
|
61
|
|
|
$mockClient = new \GuzzleHttp\Client([ |
62
|
|
|
'handler' => $stack, |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$client = new GuzzleHttpClient($mockClient); |
66
|
|
|
|
67
|
|
|
$requestUrl = "https://development.bip70.local/invoice/1"; |
68
|
|
|
|
69
|
|
|
// 10/12/2017 ish |
70
|
|
|
$dtnow = new \DateTimeImmutable(); |
71
|
|
|
$dtnow = $dtnow->setTimestamp($now); |
72
|
|
|
|
73
|
|
|
$validationConfig = new PathValidationConfig($dtnow, 10); |
74
|
|
|
$validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem()); |
75
|
|
|
$data = $client->getRequest($requestUrl, $validator); |
76
|
|
|
|
77
|
|
|
$this->assertInstanceOf(PaymentRequestInfo::class, $data); |
78
|
|
|
$this->assertCount(1, $container); |
79
|
|
|
$this->assertInstanceOf(PathValidationResult::class, $data->pathValidation()); |
80
|
|
|
|
81
|
|
|
/** @var RequestInterface $req1 */ |
82
|
|
|
$req1 = $container[0]['request']; |
83
|
|
|
$this->assertEquals($requestUrl, $req1->getUri()); |
84
|
|
|
$this->assertTrue($req1->hasHeader('Accept')); |
85
|
|
|
$this->assertEquals(MIMEType::PAYMENT_REQUEST, $req1->getHeader('Accept')[0]); |
86
|
|
|
|
87
|
|
|
/** @var ResponseInterface $res1 */ |
88
|
|
|
$res1 = $container[0]['response']; |
89
|
|
|
$res1->getBody()->rewind(); |
90
|
|
|
$this->assertEquals($serializedRequest, $res1->getBody()->getContents()); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|