Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 69 lines (58 sloc) 1.608 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
require File.dirname(__FILE__) + '/helper'

describe 'Sinatra::Base' do
  it 'includes Rack::Utils' do
    assert Sinatra::Base.included_modules.include?(Rack::Utils)
  end

  it 'can be used as a Rack application' do
    mock_app {
      get '/' do
        'Hello World'
      end
    }
    assert @app.respond_to?(:call)

    request = Rack::MockRequest.new(@app)
    response = request.get('/')
    assert response.ok?
    assert_equal 'Hello World', response.body
  end

  it 'can be used as Rack middleware' do
    app = lambda { |env| [200, {}, ['Goodbye World']] }
    mock_middleware =
      mock_app {
        get '/' do
          'Hello World'
        end
        get '/goodbye' do
          @app.call(request.env)
        end
      }
    middleware = mock_middleware.new(app)
    assert_same app, middleware.app

    request = Rack::MockRequest.new(middleware)
    response = request.get('/')
    assert response.ok?
    assert_equal 'Hello World', response.body

    response = request.get('/goodbye')
    assert response.ok?
    assert_equal 'Goodbye World', response.body
  end

  it 'can take multiple definitions of a route' do
    app = mock_app {
      user_agent(/Foo/)
      get '/foo' do
        'foo'
      end

      get '/foo' do
        'not foo'
      end
    }

    request = Rack::MockRequest.new(app)
    response = request.get('/foo', 'HTTP_USER_AGENT' => 'Foo')
    assert response.ok?
    assert_equal 'foo', response.body

    request = Rack::MockRequest.new(app)
    response = request.get('/foo')
    assert response.ok?
    assert_equal 'not foo', response.body
  end
end
Something went wrong with that request. Please try again.