-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFromApiGatewayTest.class.php
More file actions
executable file
·181 lines (155 loc) · 5.65 KB
/
FromApiGatewayTest.class.php
File metadata and controls
executable file
·181 lines (155 loc) · 5.65 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php namespace com\amazon\aws\lambda\unittest;
use com\amazon\aws\lambda\FromApiGateway;
use io\streams\Streams;
use lang\IllegalArgumentException;
use test\{Assert, Expect, Test, Values};
use util\Bytes;
use web\io\Part;
class FromApiGatewayTest {
const COOKIE = 'session=7AABXMPL1AFD9BBF-0643XMPL09956DE2';
const BOUNDARY = '------------------------899f0c287170dd63f';
/** Returns a new fixture */
private function fixture($method= 'GET', $query= '', $headers= [], $body= null) {
return new FromApiGateway([
'version' => '2.0',
'routeKey' => 'ANY /test',
'rawPath' => '/default/test',
'rawQueryString' => $query,
'cookies' => [self::COOKIE],
'headers' => $headers,
'requestContext' => [
'accountId' => '123456789012',
'apiId' => 'r3pmxmplak',
'domainName' => 'r3pmxmplak.execute-api.us-east-2.amazonaws.com',
'domainPrefix' => 'r3pmxmplak',
'http' => [
'method' => $method,
'path' => '/default/test',
'protocol' => 'HTTP/1.1',
'sourceIp' => '192.168.178.1',
'userAgent' => 'XP/Test'
],
'requestId' => 'JKJaXmPLvHcESHA=',
'routeKey' => 'ANY /test',
'stage' => 'default',
'time' => '10/Mar/2020:05:16:23 +0000',
'timeEpoch' => 1583817383220
],
'isBase64Encoded' => $body instanceof Bytes,
'body' => $body
]);
}
#[Test]
public function can_create() {
$this->fixture();
}
#[Test, Expect(class: IllegalArgumentException::class, message: '/Cannot handle API gateway without version/')]
public function cannot_create_without_version() {
new FromApiGateway([]);
}
#[Test, Expect(class: IllegalArgumentException::class, message: '/Cannot handle API gateway version 1.0/')]
public function cannot_create_with_version1() {
new FromApiGateway(['version' => '1.0']);
}
#[Test]
public function version() {
Assert::equals('1.1', $this->fixture()->version());
}
#[Test]
public function scheme_defaults_to_http() {
Assert::equals('http', $this->fixture()->scheme());
}
#[Test]
public function https_scheme_determined_via_x_forwarded_proto() {
Assert::equals('https', $this->fixture('GET', '', ['x-forwarded-proto' => 'https'])->scheme());
}
#[Test, Values(['GET', 'GET', 'POST'])]
public function method($name) {
Assert::equals($name, $this->fixture($name)->method());
}
#[Test]
public function resource() {
Assert::equals('/default/test', $this->fixture('GET')->resource());
}
#[Test]
public function resource_including_query() {
Assert::equals('/default/test?name=Test', $this->fixture('GET', 'name=Test')->resource());
}
#[Test, Values([[[]], [['accept' => '*/*', 'user-agent' => 'test']]])]
public function headers_include_cookies_and_remote_addr($headers) {
Assert::equals(
['remote-addr' => '192.168.178.1', 'cookie' => self::COOKIE] + $headers,
iterator_to_array($this->fixture('GET', '', $headers)->headers())
);
}
#[Test]
public function read_without_body() {
Assert::equals('', $this->fixture('GET')->read());
}
#[Test]
public function read_body() {
Assert::equals('Test', $this->fixture('POST', '', ['content-length' => '4'], 'Test')->read());
}
#[Test]
public function read_base64_encoded_body() {
Assert::equals('Test', $this->fixture('POST', '', ['content-length' => '8'], new Bytes('VGVzdA=='))->read());
}
#[Test]
public function read_part_of_body() {
$fixture= $this->fixture('POST', '', ['content-length' => '4'], 'Test');
Assert::equals(['Te', 'st'], [$fixture->read(2), $fixture->read()]);
}
#[Test]
public function readLine_without_body() {
Assert::null($this->fixture('GET')->readLine());
}
#[Test, Values([['Test', ['Test']], ["One\nTwo", ['One', 'Two']], ["One\nTwo\n", ['One', 'Two']]])]
public function readLine($input, $outcome) {
$fixture= $this->fixture('POST', '', ['content-length' => strlen($input)], $input);
$lines= [];
while (null !== ($line= $fixture->readLine())) {
$lines[]= $line;
}
Assert::equals($outcome, $lines);
}
#[Test]
public function stream_without_body() {
Assert::null($this->fixture('GET')->incoming());
}
#[Test]
public function stream_body() {
Assert::equals('Test', Streams::readAll($this->fixture('POST', '', ['content-length' => '4'], 'Test')->incoming()));
}
#[Test]
public function stream_base64_encoded_body() {
Assert::equals('Test', Streams::readAll($this->fixture('POST', '', ['content-length' => '8'], new Bytes('VGVzdA=='))->incoming()));
}
#[Test]
public function file_upload_including_form_value() {
$payload= sprintf(implode("\r\n", [
'--%1$s',
'Content-Disposition: form-data; name="upload"; filename="test.txt"',
'Content-Type: text/plain',
'',
'Test',
'--%1$s',
'Content-Disposition: form-data; name="submit"',
'',
'Upload',
'--%1$s--',
''
]), self::BOUNDARY);
$fixture= $this->fixture('POST', '', ['content-length' => (string)strlen($payload)], $payload);
$parts= [];
foreach ($fixture->parts(self::BOUNDARY) as $name => $part) {
if (Part::FILE === $part->kind()) {
$parts[]= ['file', $name.':'.$part->name().':'.$part->type(), $part->bytes()];
} else if (Part::PARAM === $part->kind()) {
$parts[]= ['param', $name, $part->value()];
} else {
$parts[]= ['incomplete', $name, $part->error()];
}
}
Assert::equals([['file', 'upload:test.txt:text/plain', 'Test'], ['param', 'submit', 'Upload']], $parts);
}
}