Sha256: f5f8426a12e8c652b9e88913b634251fc4ee6af62001920b0a7922597d815f66

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

require File.expand_path("spec_helper", File.dirname(File.dirname(__FILE__)))

describe "hooks plugin" do 
  before do
    a = @a = []
    app(:bare) do
      plugin :hooks

      before do
        response['foo'] = 'bar'
      end

      after do |r|
        if r
          a << [r[0], r[1]['foo'], r[2]]
          r[0] += 1
        else
          a << r
        end
      end

      route do |r|
        f = response['foo']
        response['foo'] = 'baz'
        f
      end
    end
  end

  it "adds before and after hooks for running code before and after requests" do
    s, h, b = req
    s.should == 201
    h['foo'].should == 'baz'
    b.join.should == 'bar'
    @a.should == [[200, 'baz', ['bar']]]
  end

  it "multiple plugin calls do not override existing hooks" do
    app.plugin :hooks
    s, h, b = req
    s.should == 201
    h['foo'].should == 'baz'
    b.join.should == 'bar'
    @a.should == [[200, 'baz', ['bar']]]
  end

  it "after hooks are still called if an exception is raised" do
    a = @a
    @app.before do
      raise Roda::RodaError, "foo"
    end

    @app.after do |r|
      a << r
      a << $!
    end

    proc{req}.should raise_error(Roda::RodaError)
    a.pop.should be_a_kind_of(Roda::RodaError)
    a.pop.should == nil
  end

  it "handles multiple before and after blocks correctly" do
    a = @a
    @app.before do
      response['bar'] = "foo#{response['foo']}"
    end

    @app.after do |r|
      a << r[1]['bar']
      r[0] *= 2
    end

    s, h, b = req
    s.should == 402
    h['foo'].should == 'baz'
    h['bar'].should == 'foo'
    b.join.should == 'bar'
    a.should == [[200, 'baz', ['bar']], 'foo']
  end

  it "copies before and after blocks when subclassing" do
    @app = Class.new(@app)
    @app.route do |r|
      r.on do
        "foo"
      end
    end
    s, h, b = req
    s.should == 201
    h['foo'].should == 'bar'
    b.join.should == 'foo'
    @a.should == [[200, 'bar', ['foo']]]
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
roda-2.2.0 spec/plugin/hooks_spec.rb
roda-2.1.0 spec/plugin/hooks_spec.rb
roda-2.0.0 spec/plugin/hooks_spec.rb
roda-1.3.0 spec/plugin/hooks_spec.rb
roda-1.2.0 spec/plugin/hooks_spec.rb