Module: RSpec::Httpd

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

Overview

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

Defined Under Namespace

Modules: GemHelper, Server Classes: Client

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
# File 'lib/rspec/httpd.rb', line 61

def expect_response(expected = nil, status: nil, client: nil)
  if expected.nil? && block_given?
    expected = yield
  end

  client ||= http

  # only check status? This lets us write
  #
  #    expect_response 201
  #
  if expected.is_a?(Integer) && status.nil?
    expect(client.status).to eq(expected)
    return
  end

  # do_expect_last_request is implemented in RSpec::Httpd::Expectation, and mixed in
  # here, because it needs access to the expect() implementation.

  expect(client.status).to eq(status || 200)
  return if expected.nil?

  begin
    # expect! comes from the expectation gem
    expect! client.result => expected
  rescue ::Expectation::Matcher::Mismatch
    raise ExpectationFailed.new($!, client.request)
  end
end

#loggerObject

:nodoc:



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

def logger #:nodoc:
  @logger ||= Logger.new(STDERR).tap { |logger| logger.level = Logger::INFO }
end