public
Rubygem
Description: Take FogBugz offline with you
Clone URL: git://github.com/francois/fogbugz_offline.git
Search Repo:
Initial specifications for FogbugzOffline::Connection.
francois (author)
Thu May 15 12:32:18 -0700 2008
commit  160f048dde8b65ea59a46a7ac55400d934ec4602
tree    5d49b1ac4db8dc4acf828e2bb94112e6dbcee897
parent  ba2cb8726477b42a400fb7b1509866b58a71c6b2
...
16
17
18
 
 
 
 
 
 
 
 
 
 
 
 
19
...
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
0
@@ -16,5 +16,17 @@
0
       super("You have never logged in to #{url}.\n fogbugz_offline login --email you@yourdomain.com --password thepassword #{url}\n\nwill generate a token for you.")
0
     end
0
   end
0
+
0
+ class NoApiAtLocation < RuntimeError
0
+ def initialize(url)
0
+ super("The URL #{url} does not seem to be a valid FogBugz install.")
0
+ end
0
+ end
0
+
0
+ class InvalidApiResponse < RuntimeError
0
+ def initialize(url, response)
0
+ super("The URL #{url} returned an unpexected response: I expected XML, I got:\n#{response}")
0
+ end
0
+ end
0
 end
...
1
 
 
 
2
3
4
 
 
5
6
7
8
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
11
12
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
0
@@ -1,12 +1,35 @@
0
 require "uri"
0
+require "open-uri"
0
+require "rexml/document"
0
+require "rexml/xpath"
0
 
0
 module FogbugzOffline
0
   class Connection
0
+ attr_reader :root_uri, :api_uri
0
+
0
     def initialize(url)
0
       @root_uri = url.respond_to?(:merge) ? url : URI.parse(url)
0
     end
0
 
0
     def validate!
0
+ xml_api_uri = @root_uri.merge("api.xml")
0
+ document = nil
0
+ begin
0
+ xml = get(xml_api_uri)
0
+ document = REXML::Document.new(xml)
0
+ api_url = REXML::XPath.first(document.root, "//response/url/text()")
0
+ raise FogbugzOffline::InvalidApiResponse.new(xml_api_uri, document) if api_url.nil? || api_url.to_s.strip.empty?
0
+ @api_uri = @root_uri.merge(api_url.to_s)
0
+ self
0
+ rescue REXML::ParseException
0
+ raise FogbugzOffline::InvalidApiResponse.new(xml_api_uri, document)
0
+ rescue SystemCallError, OpenURI::HTTPError
0
+ raise FogbugzOffline::NoApiAtLocation.new(@root_uri.to_s)
0
+ end
0
+ end
0
+
0
+ protected
0
+ def get(uri)
0
     end
0
   end
0
 end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
0
@@ -1 +1,112 @@
0
+require File.dirname(__FILE__) + "/spec_helper"
0
+
0
+describe FogbugzOffline::Connection, "#initialize" do
0
+ it "should accept a URL string" do
0
+ lambda { FogbugzOffline::Connection.new("http://my.project.com") }.should_not raise_error
0
+ end
0
+
0
+ it "should accept a URI object" do
0
+ lambda { FogbugzOffline::Connection.new(URI.parse("http://my.project.com")) }.should_not raise_error
0
+ end
0
+
0
+ it "should convert URL string to URI object" do
0
+ FogbugzOffline::Connection.new("http://my.project.com").root_uri.should be_kind_of(URI)
0
+ end
0
+end
0
+
0
+describe FogbugzOffline::Connection, "#validate!" do
0
+ before do
0
+ @url = "http://fogbugz.my-install.com/"
0
+ @connection = FogbugzOffline::Connection.new(@url)
0
+ @connection.stub!(:get).and_return(VALID_XML_RESPONSE)
0
+ end
0
+
0
+ it "should open the URI for reading" do
0
+ @connection.should_receive(:get).with(URI.parse(@url).merge("api.xml")).and_return(VALID_XML_RESPONSE)
0
+ @connection.validate!
0
+ end
0
+
0
+ it "should record the API URI" do
0
+ @connection.stub!(:get).and_return(NON_STANDARD_API_LOCATION_XML_RESPONSE)
0
+ @connection.validate!
0
+ @connection.api_uri.should == URI.parse(@url).merge("some-api-location.asp?")
0
+ end
0
+
0
+ it "should raise a NoApiAtLocation when the #get call raises a SystemException" do
0
+ @connection.stub!(:get).and_raise(Errno::ECONNREFUSED)
0
+ lambda { @connection.validate! }.should raise_error(FogbugzOffline::NoApiAtLocation)
0
+ end
0
+
0
+ it "should raise a NoApiAtLocation when the #get call raises an OpenURI::HTTPError" do
0
+ @connection.stub!(:get).and_raise(OpenURI::HTTPError.new("failed error", StringIO.new))
0
+ lambda { @connection.validate! }.should raise_error(FogbugzOffline::NoApiAtLocation)
0
+ end
0
+
0
+ it "should raise an InvalidApiResponse when the #get call returns a non-XML, non-XML valid document" do
0
+ @connection.stub!(:get).and_return(HTML_RESPONSE)
0
+ lambda { @connection.validate! }.should raise_error(FogbugzOffline::InvalidApiResponse)
0
+ end
0
+
0
+ it "should raise an InvalidApiResponse when the #get call returns a non-XML document" do
0
+ @connection.stub!(:get).and_return(VALID_HTML_RESPONSE)
0
+ lambda { @connection.validate! }.should raise_error(FogbugzOffline::InvalidApiResponse)
0
+ end
0
+
0
+ it "should raise an InvalidApiResponse when the #get call returns an XML document with no <url> element" do
0
+ @connection.stub!(:get).and_return(MISSING_URL_ELEMENT_XML_RESPONSE)
0
+ lambda { @connection.validate! }.should raise_error(FogbugzOffline::InvalidApiResponse)
0
+ end
0
+
0
+ it "should return self" do
0
+ @connection.validate!.should == @connection
0
+ end
0
+
0
+ VALID_XML_RESPONSE = <<EOF
0
+<?xml version="1.0" encoding="UTF-8" ?>
0
+<response>
0
+ <version>5</version>
0
+ <minversion>1</minversion>
0
+ <url>api.asp?</url>
0
+</response>
0
+EOF
0
+
0
+ MISSING_URL_ELEMENT_XML_RESPONSE = <<EOF
0
+<?xml version="1.0" encoding="UTF-8" ?>
0
+<response>
0
+ <version>5</version>
0
+ <minversion>1</minversion>
0
+</response>
0
+EOF
0
+
0
+ NON_STANDARD_API_LOCATION_XML_RESPONSE = <<EOF
0
+<?xml version="1.0" encoding="UTF-8" ?>
0
+<response>
0
+ <version>5</version>
0
+ <minversion>1</minversion>
0
+ <url>some-api-location.asp?</url>
0
+</response>
0
+EOF
0
+
0
+ HTML_RESPONSE = <<EOF
0
+<html>
0
+ <head>
0
+ <title>Bleh, you missed!</title>
0
+ </head>
0
+ <body>
0
+ <p>This is the test, you stupid git!
0
+ </body>
0
+</html>
0
+EOF
0
+
0
+ VALID_HTML_RESPONSE = <<EOF
0
+<html>
0
+ <head>
0
+ <title>Bleh, you missed!</title>
0
+ </head>
0
+ <body>
0
+ <p>This is the test, you stupid git!</p>
0
+ </body>
0
+</html>
0
+EOF
0
+end

Comments

    No one has commented yet.