Sha256: c5b1348147951bf7fb3d8881319e04a01117bc1749c5fd75d81de24f9dadb7b2

Contents?: true

Size: 1.99 KB

Versions: 15

Compression:

Stored size: 1.99 KB

Contents

module Shoulda # :nodoc:
  module ActionController # :nodoc:
    module Matchers

      # Ensures a controller responded with expected 'response' content type.
      #
      # You can pass an explicit content type such as 'application/rss+xml'
      # or its symbolic equivalent :rss
      # or a regular expression such as /rss/
      #
      # Example:
      #
      #   it { should respond_with_content_type(:xml)  }
      #   it { should respond_with_content_type(:csv)  }
      #   it { should respond_with_content_type(:atom) }
      #   it { should respond_with_content_type(:yaml) }
      #   it { should respond_with_content_type(:text) }
      #   it { should respond_with_content_type('application/rss+xml')  }
      #   it { should respond_with_content_type(/json/) }
      def respond_with_content_type(content_type)
        RespondWithContentTypeMatcher.new(content_type)
      end

      class RespondWithContentTypeMatcher # :nodoc:

        def initialize(content_type)
          @content_type = if content_type.is_a?(Symbol)
            lookup_by_extension(content_type)
          else
            content_type
          end
        end
        
        def matches?(controller)
          @controller = controller
          if @content_type.is_a?(Regexp)
            response_content_type =~ @content_type
          else
            response_content_type == @content_type
          end
        end
        
        def failure_message
          "Expected #{expectation}"
        end
        
        def negative_failure_message
          "Did not expect #{expectation}"
        end
        
        protected
        
        def response_content_type
          @controller.response.content_type
        end
        
        def lookup_by_extension(extension)
          Mime::Type.lookup_by_extension(extension.to_s).to_s
        end
        
        def expectation
          "content type to be #{@content_type}, " <<
          "but was #{response_content_type}"
        end
        
      end
      
    end
  end
end

Version data entries

15 entries across 15 versions & 7 rubygems

Version Path
Flamefork-shoulda-2.10.1 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
Flamefork-shoulda-2.10.2 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
francois-shoulda-2.10.1 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
gnip-gnip-1.1.1 test/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
technicalpickles-shoulda-2.10.0 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
thoughtbot-shoulda-2.10.0 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
thoughtbot-shoulda-2.10.1 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
thoughtbot-shoulda-2.9.2 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
shoulda-2.9.2 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
shoulda-2.10.0 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
shoulda-2.10.1 lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
gnip-0.4.2 test/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
gnip-1.0.0 test/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
gnip-1.1.1 test/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb
gnip-1.1.2 test/lib/shoulda/action_controller/matchers/respond_with_content_type_matcher.rb