Class: Net::PTTH::TestServer

Inherits:
Object
  • Object
show all
Defined in:
lib/net/ptth/test.rb

Instance Method Summary collapse

Constructor Details

#initialize(configuration = {}) ⇒ TestServer

Public: Initialize the PTTH test server

port: the port in which the server will listen


10
11
12
13
14
15
16
17
# File 'lib/net/ptth/test.rb', line 10

def initialize(configuration = {})
  port = configuration.fetch(:port, 23045)
  response = Net::HTTP::Post.new("/reverse")
  response.body = "reversed"

  @response = configuration.fetch(:response, response)
  @server = TCPServer.new(port)
end

Instance Method Details

#closeObject

Public: Stops the current server



48
49
50
# File 'lib/net/ptth/test.rb', line 48

def close
  @server.close
end

#startObject

Public: Starts the test server



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/net/ptth/test.rb', line 21

def start
  loop do
    client = @server.accept

    switch_protocols = <<-EOS.gsub(/^\s+/, '')
      HTTP/1.1 101 Switching Protocols
      Date: Mon, 14 Jan 2013 11:54:24 GMT
      Upgrade: PTTH/1.0
      Content-Length: 0
      Connection: Upgrade
    EOS

    post_response  = "#{@response.method} #{@response.path} HTTP/1.1\n"
    post_response += "Content-Length: #{@response.body.length}\n" if @response.body
    post_response += "Accept: */*\n"
    post_response += "\n"
    post_response += @response.body if @response.body

    client.puts switch_protocols
    sleep 0.5
    client.puts post_response
    client.read unless client.eof?
  end
end