├── .gitignore ├── lib ├── rack-accept-default.rb └── rack │ ├── accept_default │ └── version.rb │ └── accept_default.rb ├── .travis.yml ├── CHANGELOG.md ├── Gemfile ├── spec ├── spec_helper.rb └── rack │ └── accept_default_spec.rb ├── Rakefile ├── README.md ├── LICENSE.txt └── rack-accept-default.gemspec /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | -------------------------------------------------------------------------------- /lib/rack-accept-default.rb: -------------------------------------------------------------------------------- 1 | require 'rack/accept_default' 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 1.8.7 4 | - 1.9.2 5 | - 1.9.3 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## 0.0.2 2 | 3 | - Override nil HTTP_ACCEPT as well (@r7kamura) 4 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in rack-accept-default.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /lib/rack/accept_default/version.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | module AcceptDefaultVersion 3 | VERSION = '0.0.2' 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require 'rack-accept-default' 2 | require 'rack/test' 3 | require 'rspec' 4 | 5 | RSpec.configure do |config| 6 | end 7 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | 3 | require 'rspec/core/rake_task' 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /lib/rack/accept_default.rb: -------------------------------------------------------------------------------- 1 | module Rack 2 | class AcceptDefault 3 | def initialize(app, default='*/*') 4 | @app = app 5 | @default = default 6 | end 7 | 8 | def call(env) 9 | env['HTTP_ACCEPT'] ||= @default 10 | @app.call(env) 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /spec/rack/accept_default_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | 3 | describe Rack::AcceptDefault do 4 | include Rack::Test::Methods 5 | 6 | def app 7 | rack_app = lambda { |env| [200, {"Content-Type" => "text/plain"}, [ env["HTTP_ACCEPT"] ]] } 8 | Rack::AcceptDefault.new(rack_app) 9 | end 10 | 11 | it "should set */* as a default header" do 12 | get '/' 13 | last_response.should be_ok 14 | last_response.body.should == '*/*' 15 | end 16 | 17 | it "should override given nil Accept header" do 18 | get "/", {}, { "HTTP_ACCEPT" => nil } 19 | last_response.should be_ok 20 | last_response.body.should == "*/*" 21 | end 22 | 23 | it "should not override given Accept header" do 24 | get '/', {}, { 'HTTP_ACCEPT' => "application/json" } 25 | last_response.should be_ok 26 | last_response.body.should == 'application/json' 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rack::AcceptDefault 2 | 3 | Rack::AcceptDefault is a tiny piece of middleware that sets default `Accept:` header when it isn't present, to work around Rails 3.x issues. 4 | 5 | ## Installation 6 | 7 | ### rubygems 8 | 9 | ``` 10 | gem install rack-accept-default 11 | ``` 12 | 13 | ## Usage 14 | 15 | ``` 16 | require 'rack-accept-default` 17 | use Rack::AcceptDefault 18 | ``` 19 | 20 | ## Why 21 | 22 | Rails default MIME negotiation (in ActionPack) handles requests without `Accept:` headers as *invalid*, and assume it wants the HTML view. This assumption is wrong per RFC 2616 HTTP/1.1 specification. 23 | 24 | > If no Accept header field is present, then it is assumed that the client accepts all media types 25 | 26 | Rack::AcceptDefault is a piece of Rack middleware that provides a convenient way to work around this problem, by setting the `*/*` value to the `Accept` header when it is not present. 27 | 28 | ## License 29 | 30 | MIT License. 31 | 32 | ## Copyright 33 | 34 | Copyright 2012- Tatsuhiko Miyagawa 35 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2012 Tatsuhiko Miyagawa 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /rack-accept-default.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "rack/accept_default/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "rack-accept-default" 7 | s.version = Rack::AcceptDefaultVersion::VERSION 8 | s.authors = ["Tatsuhiko Miyagawa"] 9 | s.email = ["miyagawa@bulknews.net"] 10 | s.homepage = "https://github.com/miyagawa/rack-accept-default" 11 | s.summary = "Set default value for Accept: header when it is not present" 12 | s.description = "A tiny piece of Rack middleware to set the default Accept: header" 13 | 14 | s.rubyforge_project = "rack-accept-default" 15 | 16 | s.files = `git ls-files`.split("\n") 17 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 18 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 19 | s.require_paths = ["lib"] 20 | 21 | # specify any dependencies here; for example: 22 | s.add_development_dependency "rake" 23 | s.add_development_dependency "rack-test" 24 | s.add_development_dependency "rspec" 25 | s.add_runtime_dependency "rack" 26 | end 27 | --------------------------------------------------------------------------------