Class: Rots::Mocks::Fetcher

Inherits:
Object
  • Object
show all
Defined in:
lib/rots/mocks/mock_fetcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Fetcher

Returns a new instance of Fetcher.



9
10
11
# File 'lib/rots/mocks/mock_fetcher.rb', line 9

def initialize(app)
  @app = app
end

Instance Method Details

#fetch(url, body = nil, headers = nil, limit = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rots/mocks/mock_fetcher.rb', line 13

def fetch(url, body = nil, headers = nil, limit = nil)
  opts = (headers || {}).dup
  opts[:input] = body
  opts[:method] = "POST" if body
  env = Rack::MockRequest.env_for(url, opts)

  status, headers, body = @app.call(env)

  buf = []
  buf << "HTTP/1.1 #{status} #{Rack::Utils::HTTP_STATUS_CODES[status]}"
  headers.each { |header, value| buf << "#{header}: #{value}" }
  buf << ""
  body.each { |part| buf << part }

  io = Net::BufferedIO.new(StringIO.new(buf.join("\n")))
  res = Net::HTTPResponse.read_new(io)
  res.reading_body(io, true) {}
  OpenID::HTTPResponse._from_net_response(res, url)
end