Class: RSpec::Httpd::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/httpd/client.rb

Defined Under Namespace

Modules: ResponseParser Classes: HeadersHash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, port:) ⇒ Client

Returns a new instance of Client.



10
11
12
# File 'lib/rspec/httpd/client.rb', line 10

def initialize(host:, port:)
  @host, @port = host, port
end

Instance Attribute Details

#hostObject (readonly)

host and port. Set at initialization



7
8
9
# File 'lib/rspec/httpd/client.rb', line 7

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



8
9
10
# File 'lib/rspec/httpd/client.rb', line 8

def port
  @port
end

#requestObject (readonly)

request, response, and status, set during a request/response cycle



15
16
17
# File 'lib/rspec/httpd/client.rb', line 15

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



16
17
18
# File 'lib/rspec/httpd/client.rb', line 16

def response
  @response
end

Instance Method Details

#delete(url, headers: {}) ⇒ Object

A DELETE request



48
49
50
# File 'lib/rspec/httpd/client.rb', line 48

def delete(url, headers: {})
  run_request(::Net::HTTP::Delete, url, body: nil, headers: headers)
end

#get(url, headers: {}) ⇒ Object

A GET request



33
34
35
# File 'lib/rspec/httpd/client.rb', line 33

def get(url, headers: {})
  run_request(::Net::HTTP::Get, url, body: nil, headers: headers)
end

#headersObject

returns the headers of the latest response



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

def headers
  @headers ||= HeadersHash.new(response)
end

#post(url, body, headers: {}) ⇒ Object

A POST request



38
39
40
# File 'lib/rspec/httpd/client.rb', line 38

def post(url, body, headers: {})
  run_request(::Net::HTTP::Post, url, body: body, headers: headers)
end

#put(url, body, headers: {}) ⇒ Object

A PUT request



43
44
45
# File 'lib/rspec/httpd/client.rb', line 43

def put(url, body, headers: {})
  run_request(::Net::HTTP::Put, url, body: body || {}, headers: headers)
end

#resultObject

returns the parsed response of the latest request



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

def result
  @result ||= ResponseParser.parse(response)
end

#statusObject



18
19
20
# File 'lib/rspec/httpd/client.rb', line 18

def status
  Integer(response.code)
end