Sha256: 5952cf9bbd3697dfd7a2068887b8ab174461949c526618730284a360a206abf0

Contents?: true

Size: 1.64 KB

Versions: 2

Compression:

Stored size: 1.64 KB

Contents

require 'spec_helper'
require 'spec/support/rack_test'
require 'routemaster/receiver'

describe Routemaster::Receiver do
  let(:handler) { double 'handler', on_events: nil }
  let(:app) { described_class.new(fake_app, options) }
  
  
  def perform
    post '/events', payload, 'CONTENT_TYPE' => 'application/json'
  end
  
  let(:options) do
    {
      path:     '/events',
      uuid:     'demo',
      handler:  handler
    }
  end

  class FakeApp
    def call(env)
      [501, {}, 'fake app']
    end
  end

  let(:fake_app) { FakeApp.new }
  
  let(:payload) do
    [{
      topic: 'widgets', event: 'created', url: 'https://example.com/widgets/1', t: 1234
    }, {
      topic: 'widgets', event: 'created', url: 'https://example.com/widgets/2', t: 1234 
    }, {
      topic: 'widgets', event: 'created', url: 'https://example.com/widgets/3', t: 1234 
    }].to_json
  end


  it 'passes with valid HTTP Basic' do
    authorize 'demo', 'x'
    perform
    expect(last_response.status).to eq(204)
  end

  it 'fails without authentication' do
    perform
    expect(last_response.status).to eq(401)
  end

  it 'delegates to the next middleware for unknown paths' do
    post '/foobar'
    expect(last_response.status).to eq(501)
  end

  it 'delegates to the next middlex for non-POST' do
    get '/events'
    expect(last_response.status).to eq(501)
  end

  it 'calls the handler when receiving an avent' do
    authorize 'demo', 'x'
    expect(handler).to receive(:on_events).exactly(:once)
    perform
  end

  it 'calls the handler multiple times' do
    authorize 'demo', 'x'
    expect(handler).to receive(:on_events).exactly(3).times
    3.times { perform }
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
routemaster-client-0.0.2 spec/receiver_spec.rb
routemaster-client-0.0.1 spec/receiver_spec.rb