Sha256: 45d441874878f83abe4f9f31ae716e28221d3504f15320de2666ebff2025f338

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require 'spec_helper'
require 'commute/common/eventmachine'

describe Commute::Common::Eventmachine do

  let(:adapter) { Commute::Common::Eventmachine.new }

  let(:router) { Commute::Stack::Router.new [adapter].each }

  before do
    @stub = stub_request(:post, "http://www.example.com/").with \
      headers: {
        'Accept-Encoding' => 'gzip, deflate'
      },
      body: 'Hello World!'

    @done = proc {}
  end

  it 'should be able to make a successful request' do
    @done.expects(:call).twice
    EM.run do
      # Stub the request.
      @stub.to_return(status: 200, body: 'Hello!', headers: {})

      # Create a Http Request.
      http_request = Commute::Http::Request.new(nil).tap do |r|
        r.uri = URI('http://www.example.com')
        r.method = :post
      end

      # Execute the request.
      request = router.call http_request do |response, status|
        @done.call
        status.success?.must_equal true
        response.buffer.on(:data) do |body|
          @done.call
          body.join.must_equal 'Hello!'
        end

        EM.stop
      end
      request.write 'Hello'
      request.write ' World!'
      request.end
    end
  end

it 'should be able to handle a bad response' do
  @done.expects(:call).once
    EM.run do
      # Stub the request.
      @stub.to_return(status: 500, headers: {})

      # Create a Http Request.
      http_request = Commute::Http::Request.new(nil).tap do |r|
        r.uri = URI('http://www.example.com')
        r.method = :post
      end

      # Execute the request.
      request = router.call http_request do |response, status|
        @done.call
        status.fail?.must_equal true
        response.on(:data) { @done.call }

        EM.stop
      end
      request.write 'Hello'
      request.write ' World!'
      request.end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
commute-0.3.0.pre.2 spec/commute/common/eventmachine_spec.rb
commute-0.3.0.pre spec/commute/common/eventmachine_spec.rb