rack_respond_to_malformed_formats 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,10 +11,11 @@ module Rack
11
11
  @app = app
12
12
  end
13
13
 
14
- def call(env)
14
+ def call(env)
15
15
  if malformed_response = respond_to_malformed_parameters(env)
16
16
  input = env["rack.input"]
17
17
  input.rewind
18
+ logger = env["rack.logger"]
18
19
  logger.error "Error occurred while parsing request.\nInput:\n\n#{input.read}" if logger
19
20
  malformed_response
20
21
  else
@@ -29,11 +30,11 @@ module Rack
29
30
 
30
31
  case (env["HTTP_X_POST_DATA_FORMAT"] || request.content_type).to_s.downcase
31
32
  when /xml/
32
- parse_xml(request.body.read)
33
+ parse_xml(request.body.read).tap { request.body.rewind }
33
34
  when /yaml/
34
- parse_yaml(request.body.read)
35
+ parse_yaml(request.body.read).tap { request.body.rewind }
35
36
  when /json/
36
- parse_json(request.body.read)
37
+ parse_json(request.body.read).tap { request.body.rewind }
37
38
  else
38
39
  false
39
40
  end
@@ -63,9 +64,5 @@ module Rack
63
64
  rescue ArgumentError => e
64
65
  [400, {"Content-Type"=> "application/x-yaml"}, [e.to_s]]
65
66
  end
66
-
67
- def logger
68
- defined?(Rails.logger) ? Rails.logger : nil
69
- end
70
67
  end
71
68
  end
@@ -3,7 +3,7 @@ $:.push File.expand_path("../lib", __FILE__)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "rack_respond_to_malformed_formats"
6
- s.version = "0.0.2"
6
+ s.version = "0.0.3"
7
7
  s.platform = Gem::Platform::RUBY
8
8
  s.authors = ["Case Commons, LLC"]
9
9
  s.email = ["casecommons-dev@googlegroups.com"]
@@ -1,14 +1,35 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), "spec_helper"))
2
2
 
3
3
  describe Rack::RespondToMalformedFormats do
4
+ def wrapped_app(app)
5
+ Rack::Builder.new do
6
+ use Rack::Lint
7
+ use Rack::RespondToMalformedFormats
8
+ use Rack::Lint
9
+ run app
10
+ end
11
+ end
12
+
13
+ def response_for_env(env)
14
+ StringIO.new("Got: #{env['rack.input'].read}")
15
+ end
16
+
17
+ def read(body)
18
+ "".tap do |body_string|
19
+ body.each do |body_part|
20
+ body_string << body_part
21
+ end
22
+ end
23
+ end
24
+
4
25
  context "when the format is HTML" do
5
26
  it "should do nothing" do
6
27
  test_input = '<html></html>'
7
- app = lambda { |env| [200, {"CONTEN_TYPE" => "text/html"}, test_input] }
8
- request = Rack::MockRequest.env_for("/", :params => "", "CONTENT_TYPE" => "test/html", :input => test_input)
9
- body = Rack::RespondToMalformedFormats.new(app).call(request).last
28
+ app = lambda { |env| [200, {"Content-Type" => "text/html"}, response_for_env(env)] }
29
+ request = Rack::MockRequest.env_for("/", :params => "", "CONTENT_TYPE" => "text/html", :input => test_input)
30
+ body = wrapped_app(app).call(request).last
10
31
 
11
- body.should == test_input
32
+ read(body).should == 'Got: <html></html>'
12
33
  end
13
34
  end
14
35
 
@@ -16,24 +37,24 @@ describe Rack::RespondToMalformedFormats do
16
37
  context "and it is valid" do
17
38
  it "should do nothing" do
18
39
  test_input = '{"foo":"bar"}'
19
- app = lambda { |env| [200, {"CONTENT_TYPE" => "application/json"}, test_input] }
40
+ app = lambda { |env| [200, {"Content-Type" => "application/json"}, response_for_env(env)] }
20
41
  request = Rack::MockRequest.env_for("/", :params => "", "CONTENT_TYPE" => "application/json", :input => test_input)
21
- body = Rack::RespondToMalformedFormats.new(app).call(request).last
42
+ body = wrapped_app(app).call(request).last
22
43
 
23
- body.should == test_input
44
+ read(body).should == 'Got: {"foo":"bar"}'
24
45
  end
25
46
  end
26
47
 
27
48
  context "and it is invalid" do
28
49
  it "should return a 400 with a message" do
29
50
  test_input = '{"foo":'
30
- app = lambda { |env| [200, {"CONTENT_TYPE" => "application/json"}, test_input] }
51
+ app = lambda { |env| [200, {"Content-Type" => "application/json"}, response_for_env(env)] }
31
52
  request = Rack::MockRequest.env_for("/", :params => "", "CONTENT_TYPE" => "application/json", :input => test_input)
32
- response = Rack::RespondToMalformedFormats.new(app).call(request)
53
+ response = wrapped_app(app).call(request)
33
54
 
34
55
  response[0].should == 400
35
56
  response[1]["Content-Type"].should == "application/json"
36
- response[2].first.should =~ /.*error.*unexpected token.*/
57
+ read(response.last).should =~ /.*error.*unexpected token.*/
37
58
  end
38
59
  end
39
60
  end
@@ -42,24 +63,24 @@ describe Rack::RespondToMalformedFormats do
42
63
  context "and it is valid" do
43
64
  it "should do nothing" do
44
65
  test_input = '<?xml version="1.0"?><foo>bar</foo>'
45
- app = lambda { |env| [200, {"CONTENT_TYPE" => "application/xml"}, test_input] }
66
+ app = lambda { |env| [200, {"Content-Type" => "application/xml"}, response_for_env(env)] }
46
67
  request = Rack::MockRequest.env_for("/", :params => "", "CONTENT_TYPE" => "application/xml", :input => test_input)
47
- body = Rack::RespondToMalformedFormats.new(app).call(request).last
68
+ body = wrapped_app(app).call(request).last
48
69
 
49
- body.should == test_input
70
+ read(body).should == 'Got: <?xml version="1.0"?><foo>bar</foo>'
50
71
  end
51
72
  end
52
73
 
53
74
  context "and it is invalid" do
54
75
  it "should return a 400 with a message" do
55
76
  test_input = '<ml><foo>bar'
56
- app = lambda { |env| [200, {"CONTENT_TYPE" => "application/xml"}, test_input] }
77
+ app = lambda { |env| [200, {"Content-Type" => "application/xml"}, response_for_env(env)] }
57
78
  request = Rack::MockRequest.env_for("/", :params => "", "CONTENT_TYPE" => "application/xml", :input => test_input)
58
- response = Rack::RespondToMalformedFormats.new(app).call(request)
79
+ response = wrapped_app(app).call(request)
59
80
 
60
81
  response[0].should == 400
61
82
  response[1]["Content-Type"].should == "application/xml"
62
- response[2].first.should =~ /.*<error>Premature.*<\/error>.*/
83
+ read(response.last).should =~ /.*<error>Premature.*<\/error>.*/
63
84
  end
64
85
  end
65
86
  end
@@ -68,26 +89,25 @@ describe Rack::RespondToMalformedFormats do
68
89
  context "and it is valid" do
69
90
  it "should do nothing" do
70
91
  test_input = "--- \nfoo: bar\n"
71
- app = lambda { |env| [200, {"CONTENT_TYPE" => "application/x-yaml"}, test_input] }
92
+ app = lambda { |env| [200, {"Content-Type" => "application/x-yaml"}, response_for_env(env)] }
72
93
  request = Rack::MockRequest.env_for("/", :params => "", "CONTENT_TYPE" => "application/x-yaml", :input => test_input)
73
- body = Rack::RespondToMalformedFormats.new(app).call(request).last
94
+ body = wrapped_app(app).call(request).last
74
95
 
75
- body.should == test_input
96
+ read(body).should == "Got: --- \nfoo: bar\n"
76
97
  end
77
98
  end
78
99
 
79
100
  context "and it is invalid" do
80
101
  it "should return a 400 with a message" do
81
102
  test_input = "--- what:\nawagasd"
82
- app = lambda { |env| [200, {"CONTENT_TYPE" => "application/x-yaml"}, test_input] }
103
+ app = lambda { |env| [200, {"Content-Type" => "application/x-yaml"}, response_for_env(env)] }
83
104
  request = Rack::MockRequest.env_for("/", :params => "", "CONTENT_TYPE" => "application/x-yaml", :input => test_input)
84
- response = Rack::RespondToMalformedFormats.new(app).call(request)
105
+ response = wrapped_app(app).call(request)
85
106
 
86
107
  response[0].should == 400
87
108
  response[1]["Content-Type"].should == "application/x-yaml"
88
- response[2].first.should =~ /.*syntax error.*/
109
+ read(response.last).should =~ /.*syntax error.*/
89
110
  end
90
111
  end
91
112
  end
92
113
  end
93
-
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack_respond_to_malformed_formats
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 2
9
- version: 0.0.2
4
+ prerelease:
5
+ version: 0.0.3
10
6
  platform: ruby
11
7
  authors:
12
8
  - Case Commons, LLC
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2011-03-18 00:00:00 -04:00
13
+ date: 2011-04-01 00:00:00 -04:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -25,10 +21,6 @@ dependencies:
25
21
  requirements:
26
22
  - - ">="
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 1
30
- - 4
31
- - 4
32
24
  version: 1.4.4
33
25
  type: :runtime
34
26
  version_requirements: *id001
@@ -66,21 +58,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
58
  requirements:
67
59
  - - ">="
68
60
  - !ruby/object:Gem::Version
69
- segments:
70
- - 0
71
61
  version: "0"
72
62
  required_rubygems_version: !ruby/object:Gem::Requirement
73
63
  none: false
74
64
  requirements:
75
65
  - - ">="
76
66
  - !ruby/object:Gem::Version
77
- segments:
78
- - 0
79
67
  version: "0"
80
68
  requirements: []
81
69
 
82
70
  rubyforge_project:
83
- rubygems_version: 1.3.7
71
+ rubygems_version: 1.5.2
84
72
  signing_key:
85
73
  specification_version: 3
86
74
  summary: Intercept malformed XML and JSON requests and return XML and JSON 400 responses not HTML 500