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 = "      HTTP/1.1 101 Switching Protocols\n      Date: Mon, 14 Jan 2013 11:54:24 GMT\n      Upgrade: PTTH/1.0\n      Content-Length: 0\n      Connection: Upgrade\n    EOS\n\n    post_response  = \"\#{@response.method} \#{@response.path} HTTP/1.1\\n\"\n    post_response += \"Content-Length: \#{@response.body.length}\\n\" if @response.body\n    post_response += \"Accept: */*\\n\"\n    post_response += \"\\n\"\n    post_response += @response.body if @response.body\n\n    client.puts switch_protocols\n    sleep 0.5\n    client.puts post_response\n    client.read unless client.eof?\n  end\nend\n".gsub(/^\s+/, '')