Module: RSpec::Httpd

Extended by:
Httpd
Included in:
Httpd
Defined in:
lib/rspec/httpd.rb,
lib/rspec/httpd/server.rb,
lib/rspec/httpd/version.rb,
lib/rspec/httpd/client.rb,
lib/rspec/httpd.rb

Overview

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/MethodLength

Defined Under Namespace

Modules: GemHelper, Server Classes: Client, Config, ExpectationFailed

Constant Summary collapse

VERSION =
GemHelper.version "rspec-httpd"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/rspec/httpd.rb', line 22

def config
  @config
end

Instance Method Details

#client(host:, port:, command: nil) ⇒ Object

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.



39
40
41
42
43
44
45
# 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) ⇒ Object

Set the configuration for the default client.

See also: RSpec::Httpd.http



27
28
29
# File 'lib/rspec/httpd.rb', line 27

def configure(&block)
  @config = Config.new.tap(&block)
end

#expect_response(expected = nil, status: nil, client: nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

#loggerObject

:nodoc:



31
32
33
# File 'lib/rspec/httpd.rb', line 31

def logger #:nodoc:
  @config.logger
end