Module: Goliath::TestHelper

Defined in:
lib/goliath/test_helper.rb

Overview

Methods to help with testing Goliath APIs

Examples:

describe Echo do
  include Goliath::TestHelper

  let(:err) { Proc.new { fail "API request failed" } }
  it 'returns the echo param' do
    with_api(Echo) do
      get_request({:query => {:echo => 'test'}}, err) do |c|
        b = Yajl::Parser.parse(c.response)
        b['response'].should == 'test'
      end
    end
  end
end

Instance Method Summary (collapse)

Instance Method Details

- (Object) get_request(request_data = {}, errback = nil, &blk)

Make a GET request the currently launched API.

Parameters:

  • (Hash) request_data (defaults to: {})

    Any data to pass to the GET request.

  • (Proc) errback (defaults to: nil)

    An error handler to attach

  • (Proc) blk

    The callback block to execute



95
96
97
98
99
# File 'lib/goliath/test_helper.rb', line 95

def get_request(request_data = {}, errback = nil, &blk)
  path = request_data.delete(:path) || ''
  req = EM::HttpRequest.new("http://localhost:9000#{path}").get(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

- (Nil) hookup_request_callbacks(req, errback, &blk)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Helper method to setup common callbacks for various request methods. The given err and callback handlers will be attached and a callback to stop the reactor will be added.

Parameters:

  • (EM::HttpRequest) req

    The HTTP request to augment

  • (Proc) errback

    An error handler to attach

  • (Proc) blk

    The callback handler to attach

Returns:

  • (Nil)


82
83
84
85
86
87
88
# File 'lib/goliath/test_helper.rb', line 82

def hookup_request_callbacks(req, errback, &blk)
  req.callback &blk
  req.callback { stop }

  req.errback &errback if errback
  req.errback { stop }
end

- (Object) post_request(request_data = {}, errback = nil, &blk)

Make a POST request the currently launched API.

Parameters:

  • (Hash) request_data (defaults to: {})

    Any data to pass to the POST request.

  • (Proc) errback (defaults to: nil)

    An error handler to attach

  • (Proc) blk

    The callback block to execute



106
107
108
109
110
# File 'lib/goliath/test_helper.rb', line 106

def post_request(request_data = {}, errback = nil, &blk)
  path = request_data.delete(:path) || ''
  req = EM::HttpRequest.new("http://localhost:9000#{path}").post(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

- (Goliath::Server) server(api, port = 9000, options = {})

Launches an instance of a given API server. The server will launch on the default settings of localhost port 9000.

Parameters:

  • (Class) api

    The API class to launch

  • (Integer) port (defaults to: 9000)

    The port to run the server on

  • (Hash) options (defaults to: {})

    The options hash to provide to the server

Returns:



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/goliath/test_helper.rb', line 38

def server(api, port = 9000, options = {})
  op = OptionParser.new

  s = Goliath::Server.new
  s.logger = mock('log').as_null_object
  s.api = api.new
  s.app = Goliath::Rack::Builder.build(api, s.api)
  s.api.options_parser(op, options)
  s.options = options
  s.port = port
  s.start
  s
end

- (Nil) stop

Stops the launched API

Returns:

  • (Nil)


55
56
57
# File 'lib/goliath/test_helper.rb', line 55

def stop
  EM.stop
end

- (Object) with_api(api, options = {}, &blk)

Note:

This will not return until stop is called.

Wrapper for launching API and executing given code block. This will start the EventMachine reactor running.

Parameters:

  • (Class) api

    The API class to launch

  • (Hash) options (defaults to: {})

    The options to pass to the server

  • (Proc) blk

    The code to execute after the server is launched.



66
67
68
69
70
71
# File 'lib/goliath/test_helper.rb', line 66

def with_api(api, options = {}, &blk)
  EM.synchrony do
    @api_server = server(api, 9000, options)
    blk.call
  end
end