Sha256: a3e259aa369dde595788b1f8f0989ae3a41959eefd75b250a197ca9840a3ed14

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'rspec/webservice_matchers/util'

module RSpec
  module WebserviceMatchers
    module RedirectPermanentlyTo
      # Do we get a 301 to the place we intend?
      RSpec::Matchers.define :redirect_permanently_to do |expected|
        include RSpec
        exception = status = actual_location = nil

        match do |url_or_domain_name|
          begin
            status, headers = Util.head(url_or_domain_name)
            actual_location = headers['location']

            Util.permanent_redirect?(status) &&
              expected_location?(expected, actual_location)
          rescue Exception => e
            exception = e
            false
          end
        end

        failure_message do
          return Util.error_message(exception) if exception

          errors = []
          if Util.temp_redirect? status
            errors << "received a temporary redirect, status #{status}"
          end
          unless expected_location?(expected, actual_location)
            errors << "received location #{actual_location}"
          end
          unless Util.redirect? status
            errors << "not a redirect: received status #{status}"
          end
          Util.error_message(errors)
        end

        def expected_location?(expected, actual)
          actual =~ %r{#{expected}/?}
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-webservice_matchers-4.0.2 lib/rspec/webservice_matchers/redirect_permanently_to.rb