/
form_spec.cr
127 lines (105 loc) · 3.64 KB
/
form_spec.cr
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
require "../../spec_helper"
module Awscr
module S3
module Presigned
describe Form do
describe ".build" do
it "builds a form" do
form = Form.build(
region: "us-east-1",
aws_access_key: "test",
aws_secret_key: "test") do |f|
f.condition("bucket", "2")
end
form.should be_a(Form)
form.fields.size.should eq 6 # bucket, and all the sig and policy stuff
end
end
describe "url" do
it "returns form url" do
form = Form.build(
region: "us-east-1",
aws_access_key: "test",
aws_secret_key: "test") do |f|
f.condition("bucket", "hi")
end
form.url.should eq("http://hi.s3.amazonaws.com")
end
end
describe "fields" do
it "is a field collection" do
post = Post.new(
region: "us-east-1",
aws_access_key: "test",
aws_secret_key: "test"
)
form = Form.new(post, HTTP::Client.new("host"))
form.fields.should be_a(FieldCollection)
end
end
describe "to_html" do
it "returns an html printer" do
post = Post.new(
region: "us-east-1",
aws_access_key: "test",
aws_secret_key: "test"
)
form = Form.new(post, HTTP::Client.new("host"))
form.to_html.should be_a(HtmlPrinter)
end
end
describe "submit" do
it "sends a reasonable request over http for v2" do
time = Time.unix(1)
Timecop.freeze(time)
post = Post.new(
region: "us-east-1",
aws_access_key: "test",
aws_secret_key: "test",
signer: :v2
)
post.build do |builder|
builder.expiration(time)
builder.condition("bucket", "test")
builder.condition("key", "hi")
end
WebMock.stub(:post, "http://fake host/").to_return do |request|
request.headers.not_nil!["Content-Type"].nil?.should eq false
request.body.nil?.should eq false
HTTP::Client::Response.new(200, body: "")
end
post.fields["Signature"].should eq("2OuTzB6hWfTJsU6UuN4mLuVEHpY=")
client = HTTP::Client.new("fake host")
io = IO::Memory.new("test")
form = Form.new(post, client)
form.submit(io)
end
it "sends a reasonable request over http for v4" do
time = Time.unix(1)
Timecop.freeze(time)
post = Post.new(
region: "us-east-1",
aws_access_key: "test",
aws_secret_key: "test"
)
post.build do |builder|
builder.expiration(time)
builder.condition("bucket", "test")
builder.condition("key", "hi")
end
WebMock.stub(:post, "http://fake host/").to_return do |request|
request.headers.not_nil!["Content-Type"].nil?.should eq false
request.body.nil?.should eq false
HTTP::Client::Response.new(200, body: "")
end
post.fields["X-Amz-Signature"].should eq("db2a7f49b8c2b1513db3f85416ea04d38cb4a55b39f6c4a56b0bd83b41594a3d")
client = HTTP::Client.new("fake host")
io = IO::Memory.new("test")
form = Form.new(post, client)
form.submit(io)
end
end
end
end
end
end