Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/pr 125 add support for empty arrays in params #140

Closed
wants to merge 10 commits into from
26 changes: 17 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@ GEM
specs:
codeclimate-test-reporter (0.3.0)
simplecov (>= 0.7.1, < 1.0.0)
diff-lcs (1.2.3)
diff-lcs (1.2.5)
docile (1.1.3)
multi_json (1.9.0)
rack (1.5.2)
rack-protection (1.5.0)
rack
rake (10.0.4)
rspec (2.13.0)
rspec-core (~> 2.13.0)
rspec-expectations (~> 2.13.0)
rspec-mocks (~> 2.13.0)
rspec-core (2.13.1)
rspec-expectations (2.13.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.13.1)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
rspec-mocks (~> 3.4.0)
rspec-core (3.4.3)
rspec-support (~> 3.4.0)
rspec-expectations (3.4.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-mocks (3.4.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.4.0)
rspec-support (3.4.1)
simplecov (0.8.2)
docile (~> 1.1.0)
multi_json
Expand All @@ -39,3 +44,6 @@ DEPENDENCIES
rake
rspec
sinatra

BUNDLED WITH
1.11.2
2 changes: 1 addition & 1 deletion lib/rack/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ def env_for(path, env)
# Stringifying and upcasing methods has be commit upstream
env["REQUEST_METHOD"] ||= env[:method] ? env[:method].to_s.upcase : "GET"

if env["REQUEST_METHOD"] == "GET"
if ["GET", "DELETE"].include?(env["REQUEST_METHOD"])
# merge :params with the query string
if params = env[:params]
params = parse_nested_query(params) if params.is_a?(String)
Expand Down
16 changes: 16 additions & 0 deletions lib/rack/test/cookie_jar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def secure?
@options.has_key?("secure")
end

def http_only?
@options.has_key?('HttpOnly')
end

# :api: private
def path
@options["path"].strip || "/"
Expand Down Expand Up @@ -87,6 +91,14 @@ def <=>(other)
# Orders the cookies from least specific to most
[name, path, domain.reverse] <=> [other.name, other.path, other.domain.reverse]
end
def to_h
@options.merge(
'value' => @value,
'HttpOnly' => http_only?,
'secure' => secure?,
)
end
alias_method :to_hash, :to_h

protected

Expand Down Expand Up @@ -115,6 +127,10 @@ def []=(name, value)
merge("#{name}=#{Rack::Utils.escape(value)}")
end

def get_cookie(name)
hash_for(nil).fetch(name,nil)
end

def delete(name)
@cookies.reject! do |cookie|
cookie.name == name
Expand Down
29 changes: 18 additions & 11 deletions lib/rack/test/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,21 @@ module Test

module Utils # :nodoc:
include Rack::Utils
extend Rack::Utils

def build_nested_query(value, prefix = nil)
case value
when Array
value.map do |v|
unless unescape(prefix) =~ /\[\]$/
prefix = "#{prefix}[]"
end
build_nested_query(v, "#{prefix}")
end.join("&")
if value.empty?
"#{prefix}[]="
else
value.map do |v|
unless unescape(prefix) =~ /\[\]$/
prefix = "#{prefix}[]"
end
build_nested_query(v, "#{prefix}")
end.join("&")
end
when Hash
value.map do |k, v|
build_nested_query(v, prefix ? "#{prefix}[#{escape(k)}]" : escape(k))
Expand All @@ -23,7 +28,6 @@ def build_nested_query(value, prefix = nil)
"#{prefix}=#{escape(value)}"
end
end

module_function :build_nested_query

def build_multipart(params, first = true)
Expand Down Expand Up @@ -83,13 +87,13 @@ def build_multipart(params, first = true)
flattened_params
end
end

module_function :build_multipart

private
private
def build_parts(parameters)
get_parts(parameters).join + "--#{MULTIPART_BOUNDARY}--\r"
end
module_function :build_parts

def get_parts(parameters)
parameters.map { |name, value|
Expand All @@ -115,25 +119,27 @@ def get_parts(parameters)
end
}
end
module_function :get_parts

def build_primitive_part(parameter_name, value)
unless value.is_a? Array
value = [value]
end
value.map do |v|
<<-EOF
<<-EOF
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{parameter_name}"\r
\r
#{v}\r
EOF
end.join
end
module_function :build_primitive_part

def build_file_part(parameter_name, uploaded_file)
::File.open(uploaded_file.path, "rb") do |physical_file|
physical_file.set_encoding(Encoding::BINARY) if physical_file.respond_to?(:set_encoding)
<<-EOF
<<-EOF
--#{MULTIPART_BOUNDARY}\r
Content-Disposition: form-data; name="#{parameter_name}"; filename="#{escape(uploaded_file.original_filename)}"\r
Content-Type: #{uploaded_file.content_type}\r
Expand All @@ -143,6 +149,7 @@ def build_file_part(parameter_name, uploaded_file)
EOF
end
end
module_function :build_file_part

end

Expand Down
1 change: 1 addition & 0 deletions rack-test.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Gem::Specification.new do |s|
s.date = "2015-01-09"
s.description = "Rack::Test is a small, simple testing API for Rack apps. It can be used on its\nown or as a reusable starting point for Web frameworks and testing libraries\nto build on. Most of its initial functionality is an extraction of Merb 1.0's\nrequest helpers feature."
s.email = "bryan@brynary.com"
s.license = 'MIT'
s.extra_rdoc_files = [
"README.rdoc",
"MIT-LICENSE.txt"
Expand Down
21 changes: 21 additions & 0 deletions spec/rack/test/cookie_jar_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "spec_helper"

describe Rack::Test::CookieJar do
subject(:jar) { Rack::Test::CookieJar.new }

describe "#get_cookie" do
context "with a populated jar" do
let(:cookie_value) { "foo;abc" }
let(:cookie_name) { "a_cookie_name" }

before do
jar[cookie_name] = cookie_value
end

it "returns full cookie objects" do
cookie = jar.get_cookie(cookie_name)
expect(cookie).to be_a(Rack::Test::Cookie)
end
end
end
end
66 changes: 66 additions & 0 deletions spec/rack/test/cookie_object_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require "spec_helper"

describe Rack::Test::Cookie do
subject(:cookie) { Rack::Test::Cookie.new(cookie_string) }

let(:cookie_string) { raw_cookie_string }

let(:raw_cookie_string) {
[
"cookie_name=" + CGI.escape(value),
"domain=" + domain,
"path=" + path,
"expires=" + expires,
].join("; ")
}

let(:http_only_raw_cookie_string) {
raw_cookie_string + "; HttpOnly"
}

let(:http_only_secure_raw_cookie_string) {
http_only_raw_cookie_string + "; secure"
}

let(:value) { "the cookie value" }
let(:domain) { "www.example.org" }
let(:path) { "/" }
let(:expires) { "Mon, 10 Aug 2015 14:40:57 0100" }

describe "#to_h" do
let(:cookie_string) { http_only_secure_raw_cookie_string }

it "returns the cookie value and all options" do
expect(cookie.to_h).to eq(
"value" => value,
"domain" => domain,
"path" => path,
"expires" => expires,
"HttpOnly" => true,
"secure" => true,
)
end
end

describe "#to_hash" do
it "is an alias for #to_h" do
expect(cookie.to_hash).to eq(cookie.to_h)
end
end

describe "#http_only?" do
context "for a non HTTP only cookie" do
it "returns false" do
expect(cookie.http_only?).to be(false)
end
end

context "for a HTTP only cookie" do
let(:cookie_string) { http_only_raw_cookie_string }

it "returns true" do
expect(cookie.http_only?).to be(true)
end
end
end
end
Loading