Sha256: 06ceb3aed11208d44494d5927d9fb08f4fbf2514c1dd9d26ab88d6d7f1b84e06

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

require 'rack/test'

module Capybara::RackTestJson
  class Client
    include Rack::Test::Methods

    attr_accessor :app, :options

    def initialize(app, options)
      @app, @options = app, options
    end

    %w[ get post put delete ].each do |method|
      module_eval <<-DEF, __FILE__, __LINE__+1
        def #{method}(uri, params = {}, env = {}, &block)
          env = env.merge(:method => "#{method.upcase}", :params => params)
          if options[:follow_redirect]
            request_with_follow_redirect(uri, env, &block)
          else
            request(uri, env)
          end
        end
      DEF
    end

    def request_with_follow_redirect(uri, env)
      request(uri, env)
      (5+1).times do
        if last_response.redirect?
          request(last_response["Location"], env.merge(:method => "GET"))
        else
          return
        end
      end

      # 6 times redirect cause InfiniteRedirectError
      raise Capybara::InfiniteRedirectError,
      "redirected more than 5 times, check for infinite redirects."
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capybara-json-0.3.0 lib/capybara/rack_test_json/client.rb
capybara-json-0.3.0.beta2 lib/capybara/rack_test_json/client.rb
capybara-json-0.3.0.beta1 lib/capybara/rack_test_json/client.rb
capybara-json-0.2.4 lib/capybara/rack_test_json/client.rb