Class: HTTPI::Adapter::Rack

Inherits:
Base
  • Object
show all
Defined in:
lib/httpi/adapter/rack.rb

Overview

HTTPI::Adapter::Rack

Adapter for Rack::MockRequest. Due to limitations, not all features are supported. github.com/rack/rack/blob/master/lib/rack/mock.rb

Usage:

HTTPI::Adapter::Rack.mount 'application', RackApplication
HTTPI.get("http://application/path", :rack)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

register

Constructor Details

#initialize(request) ⇒ Rack

Returns a new instance of Rack.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/httpi/adapter/rack.rb', line 40

def initialize(request)
  @app = self.class.mounted_apps[request.url.host]


  if @app.nil?
    message  = "Application '#{request.url.host}' not mounted: ";
    message += "use `HTTPI::Adapter::Rack.mount('#{request.url.host}', RackApplicationClass)`"

    raise message
  end

  @request = request
  @client  = ::Rack::MockRequest.new(@app)
end

Class Attribute Details

.mounted_appsObject

Returns the value of attribute mounted_apps.



24
25
26
# File 'lib/httpi/adapter/rack.rb', line 24

def mounted_apps
  @mounted_apps
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



21
22
23
# File 'lib/httpi/adapter/rack.rb', line 21

def client
  @client
end

Class Method Details

.mount(host, application) ⇒ Object

Attaches Rack endpoint at specified host. Endpoint will be acessible at / url.



31
32
33
# File 'lib/httpi/adapter/rack.rb', line 31

def self.mount(host, application)
  self.mounted_apps[host] = application
end

.unmount(host) ⇒ Object

Removes Rack endpoint.



36
37
38
# File 'lib/httpi/adapter/rack.rb', line 36

def self.unmount(host)
  self.mounted_apps.delete(host)
end

Instance Method Details

#request(method) ⇒ Object

Executes arbitrary HTTP requests. You have to mount required Rack application before you can use it.

See Also:



60
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
90
91
92
93
94
95
96
# File 'lib/httpi/adapter/rack.rb', line 60

def request(method)
  unless REQUEST_METHODS.include? method
    raise NotSupportedError, "Rack adapter does not support custom HTTP methods"
  end

  if @request.proxy
    raise NotSupportedError, "Rack adapter does not support proxying"
  end

  if @request.auth.http?
    if @request.auth.basic?
      basic_auth = @request.auth.basic.join(':')
      encoded = Base64.encode64(basic_auth).gsub('\n', '')
      @request.headers['Authorization'] = "Basic #{encoded}"
    else
      raise NotSupportedError, "Rack adapter does not support HTTP #{@request.auth.type} auth"
    end
  end

  if @request.auth.ssl?
    raise NotSupportedError, "Rack adapter does not support SSL client auth"
  end

  if @request.on_body
    raise NotSupportedError, "Rack adapter does not support response streaming"
  end

  env = {}
  @request.headers.each do |header, value|
    env["HTTP_#{header.gsub('-', '_').upcase}"] = value
  end

  response = @client.request(method.to_s.upcase, @request.url.to_s,
        { :fatal => true, :input => @request.body.to_s }.merge(env))

  Response.new(response.status, response.headers, response.body)
end