Last active
December 21, 2015 11:19
-
-
Save dplummer/6297870 to your computer and use it in GitHub Desktop.
single file rails app and test to display json, */* returns html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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