-
-
Save janv/1647954 to your computer and use it in GitHub Desktop.
RSpec matcher for parsing `response.headers['Content-Type']`
This file contains hidden or 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
RSpec::Matchers.define :have_content_type do |content_type| | |
CONTENT_HEADER_MATCHER = /^(.*?)(?:; charset=(.*))?$/ | |
chain :with_charset do |charset| | |
@charset = charset | |
end | |
match do |response| | |
_, content, charset = *content_type_header.match(CONTENT_HEADER_MATCHER).to_a | |
if @charset | |
@charset == charset && content == content_type | |
else | |
content == content_type | |
end | |
end | |
failure_message_for_should do |response| | |
if @charset | |
"Content type #{content_type_header.inspect} should match #{content_type.inspect} with charset #{@charset}" | |
else | |
"Content type #{content_type_header.inspect} should match #{content_type.inspect}" | |
end | |
end | |
failure_message_for_should_not do |model| | |
if @charset | |
"Content type #{content_type_header.inspect} should not match #{content_type.inspect} with charset #{@charset}" | |
else | |
"Content type #{content_type_header.inspect} should not match #{content_type.inspect}" | |
end | |
end | |
def content_type_header | |
response.headers['Content-Type'] | |
end | |
end |
This file contains hidden or 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 'spec_helper' | |
class MyController < ApplicationController | |
respond_to :json, :only => :json | |
def json | |
respond_with(:json => 'rocks') | |
end | |
end | |
MyApp::Application.routes.draw do | |
match '/my_controller/json' => 'api_actions#json' | |
end | |
describe MyController do | |
describe 'GET #json' do | |
it 'responds with application/json' do | |
get :json, :format => :json | |
response.should have_content_type('application/json') | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment