Module: HttpSimulator

Defined in:
lib/http_sim.rb

Constant Summary collapse

@@pid =
false
@@erb_files =
{
  index: self.read_file('lib/index.html.erb'),
  request: self.read_file('lib/request.html.erb'),
  response: self.read_file('lib/response.html.erb')
}
@@endpoints =
[]

Class Method Summary collapse

Class Method Details

.check_if_port_in_use(port) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/http_sim.rb', line 113

def self.check_if_port_in_use(port)
  begin
    HTTParty.get("http://localhost:#{port}/")
    raise "Port #{port} already in use"
  rescue Errno::ECONNREFUSED
    # ignored
  end
end

.read_file(path) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/http_sim.rb', line 29

def self.read_file(path)
  lines = []
  File.open(path, 'r') do |f|
    f.each_line do |line|
      lines.push line
    end
  end
  lines.join
end

.register_endpoint(method, path, default_response) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/http_sim.rb', line 127

def self.register_endpoint(method, path, default_response)
  raise '/ is a reserved path' if path == '/'

  endpoint = Endpoint.new(method, path, default_response)
  @@endpoints.push endpoint

  case endpoint.method
    when 'GET'
      Sinatra::Base.get endpoint.path do
        endpoint.add_request request.body.read
        endpoint.default_response
      end
    when 'PUT'
      Sinatra::Base.put endpoint.path do
        endpoint.add_request request.body.read
        endpoint.default_response
      end
    when 'PATCH'
      Sinatra::Base.patch endpoint.path do
        endpoint.add_request request.body.read
        endpoint.default_response
      end
    when 'POST'
      Sinatra::Base.post endpoint.path do
        endpoint.add_request request.body.read
        endpoint.default_response
      end
    when 'DELETE'
      Sinatra::Base.delete endpoint.path do
        endpoint.add_request request.body.read
        endpoint.default_response
      end
  end

  Sinatra::Base.get "#{endpoint.path}/response" do
    if env.key?('CONTENT_TYPE') && env['CONTENT_TYPE'] && env['CONTENT_TYPE'].include?('json')
      endpoint.response
    else
      ERB.new(@@erb_files[:response]).result binding
    end
  end

  Sinatra::Base.put "#{endpoint.path}/response" do
    endpoint.response = request.body.read
  end

  Sinatra::Base.post "#{endpoint.path}/response" do
    new_response = request.body.read.sub 'response=', ''
    endpoint.response = new_response
    redirect "#{endpoint.path}/response"
  end

  Sinatra::Base.delete "#{endpoint.path}/response" do
    endpoint.response = endpoint.default_response
  end

  Sinatra::Base.get "#{endpoint.path}/requests" do
    if env.key?('CONTENT_TYPE') && env['CONTENT_TYPE'] && env['CONTENT_TYPE'].include?('json')
      endpoint.requests.to_json
    else
      ERB.new(@@erb_files[:request]).result binding
    end
  end

  Sinatra::Base.delete "#{endpoint.path}/requests" do
    endpoint.requests = []
  end
end

.reset_endpointsObject

TODO: Should this just be ‘reset’?



123
124
125
# File 'lib/http_sim.rb', line 123

def self.reset_endpoints
  @@endpoints = []
end

.run!(port: 4567) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/http_sim.rb', line 46

def self.run!(port: 4567)
  check_if_port_in_use(port)

  Sinatra::Base.get '/' do
    ERB.new(@@erb_files[:index]).result binding
  end

  Class.new(Sinatra::Base) {
    set :port, port
    set :logging, false

    include HttpSimulator
  }.run!
end

.run_daemon!(port: 4567, max_wait_seconds: 5, log_file: 'http_sim_log.log') ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/http_sim.rb', line 61

def self.run_daemon!(port: 4567, max_wait_seconds: 5, log_file: 'http_sim_log.log')
  @@pid = Process.fork do
    $stdout.reopen(log_file, 'w')
    $stdout.sync = true
    $stderr.reopen($stdout)
    run!(port: port)
  end

  wait_for_start(port, max_wait_seconds)

  at_exit do
    Process.kill 'SIGKILL', @@pid
  end

  @@pid
end

.stop_daemon!(port: 4567, max_wait_seconds: 5) ⇒ Object



78
79
80
81
# File 'lib/http_sim.rb', line 78

def self.stop_daemon!(port: 4567, max_wait_seconds: 5)
  Process.kill('SIGKILL', @@pid) if @@pid
  wait_for_stop(port, max_wait_seconds)
end

.wait_for_start(port, max_wait_seconds) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/http_sim.rb', line 83

def self.wait_for_start(port, max_wait_seconds)
  wait_count = 0
  while wait_count < max_wait_seconds * 4
    begin
      HTTParty.get("http://localhost:#{port}/")
      return
    rescue Errno::ECONNREFUSED
      wait_count += 1
      sleep 0.25
    end
  end

  raise "Simulators failed to start - timed out after #{max_wait_seconds} seconds!"
end

.wait_for_stop(port, max_wait_seconds) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/http_sim.rb', line 98

def self.wait_for_stop(port, max_wait_seconds)
  wait_count = 0
  while wait_count < max_wait_seconds * 4
    begin
      HTTParty.get("http://localhost:#{port}/")
      wait_count += 1
      sleep 0.25
    rescue Errno::ECONNREFUSED
      return
    end
  end

  raise "Simulators failed to stop - timed out after #{max_wait_seconds} seconds!"
end