Class: RoadForest::TestSupport::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
lib/roadforest/test-support/http-client.rb

Defined Under Namespace

Classes: Exchange

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, url) ⇒ HTTPClient

Returns a new instance of HTTPClient.



6
7
8
9
10
11
# File 'lib/roadforest/test-support/http-client.rb', line 6

def initialize(app, url)
  @app = app
  @default_url = Addressable::URI.parse(url)
  @exchanges = []
  @dispatcher = DispatcherFacade.new(@app.dispatcher)
end

Instance Attribute Details

#exchangesObject (readonly)

Returns the value of attribute exchanges.



12
13
14
# File 'lib/roadforest/test-support/http-client.rb', line 12

def exchanges
  @exchanges
end

Instance Method Details

#do_request(request) {|exchange| ... } ⇒ Object

Yields:

  • (exchange)


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/roadforest/test-support/http-client.rb', line 18

def do_request(request)
  uri = request.url

  uri = Addressable::URI.parse(uri)
  uri = @default_url.join(uri)

  exchange = Exchange.new

  exchange.method = request.method
  exchange.uri = uri
  exchange.body = request.body
  exchange.dispatcher = @dispatcher

  @exchanges << exchange

  exchange.header('Host', [uri.host, uri.port].compact.join(':'))
  exchange.header('Accept', '*/*')
  request.headers.each do |name, value|
    exchange.header(name, value)
  end

  yield exchange if block_given?

  #puts; puts "#{__FILE__}:#{__LINE__} => #{(request).inspect}"

  exchange.do_request

  response = HTTP::Response.new
  response.headers = exchange.response.headers.dup
  response.status = exchange.response.code
  response.body_string = exchange.response.body

  enrich_with_server_stuff(response)

  #puts; puts "#{__FILE__}:#{__LINE__} => #{(response).inspect}"
  return response
end

#enrich_with_server_stuff(response) ⇒ Object



56
57
58
59
60
# File 'lib/roadforest/test-support/http-client.rb', line 56

def enrich_with_server_stuff(response)
  response.headers["Server"]="RoadForest Test Server"
  response.headers["Date"]=Time.now.httpdate
  response.headers["Connection"] = "Keep-Alive"
end

#inspectObject



14
15
16
# File 'lib/roadforest/test-support/http-client.rb', line 14

def inspect
  "#<#{self.class.name}:#{"%0xd" % object_id} #{exchanges.length} exchanges>"
end