Created
March 25, 2014 16:07
-
-
Save kenichi/9765129 to your computer and use it in GitHub Desktop.
minitest "server gets a post" example with timeout and server thread
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require 'httpclient' | |
require 'socket' | |
require 'timeout' | |
describe 'server gets post' do | |
it 'gets a post' do | |
timeout = Timeout.timeout 5 do | |
server_thread = Thread.new do | |
server = TCPServer.new 8080 | |
client = server.accept | |
it_worked = false | |
while line = client.gets do | |
it_worked = true if line =~ /POST \/test/ | |
break if line.chomp.empty? | |
end | |
client.puts '' | |
[client,server].each &:close | |
raise unless it_worked | |
end | |
HTTPClient.new.post 'http://127.0.0.1:8080/test' | |
server_thread.join | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment