module RSpec::Httpd
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/MethodLength
Constants
- VERSION
Attributes
config[R]
Public Instance Methods
client(host:, port:, command: nil)
click to toggle source
builds and returns a client.
You can use this method to retrieve a client connection to a server specified via host:, port:, and, optionally, a command.
# File lib/rspec/httpd.rb, line 39 def client(host:, port:, command: nil) @clients ||= {} @clients[[host, port, command]] ||= begin Server.start!(host: host, port: port, command: command) if command Client.new host: host, port: port end end
configure(&block)
click to toggle source
Set the configuration for the default client.
See also: RSpec::Httpd.http
# File lib/rspec/httpd.rb, line 27 def configure(&block) @config = Config.new.tap(&block) end
expect_response(expected = nil, status: nil, client: nil) { || ... }
click to toggle source
# File lib/rspec/httpd.rb, line 61 def expect_response(expected = nil, status: nil, client: nil) if expected.nil? && block_given? expected = yield end expected = stringify_hash(expected) if expected.is_a?(Hash) client ||= http # only check status? This lets us write # # expect_response 201 # expected_status = expected.is_a?(Integer) && status.nil? ? expected : status || 200 response = client.response request = response.request if response.status != expected_status error_message = <<~MSG #{response.class} #{response.request.class} HTTP status should be #{expected_status}, but is #{response.status}, on '#{request}' MSG if response.status >= 400 error_message += <<~MSG ---- response.body ---------------------------------------------------- #{response.body} ----------------------------------------------------------------------- MSG end expect(response.status).to eq(expected_status), error_message end return if expected.nil? || expected.is_a?(Integer) begin # expect! comes from the expectation gem expect! response.content => expected rescue ::Expectation::Matcher::Mismatch => e raise ExpectationFailed.new(e, response: response), cause: nil end end
Private Instance Methods
http()
click to toggle source
returns the default client
The default client is the one configured via RSpec::Httpd.configure
.
# File lib/rspec/httpd.rb, line 52 def http config = ::RSpec::Httpd.config || raise("RSpec::Httpd configuration missing; run RSpec::Httpd.configure { |config| ... }") client(host: config.host, port: config.port, command: config.command) end
stringify_hash(hsh)
click to toggle source
# File lib/rspec/httpd.rb, line 109 def stringify_hash(hsh) return unless hsh hsh.inject({}) do |r, (k, v)| k = k.to_s if k.is_a?(Symbol) r.update k => v end end