diff options
| author | Gibheer <gibheer@gmail.com> | 2013-10-22 17:07:16 +0200 | 
|---|---|---|
| committer | Gibheer <gibheer@gmail.com> | 2013-10-22 17:07:16 +0200 | 
| commit | 6ad7650c05418a0de428c2522a6726fc6f7e4bcf (patch) | |
| tree | d9e197a6d262c848737ba6b2babe18f257d30ff4 | |
| parent | 06b851bd4f90109eae4bda8f9b69c60b221f6275 (diff) | |
add cookie support in requests
| -rw-r--r-- | lib/zero/request/parameter.rb | 21 | ||||
| -rw-r--r-- | spec/unit/zero/request/parameter/cookie_spec.rb | 21 | 
2 files changed, 41 insertions, 1 deletions
| diff --git a/lib/zero/request/parameter.rb b/lib/zero/request/parameter.rb index faae5c7..c4de839 100644 --- a/lib/zero/request/parameter.rb +++ b/lib/zero/request/parameter.rb @@ -16,6 +16,8 @@ module Zero        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 @@ module Zero        ].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 @@ module Zero        # 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 @@ module Zero        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 @@ module Zero        # @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 @@ module Zero          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 diff --git a/spec/unit/zero/request/parameter/cookie_spec.rb b/spec/unit/zero/request/parameter/cookie_spec.rb new file mode 100644 index 0000000..f1f5d9d --- /dev/null +++ b/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 | 
