Class: Cybertrain::Test::HttpReceiver
- Defined in:
- cybertrain/test/http_receiver.rb
Overview
A receiver for outbound HTTP calls in a test: start binds a port on
127.0.0.1, the application calls it, #received_request returns what arrived.
It accepts max connections (1 by default), reads one request from
each (Content-Length or chunked body) and answers it with the canned
response and Connection: close.
Cybertrain::HttpClient refuses loopback addresses unless the test sets
allow_private: true (or Cybertrain.config.http_allow_private = true).
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Receiver
Instance Method Summary collapse
-
#initialize(status: 200, headers: HttpReceiver.no_headers, body: "ok", chunked: false, gzip: false, max: 1, accept_window: 10.0, read_timeout: 5.0) ⇒ HttpReceiver
constructor
A new instance of HttpReceiver.
-
#received_request(wait = 5.0) ⇒ ReceivedRequest?
The first request, waiting for it at most
waitseconds. -
#received_requests(count = 0, wait = 5.0) ⇒ Array<ReceivedRequest>
The requests that arrived, waiting at most
waitseconds forcountof them (0: the receiver’smax). -
#respond_with(status: 200, headers: HttpReceiver.no_headers, body: "ok", chunked: false, redirect_to: "", chunks: HttpReceiver.no_chunks, gzip: false) ⇒ nil
Changes the canned response for the requests still to come.
-
#url(path = "/") ⇒ String
“http://127.0.0.1:
", once started.
Methods inherited from Receiver
Constructor Details
#initialize(status: 200, headers: HttpReceiver.no_headers, body: "ok", chunked: false, gzip: false, max: 1, accept_window: 10.0, read_timeout: 5.0) ⇒ HttpReceiver
Returns a new instance of HttpReceiver.
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'cybertrain/test/http_receiver.rb', line 118 def initialize(status: 200, headers: HttpReceiver.no_headers, body: "ok", chunked: false, gzip: false, max: 1, accept_window: 10.0, read_timeout: 5.0) super(max, accept_window, read_timeout) @reply_status = 200 @reply_headers = HttpReceiver.no_headers @reply_body = "" @reply_chunked = false @reply_chunks = HttpReceiver.no_chunks @requests = Array.new(0) { ReceivedRequest.new("", "", HttpReceiver.no_headers, "") } @lock = Mutex.new @body_failed = false respond_with(status: status, headers: headers, body: body, chunked: chunked, gzip: gzip) end |
Instance Method Details
#received_request(wait = 5.0) ⇒ ReceivedRequest?
The first request, waiting for it at most wait seconds.
183 184 185 186 187 |
# File 'cybertrain/test/http_receiver.rb', line 183 def received_request(wait = 5.0) return nil unless await_count(1, wait) @requests[0] end |
#received_requests(count = 0, wait = 5.0) ⇒ Array<ReceivedRequest>
The requests that arrived, waiting at most wait seconds for
count of them (0: the receiver’s max). Fewer when the time ran
out.
196 197 198 199 200 201 202 203 204 205 206 |
# File 'cybertrain/test/http_receiver.rb', line 196 def received_requests(count = 0, wait = 5.0) await_count(count > 0 ? count : @max, wait) out = Array.new(0) { ReceivedRequest.new("", "", HttpReceiver.no_headers, "") } n = received_count i = 0 while i < n out << @requests[i] i += 1 end out end |
#respond_with(status: 200, headers: HttpReceiver.no_headers, body: "ok", chunked: false, redirect_to: "", chunks: HttpReceiver.no_chunks, gzip: false) ⇒ nil
Changes the canned response for the requests still to come.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'cybertrain/test/http_receiver.rb', line 153 def respond_with(status: 200, headers: HttpReceiver.no_headers, body: "ok", chunked: false, redirect_to: "", chunks: HttpReceiver.no_chunks, gzip: false) @reply_status = status.to_i copy = HttpReceiver.no_headers headers.each_key { |k| copy[k.to_s] = headers[k].to_s } location = redirect_to.to_s copy["Location"] = location unless location.empty? copy["Content-Encoding"] = "gzip" if gzip @reply_headers = copy text = body.to_s @reply_body = gzip ? HttpReceiver.rx_gzip(text) : text @reply_chunked = chunked ? true : false frames = HttpReceiver.no_chunks chunks.each { |c| frames << c.to_s } @reply_chunks = frames nil end |
#url(path = "/") ⇒ String
“http://127.0.0.1:
175 176 177 |
# File 'cybertrain/test/http_receiver.rb', line 175 def url(path = "/") "http://127.0.0.1:#{@port}#{path}" end |