Since last_response.body is a string, you can always run a MD5 checksum directly on a string.
(Digest::MD5.hexdigest(last_response.body)).should eq ("a1cba6369d668ed0ae5e97658c30979a")
Question
I have the following rspec code to test the upload and download of a binary file. I know I can check for 'content-type' to verify the file is properly uploaded and downloaded. But how do I run MD5 based on the following to make sure it's actually they are the same file.
it "should upload a file" do
file = Rack::Test::UploadedFile.new("./HelloWorld.bin", "application/octet-stream")
json = {:md5sum => "0cd74c7a3cf2207ffd68d43177681e6b", :config => "./testconfig.yaml"}.to_json
post "/upload", :json => json, :file => file
last_response.should be_ok
(JSON.parse(last_response.body)["status"]).should be_true
(JSON.parse(last_response.body)["filename"]).should eq("HelloWorld.bin")
end
it "download a file successfully" do
filename = "HelloWorld.bin"
get '/download/' + filename
last_response.should be_ok
last_response.headers['Content-Type'].should eq "application/octet-stream"
end
Can the 'last_response.body' be assigned as a binary file in the get '/download' ?
La solution
Since last_response.body is a string, you can always run a MD5 checksum directly on a string.
(Digest::MD5.hexdigest(last_response.body)).should eq ("a1cba6369d668ed0ae5e97658c30979a")