Completed
Branch master (48aedc)
by thomas
03:23 queued 02:08
created

ClientHeadersTest::testInvalidContentType()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 37
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 21
nc 1
nop 0
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