Skip to content

Instantly share code, notes, and snippets.

@dplummer
Last active December 21, 2015 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dplummer/6297870 to your computer and use it in GitHub Desktop.
Save dplummer/6297870 to your computer and use it in GitHub Desktop.
single file rails app and test to display json, */* returns html
require 'rails'
require 'action_controller/railtie'
require 'rspec'
require 'rack/test'
class JsonTestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: '****************************************'
config.secret_token = '****************************************'
config.secret_key_base = 'yup'
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
get '/' => 'dummy#index'
end
end
class DummyController < ActionController::Base
def index
render text: 'Home'
end
end
ENV['RAILS_ENV'] = 'test'
RSpec.configure do |conf|
conf.include Rack::Test::Methods
end
describe "json accepts" do
let(:app) { Rails.application }
it "returns html by default" do
get '/'
last_response.body.should == "Home"
end
it "returns json with a */* in the accept" do
get '/', {}, {'HTTP_ACCEPT' => "application/json, text/javascript, */*; q=0.01"}
last_response.headers['Content-Type'].should include("application/json")
end
it "returns json if */* isn't specified" do
get '/', {}, {'HTTP_ACCEPT' => "application/json"}
last_response.headers['Content-Type'].should include("application/json")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment