Files
blob-store-app/test/test.rb
Dylan Knutson c772661cf2 reorganize test
2024-04-23 14:39:35 -07:00

63 lines
1.6 KiB
Ruby

require "rest_client"
require "json"
def run
cat1_file = -> { File.new("fixtures/cat1.jpg", mode: "rb") }
cat1_sha256 = Digest::SHA256.file("fixtures/cat1.jpg").hexdigest
puts "store, with sha256"
dump_resp(RestClient.post("http://localhost:7692/store", {
content_type: "image/jpeg",
data: cat1_file.call,
}))
puts "store, without sha256:"
dump_resp(RestClient.post("http://localhost:7692/store", {
content_type: "image/jpeg",
sha256: cat1_sha256,
data: cat1_file.call,
}))
puts "store, incorrect sha256:"
begin
RestClient.post("http://localhost:7692/store", {
content_type: "image/jpeg",
sha256: "123",
data: cat1_file.call,
})
puts "should have thrown!"
rescue => e
dump_resp(e.response)
end
puts "get, with sha256:"
dump_resp(RestClient.get("http://localhost:7692/get/#{cat1_sha256}"))
puts "get, 404 sha256:"
begin
RestClient.get("http://localhost:7692/get/e3705544cbf2fa93e16107d1821b312a7b825fc177fa28180a9c9a9d3ae8af3c")
raise "should have thrown!"
rescue => e
dump_resp(e.response)
raise "not 404" if e.response.code != 404
end
end
def dump_resp(resp)
puts " -> code: #{resp.code}"
headers = resp.headers
content_type = headers[:content_type]
puts " -> headers: #{headers}"
puts " -> content_type: #{content_type}"
puts " -> size: #{resp.size} bytes"
if content_type == "application/json"
puts " -> body: #{JSON.parse(resp.body)}"
else
body_sha256 = Digest::SHA256.hexdigest(resp.body)
puts " -> body sha256: #{body_sha256}"
end
puts "-" * 80
end
run