summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-crt-php/tests/HttpMessageTest.php
blob: eec18b62208cbdc8bdd0215d322b13140e83cea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
use AWS\CRT\HTTP\Headers;
use AWS\CRT\HTTP\Request;
use AWS\CRT\HTTP\Response;

require_once('common.inc');

final class HttpMessageTest extends CrtTestCase {
    public function testHeaders() {
        $headers = new Headers();
        $this->assertSame(0, $headers->count());
    }

    public function testHeadersMarshalling() {
        $headers_array = [
            "host" => "s3.amazonaws.com",
            "test" => "this is a test header value"
        ];
        $headers = new Headers($headers_array);
        $this->assertSame(2, $headers->count());
        $this->assertSame($headers_array['host'], $headers->get('host'));
        $this->assertSame($headers_array['test'], $headers->get('test'));
        $buffer = Headers::marshall($headers);
        $headers_copy = Headers::unmarshall($buffer);
        $this->assertSame(2, $headers_copy->count());
        $this->assertSame($headers_array['host'], $headers_copy->get('host'));
        $this->assertSame($headers_array['test'], $headers_copy->get('test'));
    }

    private function assertMessagesMatch($a, $b) {
        $this->assertSame($a->method(), $b->method());
        $this->assertSame($a->path(), $b->path());
        $this->assertSame($a->query(), $b->query());
        $this->assertSame($a->headers()->toArray(), $b->headers()->toArray());
    }

    public function testRequestMarshalling() {
        $headers = [
            "host" => "s3.amazonaws.com",
            "test" => "this is a test header value"
        ];
        $method = "GET";
        $path = "/index.php";
        $query = [];

        $msg = new Request($method, $path, $query, $headers);
        $msg_buf = Request::marshall($msg);
        $msg_copy = Request::unmarshall($msg_buf);

        $this->assertMessagesMatch($msg, $msg_copy);
    }

    public function testRequestMarshallingWithQueryParams() {
        $headers = [
            "host" => "s3.amazonaws.com",
            "test" => "this is a test header value"
        ];
        $method = "GET";
        $path = "/index.php";
        $query = [
            'request' => '1',
            'test' => 'true',
            'answer' => '42',
            'foo' => 'bar',
        ];

        $msg = new Request($method, $path, $query, $headers);
        $msg_buf = Request::marshall($msg);
        $msg_copy = Request::unmarshall($msg_buf);

        $this->assertMessagesMatch($msg, $msg_copy);
    }

    public function testResponseMarshalling() {
        $headers = [
            "content-length" => "42",
            "test" => "this is a test header value"
        ];
        $method = "GET";
        $path = "/index.php";
        $query = [
            'response' => '1'
        ];

        $msg = new Response($method, $path, $query, $headers, 200);
        $msg_buf = Request::marshall($msg);
        $msg_copy = Request::unmarshall($msg_buf);

        $this->assertMessagesMatch($msg, $msg_copy);
    }
}