Sha256: 17f31a6082c42b7fb77ed999c962087a088b463b82af4238064c14ee925081c9

Contents?: true

Size: 1.49 KB

Versions: 9

Compression:

Stored size: 1.49 KB

Contents

require 'minitest_helper'

describe Rasti::Web::Route do

  ROUTES = [
    '/', 
    '/resource', 
    '/resource/:id/:action',
    '/:resource(/:id(/:action))'
  ]

  RESPONSE = [200, {}, []]

  def build_route(pattern)
    Rasti::Web::Route.new pattern, ->(env) { RESPONSE }
  end

  def route_for(path)
    ROUTES.map    { |r| build_route r }
          .detect { |r| r.match? path }
  end

  ['', '/'].each do |path|
    it "Root '#{path}'" do
      route = route_for path

      route.pattern.must_equal '/'
      route.extract_params(path).must_be_empty
      route.call({}).must_equal RESPONSE
    end
  end

  ['/resource', '/resource/'].each do |path|
    it "Static '#{path}'" do
      route = route_for path

      route.pattern.must_equal '/resource'
      route.extract_params(path).must_be_empty
      route.call({}).must_equal RESPONSE
    end
  end

  it 'Params' do
    path = '/resource/123/show'
    route = route_for path

    route.pattern.must_equal '/resource/:id/:action'
    route.extract_params(path).must_equal 'id' => '123', 'action' => 'show'
    route.call({}).must_equal RESPONSE
  end

  ['/other', '/other/456', '/other/456/edit'].each do |path|
    it "Optional params '#{path}'" do
      route = route_for path
      sections = path[1..-1].split('/')

      route.pattern.must_equal '/:resource(/:id(/:action))'
      route.extract_params(path).must_equal 'resource' => sections[0], 'id' => sections[1], 'action' => sections[2]
      route.call({}).must_equal RESPONSE
    end
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rasti-web-0.2.3 spec/route_spec.rb
rasti-web-0.2.2 spec/route_spec.rb
rasti-web-0.2.1 spec/route_spec.rb
rasti-web-0.2.0 spec/route_spec.rb
rasti-web-0.1.1 spec/route_spec.rb
rasti-web-0.1.0 spec/route_spec.rb
rasti-web-0.0.7 spec/route_spec.rb
rasti-web-0.0.6 spec/route_spec.rb
rasti-web-0.0.5 spec/route_spec.rb