Module: HttpSimulator
- Defined in:
- lib/endpoint.rb,
lib/http_sim.rb
Defined Under Namespace
Classes: Endpoint
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
- .check_if_port_in_use(port) ⇒ Object
- .read_file(path) ⇒ Object
- .register_endpoint(method, path, default_response) ⇒ Object
-
.reset_endpoints ⇒ Object
TODO: Should this just be ‘reset’?.
- .run!(port: 4567) ⇒ Object
- .run_daemon!(port: 4567, max_wait_seconds: 5, log_file: 'http_sim_log.log') ⇒ Object
- .stop_daemon!(port: 4567, max_wait_seconds: 5) ⇒ Object
- .wait_for_start(port, max_wait_seconds) ⇒ Object
- .wait_for_stop(port, max_wait_seconds) ⇒ Object
Class Method Details
.check_if_port_in_use(port) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/http_sim.rb', line 92 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
8 9 10 11 12 13 14 15 16 |
# File 'lib/http_sim.rb', line 8 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
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 |
# File 'lib/http_sim.rb', line 106 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.response end when 'PUT' Sinatra::Base.put endpoint.path do endpoint.add_request request.body.read endpoint.response end when 'PATCH' Sinatra::Base.patch endpoint.path do endpoint.add_request request.body.read endpoint.response end when 'POST' Sinatra::Base.post endpoint.path do endpoint.add_request request.body.read endpoint.response end when 'DELETE' Sinatra::Base.delete endpoint.path do endpoint.add_request request.body.read endpoint.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 new_response = request.body.read endpoint.response = new_response 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_endpoints ⇒ Object
TODO: Should this just be ‘reset’?
102 103 104 |
# File 'lib/http_sim.rb', line 102 def self.reset_endpoints @@endpoints = [] end |
.run!(port: 4567) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/http_sim.rb', line 25 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/http_sim.rb', line 40 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
57 58 59 60 |
# File 'lib/http_sim.rb', line 57 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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/http_sim.rb', line 62 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
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/http_sim.rb', line 77 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 |