forked from aws-beam/aws-elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths3_test.exs
More file actions
329 lines (275 loc) · 8.81 KB
/
s3_test.exs
File metadata and controls
329 lines (275 loc) · 8.81 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
defmodule AWS.Generated.S3Test do
use ExUnit.Case, async: true
setup do
client = %AWS.Client{
access_key_id: "my-access-key-id",
secret_access_key: "my-secret-access-key",
endpoint: "localhost",
region: "local",
proto: "http"
}
service_metadata = %{
abbreviation: "Foo",
api_version: "2017-08-22",
content_type: "application/x-amz-json-1.1",
credential_scope: nil,
endpoint_prefix: "foo",
global?: false,
hostname: nil,
protocol: "json",
service_id: "Foo",
signature_version: "v4",
signing_name: "foo",
target_prefix: "com.amazonaws.foo"
}
%{client: client, metadata: service_metadata}
end
describe "Generated code parameter tests" do
setup do
bypass = Bypass.open()
{:ok, bypass: bypass}
end
test "Raise on invalid optional parameters", %{client: client} do
assert_raise ArgumentError, ~r/^unknown keys \[:fake_param\].+/, fn ->
AWS.S3.get_object(client, "mybucket", "key1", fake_param: true)
end
end
test "optional headers, and optional query parameters are correctly used with get_object/4",
%{
client: client,
bypass: bypass
} do
bucket = "mybucket"
key = "mykey"
md5 = "foobar"
version_id = "v42"
client = %{client | port: bypass.port}
Bypass.expect(bypass, fn conn ->
# No required headers for this method
# Optional header
[header_md5] =
Plug.Conn.get_req_header(conn, "x-amz-server-side-encryption-customer-key-md5")
assert header_md5 == md5
# No required query params for this method
# Optional query params
assert String.contains?(conn.query_string, "versionId=#{version_id}")
Plug.Conn.resp(
conn,
200,
"{ContentLength: 1234}"
)
end)
{:ok, _body, _} =
AWS.S3.get_object(client, bucket, key, sse_customer_key_md5: md5, version_id: version_id)
Bypass.down(bypass)
end
test "required query parameters are correctly used with get_bucket_analytics_configuration/3",
%{
client: client,
bypass: bypass
} do
bucket = "mybucket"
analytics_config_id = "foobar"
client = %{client | port: bypass.port}
Bypass.expect(bypass, fn conn ->
# Required query param
assert String.contains?(conn.query_string, "id=#{analytics_config_id}")
Plug.Conn.resp(
conn,
200,
# Dummy response
~s|<AnalyticsConfiguration>
<Id>string</Id>
<Filter>
<And>
<Prefix>string</Prefix>
<Tag>
<Key>string</Key>
<Value>string</Value>
</Tag>
...
</And>
<Prefix>string</Prefix>
<Tag>
<Key>string</Key>
<Value>string</Value>
</Tag>
</Filter>
<StorageClassAnalysis>
<DataExport>
<Destination>
<S3BucketDestination>
<Bucket>string</Bucket>
<BucketAccountId>string</BucketAccountId>
<Format>string</Format>
<Prefix>string</Prefix>
</S3BucketDestination>
</Destination>
<OutputSchemaVersion>string</OutputSchemaVersion>
</DataExport>
</StorageClassAnalysis>
</AnalyticsConfiguration>|
)
end)
{:ok, _body, _} =
AWS.S3.get_bucket_analytics_configuration(client, bucket, analytics_config_id)
Bypass.down(bypass)
end
test "required headers are correctly used with get_object_attributes/4", %{
client: client,
bypass: bypass
} do
bucket = "mybucket"
key = "mykey"
obj_attrs = "foobar"
client = %{client | port: bypass.port}
Bypass.expect(bypass, fn conn ->
# Required header
[header_obj_attrs] =
Plug.Conn.get_req_header(conn, "x-amz-object-attributes")
assert header_obj_attrs == obj_attrs
Plug.Conn.resp(
conn,
200,
# Dummy response
~s|<GetObjectAttributesOutput>
<ETag>string</ETag>
<Checksum>
<ChecksumCRC32>string</ChecksumCRC32>
<ChecksumCRC32C>string</ChecksumCRC32C>
<ChecksumSHA1>string</ChecksumSHA1>
<ChecksumSHA256>string</ChecksumSHA256>
</Checksum>
<ObjectParts>
<IsTruncated>boolean</IsTruncated>
<MaxParts>integer</MaxParts>
<NextPartNumberMarker>integer</NextPartNumberMarker>
<PartNumberMarker>integer</PartNumberMarker>
<Part>
<ChecksumCRC32>string</ChecksumCRC32>
<ChecksumCRC32C>string</ChecksumCRC32C>
<ChecksumSHA1>string</ChecksumSHA1>
<ChecksumSHA256>string</ChecksumSHA256>
<PartNumber>integer</PartNumber>
<Size>long</Size>
</Part>
<PartsCount>integer</PartsCount>
</ObjectParts>
<StorageClass>string</StorageClass>
<ObjectSize>long</ObjectSize>
</GetObjectAttributesOutput>|
)
end)
# This is the only function with required headers in S3.
{:ok, _body, _} =
AWS.S3.get_object_attributes(client, bucket, key, obj_attrs)
Bypass.down(bypass)
end
# write_get_object_response missing required params in def
test "optional headers with HTTP HEAD request with head_bucket/3", %{
client: client,
bypass: bypass
} do
bucket = "mybucket"
owner = "foobar"
client = %{client | port: bypass.port}
Bypass.expect(bypass, fn conn ->
# No required headers for this method
# Optional header
[header_owner] =
Plug.Conn.get_req_header(conn, "x-amz-expected-bucket-owner")
assert header_owner == owner
Plug.Conn.resp(
conn,
200,
""
)
end)
{:ok, _body, _} =
AWS.S3.head_bucket(client, bucket, expected_bucket_owner: owner)
Bypass.down(bypass)
end
test "optional body request not sent with create_bucket/3", %{
client: client,
bypass: bypass
} do
bucket = "mybucket"
client = %{client | port: bypass.port}
# No body sent
input = nil
Bypass.expect_once(bypass, fn conn ->
# No required headers for this method
# Optional header
assert {:ok, "", conn} ==
Plug.Conn.read_body(conn)
conn
|> Plug.Conn.put_resp_header("location", "foobar")
|> Plug.Conn.resp(
200,
""
)
end)
{:ok, _body, _} =
AWS.S3.create_bucket(client, bucket, input)
Bypass.down(bypass)
end
test "optional body request sent with create_bucket/3", %{
client: client,
bypass: bypass
} do
bucket = "mybucket"
client = %{client | port: bypass.port}
# Body sent
input = %{
"CreateBucketConfiguration" => %{
"LocationConstraint" => "Europe"
}
}
Bypass.expect_once(bypass, fn conn ->
# No required headers for this method
# Optional header
{:ok, body, conn} =
Plug.Conn.read_body(conn)
assert body != ""
conn
|> Plug.Conn.put_resp_header("location", "foobar")
|> Plug.Conn.resp(
200,
""
)
end)
{:ok, _body, _} =
AWS.S3.create_bucket(client, bucket, input)
Bypass.down(bypass)
end
test "required body request with upload_part/6", %{
client: client,
bypass: bypass
} do
bucket = "mybucket"
key = "fookey"
part_number = 123
upload_id = "up_id_42"
input = "part-content"
client = %{client | port: bypass.port}
Bypass.expect(bypass, fn conn ->
# No required headers for this method
# Optional header
{:ok, read_input, conn} =
Plug.Conn.read_body(conn)
assert read_input == input
conn
|> Plug.Conn.resp(
200,
""
)
end)
{:ok, _body, _} =
AWS.S3.upload_part(client, bucket, key, part_number, upload_id, input)
assert_raise FunctionClauseError, fn ->
AWS.S3.upload_part(client, bucket, key, part_number, upload_id, nil)
end
Bypass.down(bypass)
end
end
end