Class: Spec::Performance::Client::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/spec/performance/client/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_uri) ⇒ HttpClient

Returns a new instance of HttpClient.



12
13
14
15
16
17
# File 'lib/spec/performance/client/http_client.rb', line 12

def initialize(base_uri)
  @base_uri = base_uri
  @headers = {}
  @cookies = {}
  @recording = true
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



10
11
12
# File 'lib/spec/performance/client/http_client.rb', line 10

def base_uri
  @base_uri
end

#cookiesObject

Returns the value of attribute cookies.



10
11
12
# File 'lib/spec/performance/client/http_client.rb', line 10

def cookies
  @cookies
end

#recording=(value) ⇒ Object (writeonly)

Sets the attribute recording

Parameters:

  • value

    the value to set the attribute recording to.



9
10
11
# File 'lib/spec/performance/client/http_client.rb', line 9

def recording=(value)
  @recording = value
end

Instance Method Details

#get(uri, params = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spec/performance/client/http_client.rb', line 30

def get(uri, params = {})
  if params && params.size > 0
    query = params2query(params)
    uri = URI.parse("#{uri}?#{query}")
  end

  http = Net::HTTP.start(uri.host, uri.port)
  response = http.get(uri.request_uri, headers)
  http.finish

  create_http_client_response(response)
end

#post(uri, params = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/spec/performance/client/http_client.rb', line 19

def post(uri, params = {})
  request = Net::HTTP::Post.new(uri.path, headers)
  request.form_data = params
  request.basic_auth uri.user, uri.password if uri.user
  response = Net::HTTP.new(uri.host, uri.port).start do |http|
    http.request(request)
  end
  capture(response) if recording?
  create_http_client_response(response)
end

#recording?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/spec/performance/client/http_client.rb', line 43

def recording?
  @recording
end