Completed
Pull Request — master (#10)
by thomas
06:48
created

ClientHeadersTest::testMissingContentType()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 33
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 18
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);
0 ignored issues
show
Security Bug introduced by
It seems like $now defined by $now->setTimestamp(1509692666) on line 43 can also be of type false; however, X509\CertificationPath\P...onConfig::__construct() does only seem to accept object<DateTimeImmutable>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

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 returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
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);
0 ignored issues
show
Security Bug introduced by
It seems like $now defined by $now->setTimestamp(1509692666) on line 81 can also be of type false; however, X509\CertificationPath\P...onConfig::__construct() does only seem to accept object<DateTimeImmutable>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

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 returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
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