Make the tests work.
This commit is contained in:
72
test/test_moviehash.rb
Normal file
72
test/test_moviehash.rb
Normal file
@@ -0,0 +1,72 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "test_helper"
|
||||
require "webrick"
|
||||
require "debug"
|
||||
|
||||
class TestMoviehash < Minitest::Test
|
||||
def setup
|
||||
@file_path = File.expand_path("test/files", Dir.pwd) # Set absolute path
|
||||
@port = find_available_port
|
||||
|
||||
@webrick = WEBrick::HTTPServer.new(
|
||||
Port: @port,
|
||||
AccessLog: [],
|
||||
Logger: WEBrick::Log.new(File::NULL)
|
||||
)
|
||||
|
||||
@webrick.mount "/files", WEBrick::HTTPServlet::FileHandler, @file_path
|
||||
|
||||
@thread = Thread.new { @webrick.start }
|
||||
|
||||
# Give the server a moment to start
|
||||
sleep 0.1
|
||||
end
|
||||
|
||||
def teardown
|
||||
@webrick.shutdown
|
||||
@thread.join
|
||||
end
|
||||
|
||||
def test_cases
|
||||
{
|
||||
"breakdance.avi" => "8e245d9679d31e12",
|
||||
"dummy.bin" => "61f7751fc2a72bfb"
|
||||
}
|
||||
end
|
||||
|
||||
def test_that_it_has_a_version_number
|
||||
refute_nil ::Moviehash::VERSION
|
||||
end
|
||||
|
||||
def test_hash_calc
|
||||
# wget https://static.opensubtitles.org/addons/avi/dummy.rar ; unrar x dummy.rar
|
||||
# wget https://static.opensubtitles.org/addons/avi/breakdance.avi
|
||||
|
||||
test_cases.each do |file, hash|
|
||||
assert_equal hash, Moviehash.compute_hash("test/files/#{file}")
|
||||
end
|
||||
end
|
||||
|
||||
def test_hash_calc_url
|
||||
File.expand_path("files", __dir__)
|
||||
# test_cases.each do |file, hash|
|
||||
# FakeWeb.register_uri(:get, "http://example.com/#{file}", :body => File.join(file_path, file) )
|
||||
# FakeWeb.register_uri(:head, "http://example.com/#{file}", :content_length => File.size( File.join(file_path, file)) )
|
||||
# end
|
||||
|
||||
test_cases.each do |file, hash|
|
||||
uri = URI.parse("http://localhost:#{@port}/files/#{file}")
|
||||
assert_equal hash, Moviehash.compute_hash(uri.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_available_port
|
||||
server = TCPServer.new("127.0.0.1", 0)
|
||||
port = server.addr[1]
|
||||
server.close
|
||||
port
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user