Project

General

Profile

Bug #12642 ยป ipv6_test.rb

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

 
1
require 'net/http'
2
require 'webrick'
3

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

    
13
def start_echo_server(bind_address, port)
14
  server = WEBrick::HTTPServer.new(BindAddress: bind_address, Port: port)
15
  server.mount('/', HostEchoServer)
16
  server
17
end
18

    
19
# start up the echo server on ::1 port 8080 or next available
20
port = 8080
21
begin
22
  server = start_echo_server('::1', port)
23
rescue Errno::EADDRINUSE
24
  port += 1
25
  retry
26
end
27

    
28
server_thread = Thread.new { server.start }
29

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

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

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

    
40
response = nil
41
net.start do |http|
42
  response = http.request(req, nil)
43
end
44

    
45
server.shutdown
46
server_thread.join
47

    
48
body = response.body
49

    
50
puts RUBY_DESCRIPTION
51

    
52
if body == "[::1]:#{port}"
53
  puts "OK: Host is " + body.inspect
54
else
55
  puts "FAIL: Host is " + body.inspect
56
  exit 5
57
end