Class: Cybertrain::Test::HttpReceiver

Inherits:
Receiver
  • Object
show all
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).

Examples:

require "cybertrain/test/http_receiver"

test "the signup posts a webhook" do
  hook = Cybertrain::Test::HttpReceiver.new(status: 202, body: "queued")
  hook.start
  Cybertrain.config.http_allow_private = true
  Notifier.(hook.url("/hooks/signup"), "ann@example.com")  # the code under test
  req = hook.received_request          # waits at most 5 s; nil if nothing came
  hook.stop
  assert_equal "POST", req.http_method
  assert_equal "/hooks/signup", req.http_target
  assert_equal "application/json", req.http_header("Content-Type")
end

Direct Known Subclasses

SentryReceiver

Instance Attribute Summary

Attributes inherited from Receiver

#port

Instance Method Summary collapse

Methods inherited from Receiver

#serving?, #start, #stop

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.

Parameters:

  • status (Integer) (defaults to: 200) —

    the canned response’s status

  • headers (Hash{String => String}) (defaults to: HttpReceiver.no_headers) —

    extra response headers

  • body (String) (defaults to: "ok") —

    the canned response’s body

  • chunked (Boolean) (defaults to: false) —

    send the body with Transfer-Encoding: chunked

  • gzip (Boolean) (defaults to: false) —

    send the body gzip-encoded (see #respond_with)

  • max (Integer) (defaults to: 1) —

    how many connections (one request each) to accept

  • accept_window (Numeric) (defaults to: 10.0) —

    seconds to wait for each connection before the receiver gives up and closes its port

  • read_timeout (Numeric) (defaults to: 5.0) —

    seconds of silence that end a connection



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.

Parameters:

  • wait (Numeric) (defaults to: 5.0)

Returns:



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.

Parameters:

  • count (Integer) (defaults to: 0)
  • wait (Numeric) (defaults to: 5.0)

Returns:



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.

Examples:

a redirect, then a body served in three chunked frames

hook.respond_with(status: 302, redirect_to: "/next")
hook.respond_with(status: 200, chunks: ["one", "two", "three"])

Parameters:

  • status (Integer) (defaults to: 200)
  • headers (Hash{String => String}) (defaults to: HttpReceiver.no_headers)
  • body (String) (defaults to: "ok")
  • chunked (Boolean) (defaults to: false)
  • redirect_to (String) (defaults to: "") —

    when not empty, sent as the Location header (give the 3xx status with it)

  • chunks (Array<String>) (defaults to: HttpReceiver.no_chunks) —

    when not empty, the body instead of body:: each String is one chunked frame, written and flushed with a short pause before the next

  • gzip (Boolean) (defaults to: false) —

    send body: gzip-encoded, with Content-Encoding: gzip. The receiver has no deflate of its own (no zlib in a test program that does not ask for it): the gzip stream carries the body in stored (uncompressed) blocks, which any gzip reader inflates. To test a compressed body, pass gzip bytes as body: with a Content-Encoding header instead.

Returns:

  • (nil)


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:", once started.

Parameters:

  • path (String) (defaults to: "/")

Returns:

  • (String)


175
176
177
# File 'cybertrain/test/http_receiver.rb', line 175

def url(path = "/")
  "http://127.0.0.1:#{@port}#{path}"
end