Permalink
Browse files

Ensure POST/PUT body inputs are always encoded properly

Fixes #44 and LintErrors. Remove deprecated rspec matchers.
  • Loading branch information...
1 parent ef7ac66 commit ba98fe97c07019ef6ef108eef4b995ed23edba99 @mwunsch committed Jan 15, 2014
@@ -15,14 +15,15 @@ def initialize(app, type = FORM_URL_ENCODED)
end
def call(env)
+ size = length(env['rack.input'])
env.update CONTENT_TYPE => @type
- env.update CONTENT_LENGTH => length(env['rack.input'])
+ env.update CONTENT_LENGTH => size.to_s unless size.nil? or size.zero?
@app.call(env)
end
def length(input)
- input.size.to_s
+ input.respond_to?(:size) ? input.size : 0
end
end
end
-end
+end
View
@@ -71,7 +71,7 @@ def params(parameters=nil)
if !parameters.nil?
@body = query_params_from_hash(parameters)
if ["POST", "PUT"].include? method
- body StringIO.new(@body)
+ body stringio_encode(@body)
use Weary::Middleware::ContentType
else
uri.query = @body
@@ -156,4 +156,4 @@ def stringio_encode(content)
end
end
-end
+end
@@ -27,7 +27,7 @@
'rack.run_once' => false
}
lint = Rack::Lint.new subject
- expect { lint.call(rack_defaults.update(env)) }.to_not raise_error Rack::Lint::LintError
+ expect { lint.call(rack_defaults.update(env)) }.not_to raise_error
end
describe "the status" do
@@ -6,6 +6,7 @@
before do
@url = "http://github.com/api/v2/json/repos/show/mwunsch/weary"
@request = Weary::Request.new @url, 'POST'
+ @request.params apikey: "secret"
stub_request :post, @request.uri.to_s
end
View
@@ -209,13 +209,27 @@
end
describe "#call" do
- it_behaves_like "a Rack application" do
- subject {
- described_class.new "http://github.com/api/v2/json/repos/show/mwunsch/weary" do |req|
- req.adapter Class.new { include Weary::Adapter }
- end
- }
- let(:env) { subject.env }
+ context "on a GET request" do
+ it_behaves_like "a Rack application" do
+ subject {
+ described_class.new "http://github.com/api/v2/json/repos/show/mwunsch/weary" do |req|
+ req.adapter Class.new { include Weary::Adapter }
+ end
+ }
+ let(:env) { subject.env }
+ end
+ end
+
+ context "on a POST request with params" do
+ it_behaves_like "a Rack application" do
+ subject {
+ described_class.new "https://api.github.com/gists", :POST do |req|
+ req.params files: { "file1.txt" => { content: "String file contents" } }
+ req.adapter Class.new { include Weary::Adapter }
+ end
+ }
+ let(:env) { subject.env }
+ end
end
end
-end
+end

0 comments on commit ba98fe9

Please sign in to comment.