Sha256: f21bdcf35000d030fb1763fedb7b81384bdd09f27c9f5682239e42133fcef649

Contents?: true

Size: 1.87 KB

Versions: 4

Compression:

Stored size: 1.87 KB

Contents

require_relative "../../test_helper"

module Unit
  module Urb
    class TestMiddleware < Minitest::Test

      describe URB::Middleware do
        before do
          @key = "123abc"
          @path = "/foo/bar?very=long&query=string&foo[]=bar&foo[]=baz"
          @value = "#{URB::PREFIX}#{@path}"
        end

        describe "when value not stored yet" do
          it "stores the passed path, redirects to the 'key URL' and alters the request parameters" do
            URB.instance_variable_set :@paths, {}
            URB.expects(:generate_key).returns @key

            get "/"
            assert_equal "{}", last_response.body

            post URB::PATH, {:path => @path}
            assert_equal 302, last_response.status
            follow_redirect!

            assert_equal({
              "very" => "long", "query" => "string", "foo" => %w(bar baz)
            }.inspect, last_response.body)

            assert_equal({
              @key => @path,
              @value => @key
            }, URB.instance_variable_get(:@paths))
          end
        end

        describe "when value already stored" do
          it "alters the request parameters" do
            URB.instance_variable_set :@paths, {
              @key => @path,
              @value => @key
            }
            URB.expects(:generate_key).never

            get "#{URB::PATH}#{@key}"
            assert_equal({
              "very" => "long", "query" => "string", "foo" => %w(bar baz)
            }.inspect, last_response.body)

            assert_equal({
              @key => @path,
              @value => @key
            }, URB.instance_variable_get(:@paths))
          end
        end
      end

    end
  end
end

def app
  Rack::Builder.app do
    use URB::Middleware
    run lambda { |env|
      params = Rack::Request.new(env).params
      [200, {"Content-Type" => "text/plain"}, [params.inspect]]
    }
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
urb-0.1.3 test/unit/urb/test_middleware.rb
urb-0.1.2 test/unit/urb/test_middleware.rb
urb-0.1.1 test/unit/urb/test_middleware.rb
urb-0.1.0 test/unit/urb/test_middleware.rb