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

cc @dj2 @igrigorik @joshbuddy - any ideas on how to accomplish the blockless syntax?

@dj2
Copy link
Copy Markdown

dj2 commented Mar 26, 2012

I like the first set of changes, they seem a lot cleaner, can you send a pull request that integrates that into the TestHelper?

In terms of getting rid of the blocks, I don't think that will be possible. The block is passed through to the server and executed inside the EM::Synchrony.run block. If you don't pass the block I have a feeling that your tests would hang as there would be no way to stop the api spawned in the with_api call.

@dj2
Copy link
Copy Markdown

dj2 commented Mar 26, 2012

As a side note, I didn't seem to get any notification about your @dj2, so I'm not sure if that works in gist's. Just happened to notice this on twitter.

@toolmantim
Copy link
Copy Markdown
Author

Ah, damn about the notifications. I had thought maybe that had been the case.

Cool, I can do a pull request, but first let me have a stab at the block-less syntax, passing another block that saves the return value somewhere, and then return it after the EM.stop happens.

@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