Module: MockHTTP

Defined in:
lib/ramaze/spec/helper/mock_http.rb

Constant Summary collapse

DEFAULTS =
{
  'REMOTE_ADDR' => '127.0.0.1'
}
MOCK_URI =
URI::HTTP.build(
  :host => 'localhost',
  :port => 80
)
FISHING =
{
  :input => :input,
  :referrer => 'HTTP_REFERER',
  :referer => 'HTTP_REFERER',
  :cookie => 'HTTP_COOKIE',
  :if_none_match=> 'HTTP_IF_NONE_MATCH',
  :if_modified_since=> 'HTTP_IF_MODIFIED_SINCE',
}
MOCK_REQUEST =
::Rack::MockRequest.new(Ramaze::Adapter::Fake)

Instance Method Summary collapse

Instance Method Details

#create_url(path, query) ⇒ Object



56
57
58
59
60
61
# File 'lib/ramaze/spec/helper/mock_http.rb', line 56

def create_url(path, query)
  uri = MOCK_URI.dup
  uri.path = path
  uri.query = make_query(query)
  uri.to_s
end

#delete(*args) ⇒ Object



38
# File 'lib/ramaze/spec/helper/mock_http.rb', line 38

def delete(*args) mock_request(:delete, *args) end

#get(*args) ⇒ Object



35
# File 'lib/ramaze/spec/helper/mock_http.rb', line 35

def get(*args)    mock_request(:get,    *args) end

#make_query(query) ⇒ Object



63
64
65
66
67
68
# File 'lib/ramaze/spec/helper/mock_http.rb', line 63

def make_query query
	return query unless query and not query.is_a?(String)
   query.map{|key, value|
     "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}"
   }.join('&')
end

#mock_request(meth, path, query = {}) ⇒ Object



40
41
42
43
# File 'lib/ramaze/spec/helper/mock_http.rb', line 40

def mock_request(meth, path, query = {})
  uri, options = process_request(path, query)
  MOCK_REQUEST.send(meth, uri, DEFAULTS.merge(options))
end

#post(*args) ⇒ Object



37
# File 'lib/ramaze/spec/helper/mock_http.rb', line 37

def post(*args)   mock_request(:post,   *args) end

#process_request(path, query) ⇒ Object



49
50
51
52
53
54
# File 'lib/ramaze/spec/helper/mock_http.rb', line 49

def process_request(path, query)
  options = {}
  FISHING.each{|key, value|
    options[value] = query.delete(key)} if query.is_a?(Hash)
  [create_url(path, query), options]
end

#put(*args) ⇒ Object



36
# File 'lib/ramaze/spec/helper/mock_http.rb', line 36

def put(*args)    mock_request(:put,    *args) end

#raw_mock_request(meth, uri, options = {}) ⇒ Object



45
46
47
# File 'lib/ramaze/spec/helper/mock_http.rb', line 45

def raw_mock_request(meth, uri, options = {})
  MOCK_REQUEST.send(meth, uri, DEFAULTS.merge(options))
end