test/angelo/eventsource_spec.rb in angelo-0.1.22 vs test/angelo/eventsource_spec.rb in angelo-0.1.23
- old
+ new
@@ -2,34 +2,74 @@
describe Angelo::Responder::Eventsource do
describe 'route builder' do
- define_app do
+ describe 'basics' do
- eventsource '/msg' do |c|
- c.write sse_message 'hi'
- c.close
+ define_app do
+
+ eventsource '/msg' do |c|
+ c.write sse_message 'hi'
+ c.close
+ end
+
+ eventsource '/event' do |c|
+ c.write sse_event :sse, 'bye'
+ c.close
+ end
+
+ eventsource '/headers', foo: 'bar' do |c|
+ c.write sse_event :sse, 'headers'
+ c.close
+ end
+
end
- eventsource '/event' do |c|
- c.write sse_event :sse, 'bye'
- c.close
+ it 'sends messages' do
+ get_sse '/msg' do |msg|
+ msg.must_equal "data: hi\n\n"
+ end
end
- end
+ it 'sends events' do
+ get_sse '/event' do |msg|
+ msg.must_equal "event: sse\ndata: bye\n\n"
+ end
+ end
- it 'sends messages' do
- get_sse '/msg' do |msg|
- msg.must_equal "data: hi\n\n"
+ it 'accepts extra headers hash as second optional parameter' do
+ get_sse '/headers' do |msg|
+ msg.must_equal "event: sse\ndata: headers\n\n"
+ end
+ last_response.headers['Foo'].must_equal 'bar'
end
+
end
- it 'sends events' do
- get_sse '/event' do |msg|
- msg.must_equal "event: sse\ndata: bye\n\n"
+ describe 'error handling' do
+
+ define_app do
+
+ before do
+ raise 'wrong'
+ end
+
+ eventsource '/msg' do |c|
+ c.write sse_message 'hi'
+ c.close
+ end
+
end
+
+ it 'handles exceptions in before blocks' do
+ get_sse '/msg' do |msg|
+ msg.must_equal "wrong"
+ end
+ last_response.status.must_equal 500
+ end
+
end
end
end
@@ -50,10 +90,18 @@
c.write sse_event :sse, 'bye'
c.close
end
end
+ get '/headers' do
+ headers foo: 'bar'
+ eventsource do |c|
+ c.write sse_event :sse, 'headers'
+ c.close
+ end
+ end
+
end
it 'sends messages' do
get_sse '/msg' do |msg|
msg.must_equal "data: hi\n\n"
@@ -62,8 +110,15 @@
it 'sends events' do
get_sse '/event' do |msg|
msg.must_equal "event: sse\ndata: bye\n\n"
end
+ end
+
+ it 'allows headers to be set outside block' do
+ get_sse '/headers' do |msg|
+ msg.must_equal "event: sse\ndata: headers\n\n"
+ end
+ last_response.headers['Foo'].must_equal 'bar'
end
end