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
- .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! ⇒ Object
- .wait_for_start(port, max_wait_seconds) ⇒ Object
Class Method Details
.check_if_port_in_use(port) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/http_sim.rb', line 97 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
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 |
# File 'lib/http_sim.rb', line 111 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 ERB.new(@@erb_files[:response]).result binding end Sinatra::Base.put "#{endpoint.path}/response" do endpoint.response = request.body.read end Sinatra::Base.delete "#{endpoint.path}/response" do endpoint.response = endpoint.default_response end Sinatra::Base.get "#{endpoint.path}/requests" do @endpoint = endpoint ERB.new(@@erb_files[:request]).result binding end Sinatra::Base.delete "#{endpoint.path}/requests" do endpoint.requests = [] end end |
.reset_endpoints ⇒ Object
TODO: Should this just be ‘reset’?
107 108 109 |
# File 'lib/http_sim.rb', line 107 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! ⇒ Object
78 79 80 |
# File 'lib/http_sim.rb', line 78 def self.stop_daemon! Process.kill('SIGKILL', @@pid) if @@pid end |
.wait_for_start(port, max_wait_seconds) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/http_sim.rb', line 82 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 |