-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
59 lines (47 loc) · 1.3 KB
/
test.rb
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
ENV["APP_ENV"] = "test"
require "./app"
require "minitest/autorun"
require "webmock/minitest"
require "rack/test"
class HelloWorldTest < Minitest::Test
include Rack::Test::Methods
def app
Sinatra::Application
end
def test_health_check
get "/health_check"
assert last_response.ok?
assert_equal "OK", last_response.body
end
def test_not_found
get "/wrong/signature"
assert last_response.not_found?
end
def test_proxy
url = "http://example.com/image.jpg"
content_type = "image/jpeg"
signature = OpenSSL::HMAC.hexdigest("sha1", "secret", url)
stub_request(:get, url).with(headers: {
accept: "image/png,image/svg+xml,image/*"
}).to_return({
status: 200,
body: "OK",
headers: {content_type: content_type}
})
get "/#{signature}/#{hex_encode(url)}"
assert last_response.ok?
assert_equal content_type, last_response.get_header("Content-Type")
end
def test_wrong_content_type
url = "http://example.com/image.jpg"
signature = OpenSSL::HMAC.hexdigest("sha1", "secret", url)
stub_request(:get, url).to_return({
status: 500
})
get "/#{signature}/#{hex_encode(url)}"
assert last_response.not_found?
end
def hex_encode(string)
string.to_enum(:each_byte).map { |byte| "%02x" % byte }.join
end
end