From 5963ddabc51215609ba746c6ae1d0bf9c6c72bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20de=20Metz?= Date: Thu, 28 Jul 2011 10:58:10 +0200 Subject: [PATCH] Starting some tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: François de Metz --- Gemfile | 3 +++ Gemfile.lock | 31 ++++++++++++++++++++++++++ Rakefile | 7 ++++++ lib/em-eventsource.rb | 4 +++- spec/em-eventsource_spec.rb | 22 +++++++++++++++++++ spec/spec_helper.rb | 44 +++++++++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Rakefile create mode 100755 spec/em-eventsource_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..a1b93f3 --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source :rubygems + +gemspec diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..fc04ce7 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..369c081 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.pattern = "spec/*_spec.rb" +end + +task :default => :test diff --git a/lib/em-eventsource.rb b/lib/em-eventsource.rb index 951f163..e450df6 100644 --- a/lib/em-eventsource.rb +++ b/lib/em-eventsource.rb @@ -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 diff --git a/spec/em-eventsource_spec.rb b/spec/em-eventsource_spec.rb new file mode 100755 index 0000000..82b490b --- /dev/null +++ b/spec/em-eventsource_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..c30065b --- /dev/null +++ b/spec/spec_helper.rb @@ -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