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) build_app(klass)

Builds the rack middleware chain for the given API

Parameters:

  • (Class) klass

    The API class to build the middlewares for

Returns:

  • (Object)

    The Rack middleware chain



34
35
36
37
38
39
40
41
# File 'lib/goliath/test_helper.rb', line 34

def build_app(klass)
  ::Rack::Builder.new do
    klass.middlewares.each do |mw|
      use(*(mw[0..1].compact), &mw[2])
    end
    run klass.new
  end
end

- (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



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

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)


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

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



118
119
120
121
122
# File 'lib/goliath/test_helper.rb', line 118

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:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/goliath/test_helper.rb', line 50

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 = build_app(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)


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

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.



78
79
80
81
82
83
# File 'lib/goliath/test_helper.rb', line 78

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