Module: Goliath::TestHelper

Defined in:
lib/goliath/test_helper.rb,
lib/goliath/test_helper_ws.rb,
lib/goliath/test_helper_sse.rb,
lib/goliath/test_helper_streaming.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 = MultiJson.load(c.response)
        b['response'].should == 'test'
      end
    end
  end
end

Defined Under Namespace

Classes: SSEHelper, StreamingHelper, WSHelper

Constant Summary collapse

DEFAULT_ERROR =
Proc.new { fail "API request failed" }

Instance Method Summary collapse

Instance Method Details

#create_test_request(request_data) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/goliath/test_helper.rb', line 184

def create_test_request(request_data)
  domain = request_data.delete(:domain) || "localhost:#{@test_server_port}"
  path = request_data.delete(:path) || ''
  opts = request_data.delete(:connection_options) || {}

  EM::HttpRequest.new("http://#{domain}#{path}", opts)
end

#delete_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object

Make a DELETE request against the currently launched API.

Parameters:

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

    Any data to pass to the DELETE request.

  • errback (Proc) (defaults to: DEFAULT_ERROR)

    An error handler to attach

  • blk (Proc)

    The callback block to execute



169
170
171
172
# File 'lib/goliath/test_helper.rb', line 169

def delete_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
  req = create_test_request(request_data).delete(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

#get_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object

Make a GET request against the currently launched API.

Parameters:

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

    Any data to pass to the GET request.

  • errback (Proc) (defaults to: DEFAULT_ERROR)

    An error handler to attach

  • blk (Proc)

    The callback block to execute



129
130
131
132
# File 'lib/goliath/test_helper.rb', line 129

def get_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
  req = create_test_request(request_data).get(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

#head_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object

Make a HEAD request against the currently launched API.

Parameters:

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

    Any data to pass to the HEAD request.

  • errback (Proc) (defaults to: DEFAULT_ERROR)

    An error handler to attach

  • blk (Proc)

    The callback block to execute



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

def head_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
  req = create_test_request(request_data).head(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

#hookup_request_callbacks(req, errback, &blk) ⇒ Nil

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:

  • req (EM::HttpRequest)

    The HTTP request to augment

  • errback (Proc)

    An error handler to attach

  • blk (Proc)

    The callback handler to attach

Returns:

  • (Nil)


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

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

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

#options_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object

Make an OPTIONS request against the currently launched API.

Parameters:

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

    Any data to pass to the OPTIONS request.

  • errback (Proc) (defaults to: DEFAULT_ERROR)

    An error handler to attach

  • blk (Proc)

    The callback block to execute



179
180
181
182
# File 'lib/goliath/test_helper.rb', line 179

def options_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
  req = create_test_request(request_data).options(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

#patch_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object

Make a PATCH request against the currently launched API.

Parameters:

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

    Any data to pass to the PUT request.

  • errback (Proc) (defaults to: DEFAULT_ERROR)

    An error handler to attach

  • blk (Proc)

    The callback block to execute



159
160
161
162
# File 'lib/goliath/test_helper.rb', line 159

def patch_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
  req = create_test_request(request_data).patch(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

#post_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object

Make a POST request against the currently launched API.

Parameters:

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

    Any data to pass to the POST request.

  • errback (Proc) (defaults to: DEFAULT_ERROR)

    An error handler to attach

  • blk (Proc)

    The callback block to execute



139
140
141
142
# File 'lib/goliath/test_helper.rb', line 139

def post_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
  req = create_test_request(request_data).post(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

#put_request(request_data = {}, errback = DEFAULT_ERROR, &blk) ⇒ Object

Make a PUT request the currently launched API.

Parameters:

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

    Any data to pass to the PUT request.

  • errback (Proc) (defaults to: DEFAULT_ERROR)

    An error handler to attach

  • blk (Proc)

    The callback block to execute



149
150
151
152
# File 'lib/goliath/test_helper.rb', line 149

def put_request(request_data = {}, errback = DEFAULT_ERROR, &blk)
  req = create_test_request(request_data).put(request_data)
  hookup_request_callbacks(req, errback, &blk)
end

#server(api, port, options = {}, &blk) ⇒ Goliath::Server

Launches an instance of a given API server. The server will launch on the specified port.

Parameters:

  • api (Class)

    The API class to launch

  • port (Integer)

    The port to run the server on

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

    The options hash to provide to the server

Returns:

  • (Goliath::Server)

    The executed server



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/goliath/test_helper.rb', line 43

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

  s = Goliath::Server.new
  s.logger = setup_logger(options)
  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.plugins = api.plugins
  @test_server_port = s.port if blk
  s.start(&blk)
  s
end

#setup_logger(opts) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/goliath/test_helper.rb', line 59

def setup_logger(opts)
  return fake_logger if opts[:log_file].nil? && opts[:log_stdout].nil?

  log = Log4r::Logger.new('goliath')
  log_format = Log4r::PatternFormatter.new(:pattern => "[#{Process.pid}:%l] %d :: %m")
  log.level = opts[:verbose].nil? ? Log4r::INFO : Log4r::DEBUG

  if opts[:log_stdout]
    log.add(Log4r::StdoutOutputter.new('console', :formatter => log_format))
  elsif opts[:log_file]
    file = opts[:log_file]
    FileUtils.mkdir_p(File.dirname(file))

   log.add(Log4r::FileOutputter.new('fileOutput', {:filename => file,
                                                   :trunc => false,
                                                   :formatter => log_format}))
  end
  log
end

#sse_client_connect(path, &blk) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/goliath/test_helper_sse.rb', line 67

def sse_client_connect(path,&blk)
  url = "http://localhost:#{@test_server_port}#{path}"
  client = SSEHelper.new(url)
  client.with_async_http { client.connection.start }
  client.listen
  Fiber.new { blk.call(client) }.resume if blk
  stop
end

#stopNil

Stops the launched API

Returns:

  • (Nil)


82
83
84
# File 'lib/goliath/test_helper.rb', line 82

def stop
  EM.stop_event_loop
end

#streaming_client_connect(path, &blk) ⇒ Object



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

def streaming_client_connect(path, &blk)
  url = "http://localhost:#{@test_server_port}#{path}"
  client = StreamingHelper.new(url)
  blk.call(client) if blk
  stop
end

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

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:

  • api (Class)

    The API class to launch

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

    The options to pass to the server

  • blk (Proc)

    The code to execute after the server is launched.



93
94
95
# File 'lib/goliath/test_helper.rb', line 93

def with_api(api, options = {}, &blk)
  server(api, options.delete(:port) || 9900, options, &blk)
end

#ws_client_connect(path, &blk) ⇒ Object



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

def ws_client_connect(path,&blk)
  url = "ws://localhost:#{@test_server_port}#{path}"
  client = WSHelper.new(url)
  blk.call(client) if blk
  stop
end