ClientHeadersTest   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 4
dl 0
loc 78
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testMissingContentType() 0 35 1
A testInvalidContentType() 0 39 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bip70\Test\Client\GuzzleClient;
6
7
use Bip70\Client\Exception\ProtocolException;
8
use Bip70\Client\MIMEType;
9
use Bip70\Client\NetworkConfig\BitcoinNetworkConfig;
10
use PHPUnit\Framework\TestCase;
11
12
use Bip70\Client\GuzzleHttpClient;
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 X509\CertificationPath\PathValidation\PathValidationConfig;
20
21
class ClientHeadersTest extends TestCase
22
{
23
    public function testMissingContentType()
24
    {
25
        $container = [];
26
        $history = Middleware::history($container);
27
28
        $handler = new MockHandler([
29
            // response to our GET PAYMENTREQUEST request
30
            new Response(200, [], '{"result":true}')
31
        ]);
32
33
        $stack = HandlerStack::create($handler);
34
        $stack->push($history);
35
36
        $mockClient = new \GuzzleHttp\Client([
37
            'handler' => $stack,
38
        ]);
39
40
        $client = new GuzzleHttpClient($mockClient);
41
42
        $requestUrl = "https://development.bip70.local/invoice/1";
43
44
        // 10/12/2017 ish
45
        $now = new \DateTimeImmutable();
46
        $now = $now->setTimestamp(1509692666);
47
48
        $networkConfig = new BitcoinNetworkConfig();
49
50
        $validationConfig = new PathValidationConfig($now, 10);
51
        $validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem());
52
53
        $this->expectException(ProtocolException::class);
54
        $this->expectExceptionMessage("Missing Content-Type header");
55
56
        $client->getRequest($requestUrl, $validator, $networkConfig);
57
    }
58
59
    public function testInvalidContentType()
60
    {
61
        $container = [];
62
        $history = Middleware::history($container);
63
64
        $handler = new MockHandler([
65
            // response to our GET PAYMENTREQUEST request
66
            new Response(200, [
67
                'Content-Type' => [
68
                    'application/json',
69
                ]
70
            ], '{"result":true}')
71
        ]);
72
73
        $stack = HandlerStack::create($handler);
74
        $stack->push($history);
75
76
        $mockClient = new \GuzzleHttp\Client([
77
            'handler' => $stack,
78
        ]);
79
80
        $client = new GuzzleHttpClient($mockClient);
81
82
        $requestUrl = "https://development.bip70.local/invoice/1";
83
84
        // 10/12/2017 ish
85
        $now = new \DateTimeImmutable();
86
        $now = $now->setTimestamp(1509692666);
87
88
        $networkConfig = new BitcoinNetworkConfig();
89
90
        $validationConfig = new PathValidationConfig($now, 10);
91
        $validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem());
92
93
        $this->expectException(ProtocolException::class);
94
        $this->expectExceptionMessage("Content-Type was not " . $networkConfig->getPaymentRequestMimeType());
95
96
        $client->getRequest($requestUrl, $validator, $networkConfig);
97
    }
98
}
99