Project

General

Profile

Bug #12642 ยป ipv6_test.rb

test script - andy (Andy Brody), 03/14/2017 11:04 PM

 
require 'net/http'
require 'webrick'

# An echo server that responds with the Host header it receives
class HostEchoServer < WEBrick::HTTPServlet::AbstractServlet
def do_GET request, response
response.status = 200
response['Content-Type'] = 'text/plain'
response.body = request.header['host'].first
end
end

def start_echo_server(bind_address, port)
server = WEBrick::HTTPServer.new(BindAddress: bind_address, Port: port)
server.mount('/', HostEchoServer)
server
end

# start up the echo server on ::1 port 8080 or next available
port = 8080
begin
server = start_echo_server('::1', port)
rescue Errno::EADDRINUSE
port += 1
retry
end

server_thread = Thread.new { server.start }

uri = URI.parse("http://[::1]:#{port}/")
req = Net::HTTP::Get.new(uri)

if ARGV.include?('--set-host')
req['Host'] = "[::1]:#{port}"
end

net = Net::HTTP.new(uri.hostname, uri.port)
net.set_debug_output STDERR

response = nil
net.start do |http|
response = http.request(req, nil)
end

server.shutdown
server_thread.join

body = response.body

puts RUBY_DESCRIPTION

if body == "[::1]:#{port}"
puts "OK: Host is " + body.inspect
else
puts "FAIL: Host is " + body.inspect
exit 5
end
    (1-1/1)