ResponseFactoryTest::headerProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of the beebot package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author    Stephane HULARD <[email protected]>
9
 * @package   Bee4\Test\Transport\Message
10
 */
11
12
namespace Bee4\Test\Transport\Message;
13
14
use Bee4\PHPUnit\HttpClientTestCase;
15
use Bee4\Transport\Client;
16
use Bee4\Transport\MagicHandler;
17
use Bee4\Transport\Message\Response;
18
use Bee4\Transport\Message\ResponseFactory;
19
20
/**
21
 * ResponseFactory unit test definition
22
 * @package Bee4\Test\Transport\Message
23
 */
24
class ResponseFactoryTest extends HttpClientTestCase
25
{
26
    public function testBuild()
27
    {
28
        $client = new MagicHandler(new Client(self::getBaseUrl()));
29
        $request = $client->get();
30
        $request->addHeader('Content-Type', 'text/html');
31
        $request->setUserAgent('Bee4 - BeeBot/v1.0');
32
        $response = $request->send();
33
34
        $headers = $response->getHeaders();
35
        $this->assertArrayHasKey('Content-Type', $headers);
36
        $this->assertArrayHasKey('Content-Length', $headers);
37
38
        $requestHeaders = $request->getHeaders();
39
        $this->assertArrayHasKey('Content-Type', $requestHeaders);
40
        $this->assertArrayHasKey('User-Agent', $requestHeaders);
41
    }
42
43
    /**
44
     * @dataProvider headerProvider
45
     * @param type $headers
46
     * @param type $length
47
     * @param array $valid
48
     */
49
    public function testParseHeaders($headers, $length, array $valid = [])
50
    {
51
        $response = new Response();
52
        $rest = ResponseFactory::parseHeaders($headers, $response);
53
54
        $this->assertEquals($length, strlen($rest));
55
        foreach ($valid as $name => $value) {
56
            $this->assertEquals($value, $response->getHeader($name));
57
        }
58
    }
59
60
    public function headerProvider()
61
    {
62
        return [
63
            [
64
                "HTTP/1.1 100 Continue\r\n".
65
                "\r\n".
66
                "HTTP/1.1 200 OK\r\n".
67
                "Content-Type: text/html; charset=UTF-8\r\n".
68
                "Content-Length: 17\r\n".
69
                "\r\n".
70
                "Just some content",
71
                17,
72
                ["Content-Length" => 17, "Content-Type" => 'text/html; charset=UTF-8']
73
            ],
74
            [
75
                "HTTP/1.1 200 OK\r\n".
76
                "Content-Type: application/json\r\n".
77
                "Content-Length: 0\r\n".
78
                "",
79
                0,
80
                ["Content-Length" => 0, "Content-Type" => 'application/json']
81
            ],
82
            [
83
                "HTTP/1.1 302 Found\r\n".
84
                "Content-Length: 0\r\n".
85
                "Location: http://www.google.fr\r\n",
86
                0,
87
                ["Content-Length" => 0, "Location" => 'http://www.google.fr']
88
            ]
89
        ];
90
    }
91
}
92