test/test_http.rb in zold-0.20.4 vs test/test_http.rb in zold-0.21.0
- old
+ new
@@ -96,11 +96,11 @@
RandomPort::Pool::SINGLETON.acquire do |port|
thread = Thread.start do
Zold::VerboseThread.new(test_log).run do
server = TCPServer.new(port)
client = server.accept
- client.puts("HTTP/1.1 200 OK\nContent-Length: 4\n\n")
+ client.puts("HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\n")
sleep 1
client.puts('Good')
client.close
end
end
@@ -137,11 +137,14 @@
socket.print("\r\n")
socket.print('Done')
socket.close
end
end
- res = Zold::Http.new(uri: "http://127.0.0.1:#{port}/").put('how are you?')
+ res = Tempfile.open do |f|
+ IO.write(f, 'How are you?')
+ Zold::Http.new(uri: "http://127.0.0.1:#{port}/").put(f)
+ end
assert_equal(200, res.status, res)
assert(body.include?('Content-Length: 12'), body)
assert(body.include?('Content-Type: text/plain'))
headers = body.split("\n").select { |t| t =~ /^[a-zA-Z-]+:.+$/ }
assert_equal(headers.count, headers.uniq.count)
@@ -154,7 +157,66 @@
stub_request(:get, 'http://some-host-3/')
.with(headers: { 'X-Zold-Version' => Zold::VERSION })
.to_return(status: 200)
res = Zold::Http.new(uri: 'http://some-host-3/').get
assert_equal(200, res.status, res)
+ end
+
+ def test_uploads_file
+ WebMock.allow_net_connect!
+ RandomPort::Pool::SINGLETON.acquire do |port|
+ thread = Thread.start do
+ Zold::VerboseThread.new(test_log).run do
+ server = TCPServer.new(port)
+ socket = server.accept
+ body = ''
+ stops = 0
+ loop do
+ part = socket.read_nonblock(5, exception: false)
+ if part == :wait_readable
+ break if stops > 5
+ stops += 1
+ sleep 0.001
+ else
+ body += part
+ stops = 0
+ end
+ end
+ socket.close_read
+ socket.print("HTTP/1.1 200 OK\nContent-Length: #{body.length}\n\n#{body}")
+ socket.close_write
+ end
+ end
+ content = "how are you\nmy friend"
+ res = Tempfile.open do |f|
+ IO.write(f, content)
+ Zold::Http.new(uri: "http://localhost:#{port}/").put(f)
+ end
+ assert_equal(200, res.status, res)
+ assert(res.body.include?(content), res)
+ thread.kill
+ thread.join
+ end
+ end
+
+ def test_downloads_file
+ WebMock.allow_net_connect!
+ RandomPort::Pool::SINGLETON.acquire do |port|
+ content = "how are you\nmy friend" * 1000
+ thread = Thread.start do
+ Zold::VerboseThread.new(test_log).run do
+ server = TCPServer.new(port)
+ socket = server.accept
+ socket.print("HTTP/1.1 200 OK\nContent-Length: #{content.length}\n\n#{content}")
+ socket.close_write
+ end
+ end
+ body = Tempfile.open do |f|
+ Zold::Http.new(uri: "http://localhost:#{port}/").get_file(f)
+ IO.read(f)
+ end
+ assert(body.include?(content), body)
+ thread.kill
+ thread.join
+ end
end
end