Permalink
Browse files

Allow nil content type

  • Loading branch information...
1 parent 9f51c72 commit 6e930adfae31a3f53a1e8f05e654723777ac3717 @mbj committed Jun 17, 2013
View
@@ -1,3 +1,3 @@
---
threshold: 12
-total_score: 77
+total_score: 74
View
@@ -22,7 +22,8 @@ UncommunicativeMethodName:
- !ruby/regexp /[A-Z]/
LongParameterList:
max_params: 2
- exclude: []
+ exclude:
+ - Request::Rack#self.accessor # 3 params
enabled: true
overrides: {}
FeatureEnvy:
View
@@ -1,12 +1,16 @@
class Request
# Rack request
class Rack < self
+
SERVER_PORT = Key.new('SERVER_PORT')
REQUEST_METHOD = Key.new('REQUEST_METHOD')
RACK_URL_SCHEME = Key.new('rack.url_scheme')
IF_MODIFIED_SINCE = Key.new('HTTP_IF_MODIFIED_SINCE')
CONTENT_LENGTH = Key.new('CONTENT_LENGTH')
+ # Error raised when an invalid rack env key is accessed
+ InvalidKeyError = Class.new(RuntimeError)
+
# Initialize object
#
# @param [Hash] rack_env
@@ -36,11 +40,12 @@ def initialize(rack_env)
#
# @api private
#
- def self.accessor(name, key)
+ def self.accessor(name, key, *args)
define_method(name) do
- access(key)
+ access(key, *args)
end
end
+ private_class_method :accessor
# Return http port
#
@@ -75,15 +80,26 @@ def request_method
end
memoize :request_method
+ CONTENT_LENGTH_REGEXP = /\A[0-9]+\z/.freeze
+
# Return content length
#
# @return [Fixnum]
#
# @api private
#
def content_length
- access(CONTENT_LENGTH).to_i(10)
+ value = @rack_env.fetch(CONTENT_LENGTH) do
+ return 0
+ end
+
+ unless value =~ CONTENT_LENGTH_REGEXP
+ raise InvalidKeyError, 'invalid content length'
+ end
+
+ value.to_i
end
+ memoize :content_length
# Return query params
#
@@ -116,11 +132,11 @@ def if_modified_since
end
memoize :if_modified_since
- accessor(:path_info, Key.new('PATH_INFO') )
- accessor(:host, Key.new('SERVER_NAME') )
- accessor(:query_string, Key.new('QUERY_STRING'))
- accessor(:content_type, Key.new('CONTENT_TYPE'))
- accessor(:body, Key.new('rack.input'))
+ accessor(:path_info, Key.new('PATH_INFO') )
+ accessor(:host, Key.new('SERVER_NAME') )
+ accessor(:query_string, Key.new('QUERY_STRING') )
+ accessor(:content_type, Key.new('CONTENT_TYPE'), nil)
+ accessor(:body, Key.new('rack.input') )
private
@@ -132,8 +148,9 @@ def if_modified_since
#
# @api private
#
- def access(key)
- @rack_env.fetch(key)
+ def access(key, *args)
+ @rack_env.fetch(key, *args)
end
- end
-end
+
+ end # Rack
+end # Request
@@ -1,4 +1,4 @@
-shared_examples_for 'a rack env accessor' do
+shared = proc do
let(:rack_key_value) { :Value }
let(:default_env) do
@@ -11,9 +11,13 @@
}
end
- let(:expected_value) { rack_key_value }
+ let(:expected_value) { rack_key_value }
+ let(:env) { default_env.merge(rack_key => rack_key_value) }
+ let(:object) { described_class.new(env) }
+end
- let(:env) { default_env.merge(rack_key => rack_key_value) }
+shared_examples_for 'a rack env accessor' do
+ instance_eval(&shared)
it { should eql(expected_value) }
@@ -22,3 +26,11 @@
env.frozen?.should be(false)
end
end
+
+shared_examples_for 'an invalid rack env accessor' do
+ instance_eval(&shared)
+
+ it 'should raise error' do
+ expect { subject }.to raise_error(Request::Rack::InvalidKeyError, expected_message)
+ end
+end
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+describe Request::Rack, '#content_length' do
+ subject { object.content_length }
+
+ let(:rack_key) { 'CONTENT_LENGTH'}
+
+ context 'with decimal positive integer' do
+ it_should_behave_like 'a rack env accessor' do
+
+ let(:rack_key_value) { '10' }
+ let(:expected_value) { 10 }
+
+ end
+ end
+
+ context 'with decimal negative integer' do
+
+ it_should_behave_like 'an invalid rack env accessor' do
+
+ let(:rack_key_value) { '-10' }
+ let(:expected_message) { 'invalid content length' }
+
+ end
+ end
+
+ context 'other garbadge' do
+
+ it_should_behave_like 'an invalid rack env accessor' do
+
+ let(:rack_key_value) { '0asd2431' }
+ let(:expected_message) { 'invalid content length' }
+
+ end
+ end
+
+ context 'when CONTENT_LENGTH key is not present' do
+
+ let(:rack_key) { 'SOMETHING_OTHER' }
+
+ it_should_behave_like 'a rack env accessor' do
+
+ let(:expected_value) { 0 }
+
+ end
+ end
+end
@@ -11,8 +11,6 @@
let(:rack_key_value) { protocol.name }
let(:expected_value) { protocol }
- #let(:expected_method)
-
end
end
end
@@ -6,7 +6,7 @@
it_should_behave_like 'a rack env accessor' do
let(:object) { described_class.new(env) }
- let(:rack_key) { 'QUERY_STRING' }
+ let(:rack_key) { 'QUERY_STRING' }
end
end
@@ -11,8 +11,6 @@
let(:rack_key_value) { method.verb }
let(:expected_value) { method }
- #let(:expected_method)
-
end
end
end

0 comments on commit 6e930ad

Please sign in to comment.