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
- .run!(port: 4567) ⇒ Object
- .run_daemon!(port: 4567, max_wait_seconds: 5) ⇒ Object
- .stop_daemon! ⇒ Object
- .wait_for_start(port, max_wait_seconds) ⇒ Object
Class Method Details
.check_if_port_in_use(port) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/http_sim.rb', line 103 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
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 |
# File 'lib/http_sim.rb', line 116 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
112 113 114 |
# File 'lib/http_sim.rb', line 112 def self.reset_endpoints @@endpoints = [] end |
.run!(port: 4567) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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 include HttpSimulator }.run! end |
.run_daemon!(port: 4567, max_wait_seconds: 5) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/http_sim.rb', line 60 def self.run_daemon!(port: 4567, max_wait_seconds: 5) check_if_port_in_use(port) Sinatra::Base.get '/' do ERB.new(@@erb_files[:index]).result binding end @@pid = Process.fork do Class.new(Sinatra::Base) { set :port, port include HttpSimulator }.run! end wait_for_start(port, max_wait_seconds) at_exit do Process.kill 'SIGKILL', @@pid end @@pid end |
.stop_daemon! ⇒ Object
84 85 86 |
# File 'lib/http_sim.rb', line 84 def self.stop_daemon! Process.kill('SIGKILL', @@pid) if @@pid end |
.wait_for_start(port, max_wait_seconds) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/http_sim.rb', line 88 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 |