Skip to content
Browse files

add cookie support in requests

  • Loading branch information...
1 parent 06b851b commit 6ad7650c05418a0de428c2522a6726fc6f7e4bcf @Gibheer Gibheer committed Oct 22, 2013
Showing with 41 additions and 1 deletion.
  1. +20 −1 lib/zero/request/parameter.rb
  2. +21 −0 spec/unit/zero/request/parameter/cookie_spec.rb
View
21 lib/zero/request/parameter.rb
@@ -16,6 +16,8 @@ class Parameter
ENV_KEY_CUSTOM = 'zero.params.custom'
# the key for the content type
ENV_KEY_CONTENT_TYPE = 'CONTENT_TYPE'
+ # the key to cookie heaven
+ ENV_KEY_COOKIES = 'HTTP_COOKIE'
# the separator of the type and charset
# for example multipart/form-data; charset=UTF-8
CONTENT_TYPE_SEPERATOR = ';'
@@ -26,6 +28,10 @@ class Parameter
].to_set
# match keys for list attribute
REGEX_MATCH_LIST = /\[\]$/
+ # split cookie header on =
+ REGEX_SPLIT_COOKIE = /=/
+ # split cookie seperator
+ REGEX_SPLIT_COOKIES = /;\s*/
# get the query parameters
attr_reader :query
@@ -38,6 +44,9 @@ class Parameter
# get all custom parameters
attr_reader :custom
+ # get all cookie parameters
+ attr_reader :cookie
+
# creates a new parameter instance
#
# This should never be called directly, as it will be generated for you.
@@ -47,6 +56,7 @@ class Parameter
def initialize(environment)
@query = extract_query_params(environment)
@payload = extract_payload_params(environment)
+ @cookie = extract_cookie_params(environment)
if environment.has_key?(ENV_KEY_CUSTOM)
@custom = environment[ENV_KEY_CUSTOM]
else
@@ -66,7 +76,7 @@ def initialize(environment)
# @param key [String] a key to look for
# @return [String] the value of the key
def [](key)
- @custom[key] || @payload[key] || @query[key]
+ @custom[key] || @payload[key] || @query[key] || @cookie[key]
end
# set a custom key/value pair
@@ -96,6 +106,15 @@ def extract_payload_params(environment)
parse_string(environment[ENV_KEY_PAYLOAD].read)
end
+ # extracts the cookie key value pairs
+ # @return Hash all key value pairs from cookies
+ def extract_cookie_params(environment)
+ return {} unless environment.has_key?(ENV_KEY_COOKIES)
+ r = Hash[environment[ENV_KEY_COOKIES].split(REGEX_SPLIT_COOKIES).map do |e|
+ e.split(REGEX_SPLIT_COOKIE)
+ end]
+ end
+
# check if the content-type matches one of the payload types
# @param [String] type - the content type string
# @return Boolean true if it matches
View
21 spec/unit/zero/request/parameter/cookie_spec.rb
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+describe Zero::Request::Parameter, '#cookie' do
+ subject { Zero::Request::Parameter.new(env) }
+ let(:env) { EnvGenerator.get('/foo', {'HTTP_COOKIE' => cookie}) }
+
+ context 'without parameters' do
+ let(:env) { EnvGenerator.get('/foo') }
+ its(:cookie) { should == {} }
+ end
+
+ context 'with a single key value pair' do
+ let(:cookie) { 'foo=bar' }
+ its(:cookie) { should == {'foo' => 'bar'} }
+ end
+
+ context 'with multiple key value pairs' do
+ let(:cookie) { 'foo=bar; baz=foobar' }
+ its(:cookie) { should == {'foo' => 'bar', 'baz' => 'foobar'} }
+ end
+end

0 comments on commit 6ad7650

Please sign in to comment.
Something went wrong with that request. Please try again.