Permalink
Browse files

Starting some tests.

Signed-off-by: François de Metz <fdemetz@af83.com>
  • Loading branch information...
1 parent faca7d1 commit 5963ddabc51215609ba746c6ae1d0bf9c6c72bdc @francois2metz francois2metz committed Jul 28, 2011
Showing with 110 additions and 1 deletion.
  1. +3 −0 Gemfile
  2. +31 −0 Gemfile.lock
  3. +7 −0 Rakefile
  4. +3 −1 lib/em-eventsource.rb
  5. +22 −0 spec/em-eventsource_spec.rb
  6. +44 −0 spec/spec_helper.rb
View
@@ -0,0 +1,3 @@
+source :rubygems
+
+gemspec
View
@@ -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
View
@@ -0,0 +1,7 @@
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.pattern = "spec/*_spec.rb"
+end
+
+task :default => :test
@@ -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
@@ -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
View
@@ -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.