Skip to content

Commit

Permalink
Starting some tests.
Browse files Browse the repository at this point in the history
Signed-off-by: François de Metz <fdemetz@af83.com>
  • Loading branch information
francois2metz committed Jul 28, 2011
1 parent faca7d1 commit 5963dda
Showing 6 changed files with 110 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
source :rubygems

gemspec
31 changes: 31 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
PATH
remote: .
specs:
em-eventsource (0.0.1)
em-http-request (= 1.0.0.beta4)
eventmachine (= 1.0.0.beta3)

GEM
remote: http://rubygems.org/
specs:
addressable (2.2.6)
em-http-request (1.0.0.beta.4)
addressable (>= 2.2.3)
em-socksify
eventmachine (>= 1.0.0.beta.3)
http_parser.rb (>= 0.5.1)
em-socksify (0.1.0)
eventmachine
eventmachine (1.0.0.beta.3)
http_parser.rb (0.5.1)
minitest (2.3.1)
rake (0.9.2)

PLATFORMS
ruby

DEPENDENCIES
bundler
em-eventsource!
minitest (~> 2.0)
rake
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'rake/testtask'

Rake::TestTask.new do |t|
t.pattern = "spec/*_spec.rb"
end

task :default => :test
4 changes: 3 additions & 1 deletion lib/em-eventsource.rb
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ def initialize(url, query={}, headers={})
end

def on(type, &block)
@on[type] = [] if @on[type].nil?
@on[type] << block
end

@@ -60,7 +61,7 @@ def listen
while index = stream.index("\n")
subpart = stream[0..index]
/^data: (.+)$/.match(subpart) do |m|
@block.call(m[1])
@messages.each { |message| message.call(m[1]) }
end
/^id: (.+)$/.match(subpart) do |m|
@lastid = m[1]
@@ -77,6 +78,7 @@ def prepare_request
conn = EM::HttpRequest.new(@url)
conn.get({ :query => @query,
:head => {'Last-Event-Id' => @lastid }.merge(@headers)})
conn
end
end
end
22 changes: 22 additions & 0 deletions spec/em-eventsource_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env ruby

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
require "minitest/autorun"

describe EventMachine::EventSource do
it "connect and handle message" do
EM.run do
source = EventMachine::EventSource.new("http://example.com/streaming")
source.message do |message|
message.must_be :==, 'hello world'
source.close
EM.stop
end
EM.add_timer(1) do
req = source.instance_variable_get "@req"
req.stream_data("data: hello world\n")
end
source.start
end
end
end
44 changes: 44 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require "em-eventsource"

module EventMachine
class MockHttpRequest
attr_reader :url, :get_args

def initialize(url)
@url = url
@streams = []
@errors = []
@headers = []
end

def get(*args)
@get_args = args
end

def stream(&block)
@streams << block
end

def errback(&block)
@errors << block
end

def headers(&block)
@headers << block
end

def stream_data(data)
@streams.each { |stream| stream.call(data) }
end

def close

end
end

class HttpRequest
def self.new(url)
EM::MockHttpRequest.new(url)
end
end
end

0 comments on commit 5963dda

Please sign in to comment.