rack-force-status 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,28 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-force-status (0.0.1)
5
+ rack
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.1.3)
11
+ rack (1.5.1)
12
+ rake (10.0.3)
13
+ rspec (2.12.0)
14
+ rspec-core (~> 2.12.0)
15
+ rspec-expectations (~> 2.12.0)
16
+ rspec-mocks (~> 2.12.0)
17
+ rspec-core (2.12.2)
18
+ rspec-expectations (2.12.1)
19
+ diff-lcs (~> 1.1.3)
20
+ rspec-mocks (2.12.2)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ rack-force-status!
27
+ rake
28
+ rspec
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # Rack::ForceStatus
2
+
3
+ Rack::ForceStatus is a simple Rack middleware for forcing status codes on responses. This is useful for dealing with things like [XDomainRequest's inability to provide responseText on errors](http://stackoverflow.com/questions/10390539/xdomainrequest-get-response-body-on-error). We can now return the error with a 200 response that XDomainRequest can receive and handle.
4
+
5
+ ## Installation
6
+
7
+ Install the Rack::ForceStatus gem with a simple:
8
+
9
+ ```ruby
10
+ gem install rack-force-status
11
+ ```
12
+
13
+ Or add it to your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'rack-force-status'
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ Use the Rack::ForceStatus middleware like anything else:
22
+
23
+ ```ruby
24
+ use Rack::ForceStatus
25
+ ```
26
+
27
+ Or for Rails application, add the following line to your application config file (config/application.rb for Rails 3 & 4, config/environment.rb for Rails 2):
28
+
29
+ ```ruby
30
+ config.middleware.use Rack::ForceStatus
31
+ ```
32
+
33
+ From there you can force the status on any request by passing a ```force_status``` param, set to the value of the status you'd like. For example, if you'd like everything to always return successful, you'd do:
34
+
35
+ ```
36
+ http://myapp.com/some/path?force_status=200
37
+ ```
38
+
39
+ In addition to forcing the status to whatever you've specified, if the original status was different you will get a header added telling you what it was originally.
40
+
41
+ ```
42
+ X-Original-Status-Code: 422
43
+ ```
44
+
45
+ ## Options
46
+
47
+ If you'd like to customize the incoming param name or the outgoing header name, you can pass them as options.
48
+
49
+ ```ruby
50
+ config.middleware.use Rack::ForceStatus, :param => 'my-custom-param', :header => 'My-Custom-Header'
51
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rake'
5
+ require 'rspec/core/rake_task'
6
+
7
+ desc "Run all examples"
8
+ RSpec::Core::RakeTask.new(:spec) do |t|
9
+ t.rspec_opts = %w[--color]
10
+ end
11
+
12
+ task :default => [:spec]
@@ -0,0 +1 @@
1
+ require 'rack/force-status'
@@ -0,0 +1 @@
1
+ require 'rack/force-status/middleware'
@@ -0,0 +1,23 @@
1
+ module Rack
2
+ class ForceStatus
3
+ def initialize(app, options={})
4
+ @app = app
5
+ @param = options[:param] || 'force_status'
6
+ @header = options[:header] || 'X-Original-Status-Code'
7
+ end
8
+
9
+ def call(env)
10
+ request = Rack::Request.new(env)
11
+ force_status = request.params.delete(@param).to_i
12
+
13
+ status, headers, body = @app.call(env)
14
+
15
+ if force_status > 0 && status != force_status
16
+ headers[@header] = status.to_s
17
+ status = force_status
18
+ end
19
+
20
+ [status, headers, body]
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class ForceStatus
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack/force-status/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-force-status"
7
+ s.version = Rack::ForceStatus::VERSION
8
+ s.authors = ["Big Cartel"]
9
+ s.email = "dev@bigcartel.com"
10
+ s.homepage = "https://github.com/bigcartel/rack-force-status"
11
+ s.summary = "Rack middleware for forcing status codes on responses."
12
+ s.description = "Rack middleware for forcing status codes on responses."
13
+
14
+ s.files = `git ls-files`.split($\)
15
+ s.platform = Gem::Platform::RUBY
16
+ s.require_path = 'lib'
17
+
18
+ s.add_dependency('rack')
19
+ s.add_development_dependency('rake')
20
+ s.add_development_dependency('rspec')
21
+ end
@@ -0,0 +1,62 @@
1
+ require 'spec_helper'
2
+
3
+ describe Rack::ForceStatus do
4
+ let(:body) { '<html><body><h1>Hi</h1></body></html>' }
5
+ let(:original_status) { 422 }
6
+ let(:custom_param) { nil }
7
+ let(:custom_header) { nil }
8
+ let(:options) { { :param => custom_param, :header => custom_header }}
9
+ let(:app) { lambda { |env| [original_status, {}, [body]] } }
10
+ let(:request) { Rack::MockRequest.env_for("/", :params => "foo=bar&#{ custom_param || 'force_status' }=#{ forced_status }") }
11
+ let(:response) { Rack::ForceStatus.new(app, options).call(request) }
12
+
13
+ describe "when we want to force the status" do
14
+ let(:forced_status) { 200 }
15
+
16
+ it "should force the status code" do
17
+ response[0].should == forced_status
18
+ end
19
+
20
+ it "should add a header with the original status code" do
21
+ response[1]['X-Original-Status-Code'].should == original_status.to_s
22
+ end
23
+
24
+ it "shouldn't effect the body" do
25
+ response[2].should == [body]
26
+ end
27
+ end
28
+
29
+ describe "when we've customized the middleware" do
30
+ let(:forced_status) { 200 }
31
+ let(:custom_param) { 'my-param' }
32
+ let(:custom_header) { 'my-header' }
33
+
34
+ it "should force the status code" do
35
+ response[0].should == forced_status
36
+ end
37
+
38
+ it "should add a header with the original status code" do
39
+ response[1][custom_header].should == original_status.to_s
40
+ end
41
+
42
+ it "shouldn't effect the body" do
43
+ response[2].should == [body]
44
+ end
45
+ end
46
+
47
+ describe "when we don't want to force the status" do
48
+ let(:forced_status) { nil }
49
+
50
+ it "should maintain the original code" do
51
+ response[0].should == original_status
52
+ end
53
+
54
+ it "should not add a header with the original status code" do
55
+ response[1]['X-Original-Status-Code'].should be_nil
56
+ end
57
+
58
+ it "shouldn't effect the body" do
59
+ response[2].should == [body]
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,2 @@
1
+ require 'rack'
2
+ require 'rack-force-status'
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-force-status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Big Cartel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Rack middleware for forcing status codes on responses.
63
+ email: dev@bigcartel.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - README.md
71
+ - Rakefile
72
+ - lib/rack-force-status.rb
73
+ - lib/rack/force-status.rb
74
+ - lib/rack/force-status/middleware.rb
75
+ - lib/rack/force-status/version.rb
76
+ - rack-force-status.gemspec
77
+ - spec/rack_force_status_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: https://github.com/bigcartel/rack-force-status
80
+ licenses: []
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 1.8.24
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: Rack middleware for forcing status codes on responses.
103
+ test_files: []