Skip to content

Instantly share code, notes, and snippets.

@toolmantim
Created March 25, 2012 18:57
Show Gist options
  • Select an option

  • Save toolmantim/2199021 to your computer and use it in GitHub Desktop.

Select an option

Save toolmantim/2199021 to your computer and use it in GitHub Desktop.
Simpler test helpers for Goliath
# Additional methods for Goliath::TestHelper to make simple Goliath testing
# simple. You might need the other syntax for testing gnarly stuff, but I've
# yet to need it. To use, just include this in your spec_helper.rb.
#
# Your spec must respond to #api and return the Goliath API class to test.
#
# @example
# describe MyAPI do
# let(:api) { MyAPI }
# describe "GET /" do
# subject { get("/") }
# its("response_header.status") { should == 200 }
# end
# end
#
# It's just the same if you're using #before instead of #subject:
#
# @example
# describe MyAPI do
# let(:api) { MyAPI }
# describe "GET /" do
# before do
# @client = get("/")
# end
# it "has a 200 response" do
# @client.response_header.status.should == 200
# end
# end
# end
#
# If you need to execute within the EM reactor use the block syntax:
#
# @example
# describe MyAPI do
# let(:api) { MyAPI }
# describe "GET /" do
# get("/") do |client|
# @client.response_header.status.should == 200
# end
# end
# end
module Goliath::TestHelper
def get path, opts={}, &callback
server_request :get, path, opts, &callback
end
def post path, opts={}, &callback
server_request :post, path, opts, &callback
end
def put path, opts={}, &callback
server_request :put, path, opts, &callback
end
def delete path, opts={}, &callback
server_request :delete, path, opts, &callback
end
def server_request method, path, opts={}, &callback
req = nil
with_api(api) do
req = EM::HttpRequest.new("http://localhost:#{@test_server_port}#{path}").__send__(method, opts)
req.callback &callback
req.callback { stop }
req.errback { stop }
req
end
req
end
end
@toolmantim
Copy link
Copy Markdown
Author

Just pushed a new version above that supports the block and block-less syntax…

@dj2
Copy link
Copy Markdown

dj2 commented Mar 27, 2012

Ah, nice. Yea if you don't need to use the reactor that should work well.

@igrigorik
Copy link
Copy Markdown

Hey guys, sorry, running way behind here.. @toolmantim that looks pretty nice. Could you convert this into a pull on the repo, and we'll continue there?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment