Class: Mosquito::MockRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/mosquito.rb

Overview

Mock request is used for composing the request body and headers

Constant Summary collapse

DEFAULT_HEADERS =
{
  'SERVER_NAME' => 'test.host',
  'PATH_INFO' => '',
  'HTTP_ACCEPT_ENCODING' => 'gzip,deflate',
  'HTTP_USER_AGENT' => 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.8.0.1) Gecko/20060214 Camino/1.0',
  'SCRIPT_NAME' => '/',
  'SERVER_PROTOCOL' => 'HTTP/1.1',
  'HTTP_CACHE_CONTROL' => 'max-age=0',
  'HTTP_ACCEPT_LANGUAGE' => 'en,ja;q=0.9,fr;q=0.9,de;q=0.8,es;q=0.7,it;q=0.7,nl;q=0.6,sv;q=0.5,nb;q=0.5,da;q=0.4,fi;q=0.3,pt;q=0.3,zh-Hans;q=0.2,zh-Hant;q=0.1,ko;q=0.1',
  'HTTP_HOST' => 'test.host',
  'REMOTE_ADDR' => '127.0.0.1',
  'SERVER_SOFTWARE' => 'Mongrel 0.3.12.4',
  'HTTP_KEEP_ALIVE' => '300',
  'HTTP_REFERER' => 'http://localhost/',
  'HTTP_ACCEPT_CHARSET' => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7',
  'HTTP_VERSION' => 'HTTP/1.1',
  'REQUEST_URI' => '/',
  'SERVER_PORT' => '80',
  'GATEWAY_INTERFACE' => 'CGI/1.2',
  'HTTP_ACCEPT' => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5',
  'HTTP_CONNECTION' => 'keep-alive',
  'REQUEST_METHOD' => 'GET',
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMockRequest

Returns a new instance of MockRequest.



106
107
108
109
# File 'lib/mosquito.rb', line 106

def initialize
  @headers = DEFAULT_HEADERS.with_indifferent_access # :-)
  @body = StringIO.new('')
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Allow getters like this:

o.REQUEST_METHOD or o.request_method


155
156
157
158
159
160
161
# File 'lib/mosquito.rb', line 155

def method_missing(method_name, *args)
  triables = [method_name.to_s, method_name.to_s.upcase, "HTTP_" + method_name.to_s.upcase]
  triables.map do | possible_key |
    return @headers[possible_key] if @headers.has_key?(possible_key)
  end
  super(method_name, args)
end

Instance Attribute Details

#bodyObject

Should be a StringIO. However, you got some assignment methods that will stuff it with encoded parameters for you



78
79
80
# File 'lib/mosquito.rb', line 78

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



79
80
81
# File 'lib/mosquito.rb', line 79

def headers
  @headers
end

Instance Method Details

#[](key) ⇒ Object

Gets a header



117
118
119
# File 'lib/mosquito.rb', line 117

def [](key)
  @headers[key]
end

#[]=(key, value) ⇒ Object Also known as: set

Sets a header



122
123
124
# File 'lib/mosquito.rb', line 122

def []=(key, value)
  @headers[key] = value
end

#append_to_query_string(piece) ⇒ Object

Append a freeform segment to the query string in the request. Useful when you want to quickly combine the query strings.



170
171
172
173
# File 'lib/mosquito.rb', line 170

def append_to_query_string(piece)
  new_qs = '?' + [self.query_string.gsub(/^\?/, ''), piece].reject{|e| e.blank? }.join('&')
  self.query_string = new_qs
end

#domainObject

Retrieve the domain (analogous to HTTP_HOST)



144
145
146
# File 'lib/mosquito.rb', line 144

def domain
  server_name || http_host
end

#domain=(nd) ⇒ Object

Set the domain (changes both HTTP_HOST and SERVER_NAME)



149
150
151
# File 'lib/mosquito.rb', line 149

def domain=(nd)
  self['SERVER_NAME'] = self['HTTP_HOST'] = nd
end

#generate_boundaryObject

Generates a random 22-character MIME boundary (useful for composing multipart POSTs)



190
191
192
# File 'lib/mosquito.rb', line 190

def generate_boundary
   "msqto-" + Mosquito::garbage(16)
end

#post_params=(new_param_hash_or_str) ⇒ Object

Assign a hash of parameters that should be used for POST. These might include objects that act like a file upload (with #original_filename and all)



177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mosquito.rb', line 177

def post_params=(new_param_hash_or_str)
  # First see if this is a body payload
  if !new_param_hash_or_str.kind_of?(Hash)
    compose_verbatim_payload(new_param_hash_or_str)
  # then check if anything in the new param hash resembles an uplaod
  elsif extract_values(new_param_hash_or_str).any?{|value| value.respond_to?(:original_filename) }
    compose_multipart_params(new_param_hash_or_str)
  else
    compose_urlencoded_params(new_param_hash_or_str)
  end
end

#query_stringObject

Retrieve a composed query string (including the eventual “?”) with URL-escaped segments



128
129
130
# File 'lib/mosquito.rb', line 128

def query_string
  (@query_string_with_qmark.blank? ? '' : @query_string_with_qmark)
end

#query_string=(nqs) ⇒ Object

Set a composed query string, should have URL-escaped segments and include the elements after the “?”



133
134
135
136
137
138
139
140
141
# File 'lib/mosquito.rb', line 133

def query_string=(nqs)
  @query_string_with_qmark = nqs.gsub(/^([^\?])/, '?\1')
  @headers["REQUEST_URI"] = @headers["REQUEST_URI"].split(/\?/).shift + @query_string_with_qmark
  if nqs.blank?
    @headers.delete "QUERY_STRING"
  else
    @headers["QUERY_STRING"] = nqs.gsub(/^\?/, '')
  end
end

#query_string_params=(new_param_hash) ⇒ Object

Assign a hash of parameters that should be used for the query string



164
165
166
# File 'lib/mosquito.rb', line 164

def query_string_params=(new_param_hash)
  self.query_string = qs_build(new_param_hash)
end

#to_camping_argsObject

Return an array of Camping arguments



207
208
209
# File 'lib/mosquito.rb', line 207

def to_camping_args
  [@body, self]
end

#to_hashObject

Returns the hash of headers



112
113
114
# File 'lib/mosquito.rb', line 112

def to_hash
  @headers
end

#to_rack_requestObject

Return a hash that can be used as a Rack request



195
196
197
198
199
200
201
202
203
204
# File 'lib/mosquito.rb', line 195

def to_rack_request
  returning({}) do | inp |
    inp.merge! @headers
    inp.merge! 'rack.input' => @body, 
      'rack.version' => [0,1], 
      'rack.errors' => STDERR, 
      'rack.url_scheme' => 'http',
      'CONTENT_LENGTH' => @body.string.length.to_s # Rack throws an error when it does not proper clen
  end
end