From 31212387e629465734d81d8b86b4ad8b630e12ea Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Mon, 2 Mar 2009 05:54:31 +0000 Subject: [PATCH] Catch errors better. --- lib/vpim/agent/app.rb | 47 ++++++++++++++++++++++++-------------- test/common.rb | 9 +++++++- test/test_agent_app.rb | 14 +++++++++++- test/test_agent_atomize.rb | 6 ++--- 4 files changed, 54 insertions(+), 22 deletions(-) diff --git a/lib/vpim/agent/app.rb b/lib/vpim/agent/app.rb index 43ef827..b8c9694 100644 --- a/lib/vpim/agent/app.rb +++ b/lib/vpim/agent/app.rb @@ -11,6 +11,8 @@ require 'vpim/repo' require 'vpim/view' +require 'cgi' + configure do server = Sinatra::Application.server set :server, Proc.new { @@ -44,33 +46,27 @@ def self.atomize(caluri, feeduri) end end -def from_is_ics(from) - true -end - -def from_is_empty(from) - return from.empty? - not from or from =~ /^\s*$/ -end - get '/ics' do from = env['QUERY_STRING'] url = URI.parse(request.url) url.query = nil url_base = url.to_s + url_atom = nil + + @url_ics = from # ics from here + @url_atom = nil + + if not from.empty? + # Error out if we can't atomize the feed + Vpim::Agent::App.atomize(from, "http://example.com") - if from.empty? - from = nil - url_atom = nil - else url = URI.parse(request.url) url.path << "/atom" url_atom = url.to_s end @url_base = url_base # clean input form - @url_ics = from # ics from here @url_atom = url_atom # atomized ics from here haml :"ics.haml" @@ -91,7 +87,11 @@ def from_is_empty(from) port = env["SERVER_PORT"].to_i here = request.url - # if from is empty, redirect to /ics + if from.empty? + url = URI.parse(here) + url.path.sub(/atom$/, "") + redirect here.to_s + end xml, xmltype = Vpim::Agent::App.atomize(from, here) @@ -104,6 +104,11 @@ def from_is_empty(from) sass :"ics.sass" end +error do + @url_error = CGI.escapeHTML(env['sinatra.error'].inspect) + haml :"ics.haml" +end + use_in_file_templates! # FIXME - hard-coded :action paths below, bad! @@ -116,6 +121,8 @@ def from_is_empty(from) a :color black :font-style italic + a:hover + :color darkred #header :border-bottom 3px solid darkred @@ -165,7 +172,7 @@ def from_is_empty(from) %form#form{:method => 'POST', :action => '/ics'} %input#url{:name => 'url', :value => params[:url]} %input#button{:type => 'submit', :value => 'Submit'} - - if @url_ics + - if @url_atom #subscribe .text Subscribe to @@ -174,8 +181,14 @@ def from_is_empty(from) %ul.feed %a{:href => @url_atom}= @url_atom (atom feed) + - if @url_error + #error.text + #preamble Sorry, trying to access: + #source= @url_ics + #transition resulted in: + #destination= @url_error #footer .text :textile - Coming from the "Octet Cloud":http://octetcloud.com/ using "vPim":http://vpim.rubyforge.org/, with the aid of cloud monkey "Sam Roberts":mailto:vieuxtech@gmail.com + Coming from the "Octet Cloud":http://octetcloud.com/ using "vPim":http://vpim.rubyforge.org/, piloted by cloud monkey "Sam Roberts":mailto:vieuxtech@gmail.com diff --git a/test/common.rb b/test/common.rb index 100eff6..9bcf009 100644 --- a/test/common.rb +++ b/test/common.rb @@ -19,7 +19,14 @@ def data_on_port(data, port) Thread.abort_on_exception = true thrd = Thread.new do require "webrick" - server = WEBrick::HTTPServer.new( :Port => port ) + l = WEBrick::Log.new(STDOUT, WEBrick::BasicLog::WARN) + server = WEBrick::HTTPServer.new( + :Port => port, + :Logger => l, + :AccessLog => l + ) + #server.logger.level = WEBrick::BasicLog::WARN + #pp server.logger begin server.mount_proc("/") do |req,resp| resp.body = data.to_str diff --git a/test/test_agent_app.rb b/test/test_agent_app.rb index 39ecb58..ad9a177 100644 --- a/test/test_agent_app.rb +++ b/test/test_agent_app.rb @@ -55,8 +55,20 @@ def test_ics_query end def test_ics_atom - # TODO redirect to ics + get '/ics/atom' + assert_equal(302, @response.status) end +=begin + +WTF? Sinatra doesn't run it's error catcher in unit test mode? + def test_ics_atom_query_bad + get '/ics/atom?http://example.com' + assert_equal(500, @response.status) + assert(@response.body =~ Regexp.new( + Regexp.quote("error")), @response.body) + end +=end + end diff --git a/test/test_agent_atomize.rb b/test/test_agent_atomize.rb index cc620b6..72b3599 100644 --- a/test/test_agent_atomize.rb +++ b/test/test_agent_atomize.rb @@ -26,7 +26,7 @@ def test_minimal assert_equal(feed.entries.size, 1) assert_equal("http://example.com/feed", feed.id) - assert_equal("http://example.com/calendar - atomized!", feed.title) + assert_equal("http://example.com/calendar", feed.title) assert(feed.to_xml.to_str) assert_equal(nil, feed.entries.first.title) assert_equal(nil, feed.entries.first.content) @@ -47,7 +47,7 @@ def test_small assert_equal(feed.entries.size, 1) assert_equal("http://example.com/feed", feed.id) - assert_equal("http://example.com/calendar - atomized!", feed.title) + assert_equal("http://example.com/calendar", feed.title) assert_equal("I am summarized", feed.entries.first.title) assert_equal("And I am described", feed.entries.first.content) assert(feed.to_xml.to_str) @@ -73,7 +73,7 @@ def test_recurring puts feed.to_xml assert_equal(1, feed.entries.size) assert_equal("http://example.com/feed", feed.id) - assert_equal("http://example.com/calendar - atomized!", feed.title) + assert_equal("http://example.com/calendar", feed.title) assert_equal("I am summarized", feed.entries.first.title) assert_equal("And I am described", feed.entries.first.content) assert(feed.to_xml.to_str)