pelle-ruby-openid 2.1.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (197) hide show
  1. data/CHANGELOG +215 -0
  2. data/CHANGES-2.1.0 +36 -0
  3. data/INSTALL +47 -0
  4. data/LICENSE +210 -0
  5. data/NOTICE +2 -0
  6. data/README +82 -0
  7. data/UPGRADE +127 -0
  8. data/VERSION +1 -0
  9. data/examples/README +32 -0
  10. data/examples/active_record_openid_store/README +58 -0
  11. data/examples/active_record_openid_store/XXX_add_open_id_store_to_db.rb +24 -0
  12. data/examples/active_record_openid_store/XXX_upgrade_open_id_store.rb +26 -0
  13. data/examples/active_record_openid_store/init.rb +8 -0
  14. data/examples/active_record_openid_store/lib/association.rb +10 -0
  15. data/examples/active_record_openid_store/lib/nonce.rb +3 -0
  16. data/examples/active_record_openid_store/lib/open_id_setting.rb +4 -0
  17. data/examples/active_record_openid_store/lib/openid_ar_store.rb +57 -0
  18. data/examples/active_record_openid_store/test/store_test.rb +212 -0
  19. data/examples/discover +49 -0
  20. data/examples/rails_openid/README +153 -0
  21. data/examples/rails_openid/Rakefile +10 -0
  22. data/examples/rails_openid/app/controllers/application.rb +4 -0
  23. data/examples/rails_openid/app/controllers/consumer_controller.rb +122 -0
  24. data/examples/rails_openid/app/controllers/login_controller.rb +45 -0
  25. data/examples/rails_openid/app/controllers/server_controller.rb +265 -0
  26. data/examples/rails_openid/app/helpers/application_helper.rb +3 -0
  27. data/examples/rails_openid/app/helpers/login_helper.rb +2 -0
  28. data/examples/rails_openid/app/helpers/server_helper.rb +9 -0
  29. data/examples/rails_openid/app/views/consumer/index.rhtml +81 -0
  30. data/examples/rails_openid/app/views/layouts/server.rhtml +68 -0
  31. data/examples/rails_openid/app/views/login/index.rhtml +56 -0
  32. data/examples/rails_openid/app/views/server/decide.rhtml +26 -0
  33. data/examples/rails_openid/config/boot.rb +19 -0
  34. data/examples/rails_openid/config/database.yml +74 -0
  35. data/examples/rails_openid/config/environment.rb +54 -0
  36. data/examples/rails_openid/config/environments/development.rb +19 -0
  37. data/examples/rails_openid/config/environments/production.rb +19 -0
  38. data/examples/rails_openid/config/environments/test.rb +19 -0
  39. data/examples/rails_openid/config/routes.rb +24 -0
  40. data/examples/rails_openid/doc/README_FOR_APP +2 -0
  41. data/examples/rails_openid/public/.htaccess +40 -0
  42. data/examples/rails_openid/public/404.html +8 -0
  43. data/examples/rails_openid/public/500.html +8 -0
  44. data/examples/rails_openid/public/dispatch.cgi +12 -0
  45. data/examples/rails_openid/public/dispatch.fcgi +26 -0
  46. data/examples/rails_openid/public/dispatch.rb +12 -0
  47. data/examples/rails_openid/public/favicon.ico +0 -0
  48. data/examples/rails_openid/public/images/openid_login_bg.gif +0 -0
  49. data/examples/rails_openid/public/javascripts/controls.js +750 -0
  50. data/examples/rails_openid/public/javascripts/dragdrop.js +584 -0
  51. data/examples/rails_openid/public/javascripts/effects.js +854 -0
  52. data/examples/rails_openid/public/javascripts/prototype.js +1785 -0
  53. data/examples/rails_openid/public/robots.txt +1 -0
  54. data/examples/rails_openid/script/about +3 -0
  55. data/examples/rails_openid/script/breakpointer +3 -0
  56. data/examples/rails_openid/script/console +3 -0
  57. data/examples/rails_openid/script/destroy +3 -0
  58. data/examples/rails_openid/script/generate +3 -0
  59. data/examples/rails_openid/script/performance/benchmarker +3 -0
  60. data/examples/rails_openid/script/performance/profiler +3 -0
  61. data/examples/rails_openid/script/plugin +3 -0
  62. data/examples/rails_openid/script/process/reaper +3 -0
  63. data/examples/rails_openid/script/process/spawner +3 -0
  64. data/examples/rails_openid/script/process/spinner +3 -0
  65. data/examples/rails_openid/script/runner +3 -0
  66. data/examples/rails_openid/script/server +3 -0
  67. data/examples/rails_openid/test/functional/login_controller_test.rb +18 -0
  68. data/examples/rails_openid/test/functional/server_controller_test.rb +18 -0
  69. data/examples/rails_openid/test/test_helper.rb +28 -0
  70. data/lib/hmac/hmac.rb +112 -0
  71. data/lib/hmac/sha1.rb +11 -0
  72. data/lib/hmac/sha2.rb +25 -0
  73. data/lib/openid/association.rb +249 -0
  74. data/lib/openid/consumer/associationmanager.rb +344 -0
  75. data/lib/openid/consumer/checkid_request.rb +186 -0
  76. data/lib/openid/consumer/discovery.rb +498 -0
  77. data/lib/openid/consumer/discovery_manager.rb +123 -0
  78. data/lib/openid/consumer/html_parse.rb +134 -0
  79. data/lib/openid/consumer/idres.rb +523 -0
  80. data/lib/openid/consumer/responses.rb +148 -0
  81. data/lib/openid/consumer.rb +395 -0
  82. data/lib/openid/cryptutil.rb +97 -0
  83. data/lib/openid/dh.rb +89 -0
  84. data/lib/openid/extension.rb +39 -0
  85. data/lib/openid/extensions/ax.rb +516 -0
  86. data/lib/openid/extensions/oauth.rb +91 -0
  87. data/lib/openid/extensions/pape.rb +179 -0
  88. data/lib/openid/extensions/sreg.rb +277 -0
  89. data/lib/openid/extras.rb +11 -0
  90. data/lib/openid/fetchers.rb +238 -0
  91. data/lib/openid/kvform.rb +136 -0
  92. data/lib/openid/kvpost.rb +58 -0
  93. data/lib/openid/message.rb +553 -0
  94. data/lib/openid/protocolerror.rb +8 -0
  95. data/lib/openid/server.rb +1544 -0
  96. data/lib/openid/store/filesystem.rb +271 -0
  97. data/lib/openid/store/interface.rb +75 -0
  98. data/lib/openid/store/memcache.rb +107 -0
  99. data/lib/openid/store/memory.rb +84 -0
  100. data/lib/openid/store/nonce.rb +68 -0
  101. data/lib/openid/trustroot.rb +349 -0
  102. data/lib/openid/urinorm.rb +75 -0
  103. data/lib/openid/util.rb +110 -0
  104. data/lib/openid/yadis/accept.rb +148 -0
  105. data/lib/openid/yadis/constants.rb +21 -0
  106. data/lib/openid/yadis/discovery.rb +153 -0
  107. data/lib/openid/yadis/filters.rb +205 -0
  108. data/lib/openid/yadis/htmltokenizer.rb +305 -0
  109. data/lib/openid/yadis/parsehtml.rb +45 -0
  110. data/lib/openid/yadis/services.rb +42 -0
  111. data/lib/openid/yadis/xrds.rb +155 -0
  112. data/lib/openid/yadis/xri.rb +90 -0
  113. data/lib/openid/yadis/xrires.rb +106 -0
  114. data/lib/openid.rb +20 -0
  115. data/setup.rb +1551 -0
  116. data/test/data/accept.txt +124 -0
  117. data/test/data/dh.txt +29 -0
  118. data/test/data/example-xrds.xml +14 -0
  119. data/test/data/linkparse.txt +587 -0
  120. data/test/data/n2b64 +650 -0
  121. data/test/data/test1-discover.txt +137 -0
  122. data/test/data/test1-parsehtml.txt +152 -0
  123. data/test/data/test_discover/malformed_meta_tag.html +19 -0
  124. data/test/data/test_discover/openid.html +11 -0
  125. data/test/data/test_discover/openid2.html +11 -0
  126. data/test/data/test_discover/openid2_xrds.xml +12 -0
  127. data/test/data/test_discover/openid2_xrds_no_local_id.xml +11 -0
  128. data/test/data/test_discover/openid_1_and_2.html +11 -0
  129. data/test/data/test_discover/openid_1_and_2_xrds.xml +16 -0
  130. data/test/data/test_discover/openid_1_and_2_xrds_bad_delegate.xml +17 -0
  131. data/test/data/test_discover/openid_and_yadis.html +12 -0
  132. data/test/data/test_discover/openid_no_delegate.html +10 -0
  133. data/test/data/test_discover/yadis_0entries.xml +12 -0
  134. data/test/data/test_discover/yadis_2_bad_local_id.xml +15 -0
  135. data/test/data/test_discover/yadis_2entries_delegate.xml +22 -0
  136. data/test/data/test_discover/yadis_2entries_idp.xml +21 -0
  137. data/test/data/test_discover/yadis_another_delegate.xml +14 -0
  138. data/test/data/test_discover/yadis_idp.xml +12 -0
  139. data/test/data/test_discover/yadis_idp_delegate.xml +13 -0
  140. data/test/data/test_discover/yadis_no_delegate.xml +11 -0
  141. data/test/data/test_xrds/=j3h.2007.11.14.xrds +25 -0
  142. data/test/data/test_xrds/README +12 -0
  143. data/test/data/test_xrds/delegated-20060809-r1.xrds +34 -0
  144. data/test/data/test_xrds/delegated-20060809-r2.xrds +34 -0
  145. data/test/data/test_xrds/delegated-20060809.xrds +34 -0
  146. data/test/data/test_xrds/no-xrd.xml +7 -0
  147. data/test/data/test_xrds/not-xrds.xml +2 -0
  148. data/test/data/test_xrds/prefixsometimes.xrds +34 -0
  149. data/test/data/test_xrds/ref.xrds +109 -0
  150. data/test/data/test_xrds/sometimesprefix.xrds +34 -0
  151. data/test/data/test_xrds/spoof1.xrds +25 -0
  152. data/test/data/test_xrds/spoof2.xrds +25 -0
  153. data/test/data/test_xrds/spoof3.xrds +37 -0
  154. data/test/data/test_xrds/status222.xrds +9 -0
  155. data/test/data/test_xrds/subsegments.xrds +58 -0
  156. data/test/data/test_xrds/valid-populated-xrds.xml +39 -0
  157. data/test/data/trustroot.txt +153 -0
  158. data/test/data/urinorm.txt +79 -0
  159. data/test/discoverdata.rb +131 -0
  160. data/test/test_accept.rb +170 -0
  161. data/test/test_association.rb +266 -0
  162. data/test/test_associationmanager.rb +917 -0
  163. data/test/test_ax.rb +648 -0
  164. data/test/test_checkid_request.rb +294 -0
  165. data/test/test_consumer.rb +257 -0
  166. data/test/test_cryptutil.rb +119 -0
  167. data/test/test_dh.rb +86 -0
  168. data/test/test_discover.rb +838 -0
  169. data/test/test_discovery_manager.rb +262 -0
  170. data/test/test_extension.rb +46 -0
  171. data/test/test_extras.rb +35 -0
  172. data/test/test_fetchers.rb +538 -0
  173. data/test/test_filters.rb +270 -0
  174. data/test/test_idres.rb +963 -0
  175. data/test/test_kvform.rb +165 -0
  176. data/test/test_kvpost.rb +65 -0
  177. data/test/test_linkparse.rb +101 -0
  178. data/test/test_message.rb +1116 -0
  179. data/test/test_nonce.rb +89 -0
  180. data/test/test_oauth.rb +175 -0
  181. data/test/test_openid_yadis.rb +178 -0
  182. data/test/test_pape.rb +247 -0
  183. data/test/test_parsehtml.rb +80 -0
  184. data/test/test_responses.rb +63 -0
  185. data/test/test_server.rb +2457 -0
  186. data/test/test_sreg.rb +479 -0
  187. data/test/test_stores.rb +298 -0
  188. data/test/test_trustroot.rb +113 -0
  189. data/test/test_urinorm.rb +35 -0
  190. data/test/test_util.rb +145 -0
  191. data/test/test_xrds.rb +169 -0
  192. data/test/test_xri.rb +48 -0
  193. data/test/test_xrires.rb +63 -0
  194. data/test/test_yadis_discovery.rb +220 -0
  195. data/test/testutil.rb +127 -0
  196. data/test/util.rb +53 -0
  197. metadata +316 -0
@@ -0,0 +1,538 @@
1
+ require 'test/unit'
2
+ require 'net/http'
3
+ require 'webrick'
4
+
5
+ require 'testutil'
6
+ require 'util'
7
+
8
+ require 'openid/fetchers'
9
+
10
+ require 'stringio'
11
+
12
+ begin
13
+ require 'net/https'
14
+ rescue LoadError
15
+ # We need these names for testing.
16
+
17
+ module OpenSSL
18
+ module SSL
19
+ class SSLError < StandardError; end
20
+ end
21
+ end
22
+ end
23
+
24
+ module HttpResultAssertions
25
+ def assert_http_result_is(expected, result)
26
+ assert_equal expected.code, result.code
27
+ assert_equal expected.body, result.body
28
+ assert_equal expected.final_url, result.final_url
29
+ end
30
+ end
31
+
32
+ class BogusFetcher
33
+ RESPONSE = "bogus"
34
+
35
+ def fetch(url, body=nil, headers=nil, redirect_limit=5)
36
+ return BogusFetcher::RESPONSE
37
+ end
38
+ end
39
+
40
+ class FetcherTestCase < Test::Unit::TestCase
41
+ include HttpResultAssertions
42
+ include OpenID::TestUtil
43
+
44
+ @@test_header_name = 'X-test-header'
45
+ @@test_header_value = 'marmoset'
46
+
47
+ class ExpectedResponse < Net::HTTPResponse
48
+ attr_reader :final_url
49
+
50
+ def initialize(code, final_url, body="the expected body",
51
+ httpv="1.1", msg=nil)
52
+ super(httpv, code, msg)
53
+ @code = code
54
+ @body = body
55
+ @final_url = final_url
56
+ end
57
+
58
+ def body
59
+ @body
60
+ end
61
+ end
62
+
63
+ @@cases =
64
+ [
65
+ # path, status code, expected url (nil = default to path)
66
+ ['/success', 200, nil],
67
+ ['/notfound', 404, nil],
68
+ ['/badreq', 400, nil],
69
+ ['/forbidden', 403, nil],
70
+ ['/error', 500, nil],
71
+ ['/server_error', 503, nil],
72
+ ['/301redirect', 200, '/success'],
73
+ ['/302redirect', 200, '/success'],
74
+ ['/303redirect', 200, '/success'],
75
+ ['/307redirect', 200, '/success'],
76
+ ]
77
+
78
+ def _redirect_with_code(code)
79
+ lambda { |req, resp|
80
+ resp.status = code
81
+ resp['Location'] = _uri_build('/success')
82
+ }
83
+ end
84
+
85
+ def _respond_with_code(code)
86
+ lambda { |req, resp|
87
+ resp.status = code
88
+ resp.body = "the expected body"
89
+ }
90
+ end
91
+
92
+ def _require_header
93
+ lambda { |req, resp|
94
+ assert_equal @@test_header_value, req[@@test_header_name]
95
+ assert_match 'ruby-openid', req['User-agent']
96
+ }
97
+ end
98
+
99
+ def _require_post
100
+ lambda { |req, resp|
101
+ assert_equal 'POST', req.request_method
102
+ assert_equal "postbody\n", req.body
103
+ }
104
+ end
105
+
106
+ def _redirect_loop
107
+ lambda { |req, resp|
108
+ @_redirect_counter += 1
109
+ resp.status = 302
110
+ resp['Location'] = _uri_build('/redirect_loop')
111
+ resp.body = "Fetched #{@_redirect_counter} times."
112
+ assert_block("Fetched too many times.") { @_redirect_counter < 10 }
113
+ }
114
+ end
115
+
116
+ def setup
117
+ @fetcher = OpenID::StandardFetcher.new
118
+ @logfile = StringIO.new
119
+ @weblog = WEBrick::Log.new(logfile=@logfile)
120
+ @server = WEBrick::HTTPServer.new(:Port => 0,
121
+ :Logger => @weblog,
122
+ :AccessLog => [])
123
+ @server_thread = Thread.new {
124
+ @server.mount_proc('/success', _respond_with_code(200))
125
+ @server.mount_proc('/301redirect', _redirect_with_code(301))
126
+ @server.mount_proc('/302redirect', _redirect_with_code(302))
127
+ @server.mount_proc('/303redirect', _redirect_with_code(303))
128
+ @server.mount_proc('/307redirect', _redirect_with_code(307))
129
+ @server.mount_proc('/badreq', _respond_with_code(400))
130
+ @server.mount_proc('/forbidden', _respond_with_code(403))
131
+ @server.mount_proc('/notfound', _respond_with_code(404))
132
+ @server.mount_proc('/error', _respond_with_code(500))
133
+ @server.mount_proc('/server_error', _respond_with_code(503))
134
+ @server.mount_proc('/require_header', _require_header)
135
+ @server.mount_proc('/redirect_to_reqheader') { |req, resp|
136
+ resp.status = 302
137
+ resp['Location'] = _uri_build('/require_header')
138
+ }
139
+ @server.mount_proc('/post', _require_post)
140
+ @server.mount_proc('/redirect_loop', _redirect_loop)
141
+ @server.start
142
+ }
143
+ @uri = _uri_build
144
+ sleep 0.2
145
+ end
146
+
147
+ def _uri_build(path='/')
148
+ u = URI::HTTP.build({
149
+ :host => @server.config[:ServerName],
150
+ :port => @server.config[:Port],
151
+ :path => path,
152
+ })
153
+ return u.to_s
154
+ end
155
+
156
+ def teardown
157
+ @server.shutdown
158
+ # Sleep a little because sometimes this blocks forever.
159
+ @server_thread.join
160
+ end
161
+
162
+ =begin
163
+ # XXX This test no longer works since we're not dealing with URI
164
+ # objects internally.
165
+ def test_final_url_tainted
166
+ uri = _uri_build('/301redirect')
167
+ result = @fetcher.fetch(uri)
168
+
169
+ final_url = URI::parse(result.final_url)
170
+
171
+ assert final_url.host.tainted?
172
+ assert final_url.path.tainted?
173
+ end
174
+ =end
175
+
176
+ def test_headers
177
+ headers = {
178
+ @@test_header_name => @@test_header_value
179
+ }
180
+ uri = _uri_build('/require_header')
181
+ result = @fetcher.fetch(uri, nil, headers)
182
+ # The real test runs under the WEBrick handler _require_header,
183
+ # this just checks the return code from that.
184
+ assert_equal '200', result.code, @logfile.string
185
+ end
186
+
187
+ def test_headers_after_redirect
188
+ headers = {
189
+ @@test_header_name => @@test_header_value
190
+ }
191
+ uri = _uri_build('/redirect_to_reqheader')
192
+ result = @fetcher.fetch(uri, nil, headers)
193
+ # The real test runs under the WEBrick handler _require_header,
194
+ # this just checks the return code from that.
195
+ assert_equal '200', result.code, @logfile.string
196
+ end
197
+
198
+ def test_post
199
+ uri = _uri_build('/post')
200
+ result = @fetcher.fetch(uri, "postbody\n")
201
+ # The real test runs under the WEBrick handler _require_header,
202
+ # this just checks the return code from that.
203
+ assert_equal '200', result.code, @logfile.string
204
+ end
205
+
206
+ def test_redirect_limit
207
+ @_redirect_counter = 0
208
+ uri = _uri_build('/redirect_loop')
209
+ assert_raise(OpenID::HTTPRedirectLimitReached) {
210
+ @fetcher.fetch(uri)
211
+ }
212
+ end
213
+
214
+ def test_cases
215
+ for path, expected_code, expected_url in @@cases
216
+ uri = _uri_build(path)
217
+ if expected_url.nil?
218
+ expected_url = uri
219
+ else
220
+ expected_url = _uri_build(expected_url)
221
+ end
222
+
223
+ expected = ExpectedResponse.new(expected_code.to_s, expected_url)
224
+ result = @fetcher.fetch(uri)
225
+
226
+ begin
227
+ assert_http_result_is expected, result
228
+ rescue Test::Unit::AssertionFailedError => err
229
+ if result.code == '500' && expected_code != 500
230
+ # Looks like our WEBrick harness broke.
231
+ msg = <<EOF
232
+ Status #{result.code} from case #{path}. Logs:
233
+ #{@logfile.string}
234
+ EOF
235
+ raise msg
236
+ end
237
+
238
+ # Wrap failure messages so we can tell which case failed.
239
+ new_msg = "#{path}: #{err.message.to_s}"
240
+ new_err = Test::Unit::AssertionFailedError.new(new_msg)
241
+ new_err.set_backtrace(err.backtrace)
242
+ raise new_err
243
+ end
244
+ end
245
+ end
246
+
247
+ def test_https_no_openssl
248
+ # Override supports_ssl? to always claim that connections don't
249
+ # support SSL. Test the behavior of fetch() for HTTPS URLs in
250
+ # that case.
251
+ f = OpenID::StandardFetcher.new
252
+ f.extend(OpenID::InstanceDefExtension)
253
+
254
+ f.instance_def(:supports_ssl?) do |conn|
255
+ false
256
+ end
257
+
258
+ begin
259
+ f.fetch("https://someurl.com/")
260
+ flunk("Expected RuntimeError")
261
+ rescue RuntimeError => why
262
+ assert_equal(why.to_s, "SSL support not found; cannot fetch https://someurl.com/")
263
+ end
264
+ end
265
+
266
+ class FakeConnection < Net::HTTP
267
+ attr_reader :use_ssl, :ca_file
268
+
269
+ def initialize *args
270
+ super
271
+ @ca_file = nil
272
+ end
273
+
274
+ def use_ssl=(v)
275
+ @use_ssl = v
276
+ end
277
+
278
+ def ca_file=(ca_file)
279
+ @ca_file = ca_file
280
+ end
281
+ end
282
+
283
+ def test_ssl_with_ca_file
284
+ f = OpenID::StandardFetcher.new
285
+ ca_file = "BOGUS"
286
+ f.ca_file = ca_file
287
+
288
+ f.extend(OpenID::InstanceDefExtension)
289
+ f.instance_def(:make_http) do |uri|
290
+ FakeConnection.new(uri.host, uri.port)
291
+ end
292
+
293
+ testcase = self
294
+
295
+ f.instance_def(:set_verified) do |conn, verified|
296
+ testcase.assert(verified)
297
+ end
298
+
299
+ conn = f.make_connection(URI::parse("https://someurl.com"))
300
+ assert_equal(conn.ca_file, ca_file)
301
+ end
302
+
303
+ def test_ssl_without_ca_file
304
+ f = OpenID::StandardFetcher.new
305
+
306
+ f.extend(OpenID::InstanceDefExtension)
307
+ f.instance_def(:make_http) do |uri|
308
+ FakeConnection.new(uri.host, uri.port)
309
+ end
310
+
311
+ testcase = self
312
+
313
+ f.instance_def(:set_verified) do |conn, verified|
314
+ testcase.assert(!verified)
315
+ end
316
+
317
+ conn = nil
318
+ assert_log_matches(/making https request to https:\/\/someurl.com without verifying/) {
319
+ conn = f.make_connection(URI::parse("https://someurl.com"))
320
+ }
321
+
322
+ assert(conn.ca_file.nil?)
323
+ end
324
+
325
+ def test_make_http_nil
326
+ f = OpenID::StandardFetcher.new
327
+
328
+ f.extend(OpenID::InstanceDefExtension)
329
+ f.instance_def(:make_http) do |uri|
330
+ nil
331
+ end
332
+
333
+ assert_raise(RuntimeError) {
334
+ f.make_connection(URI::parse("http://example.com/"))
335
+ }
336
+ end
337
+
338
+ def test_make_http_invalid
339
+ f = OpenID::StandardFetcher.new
340
+
341
+ f.extend(OpenID::InstanceDefExtension)
342
+ f.instance_def(:make_http) do |uri|
343
+ "not a Net::HTTP object"
344
+ end
345
+
346
+ assert_raise(RuntimeError) {
347
+ f.make_connection(URI::parse("http://example.com/"))
348
+ }
349
+ end
350
+
351
+ class BrokenSSLConnection
352
+ def start(&block)
353
+ raise OpenSSL::SSL::SSLError
354
+ end
355
+ end
356
+
357
+ def test_sslfetchingerror
358
+ f = OpenID::StandardFetcher.new
359
+
360
+ f.extend(OpenID::InstanceDefExtension)
361
+ f.instance_def(:make_connection) do |uri|
362
+ BrokenSSLConnection.new
363
+ end
364
+
365
+ assert_raise(OpenID::SSLFetchingError) {
366
+ f.fetch("https://bogus.com/")
367
+ }
368
+ end
369
+
370
+ class TimeoutConnection
371
+ def start(&block)
372
+ raise Timeout::Error
373
+ end
374
+ end
375
+
376
+ def test_fetchingerror
377
+ f = OpenID::StandardFetcher.new
378
+
379
+ f.extend(OpenID::InstanceDefExtension)
380
+ f.instance_def(:make_connection) do |uri|
381
+ TimeoutConnection.new
382
+ end
383
+
384
+ assert_raise(OpenID::FetchingError) {
385
+ f.fetch("https://bogus.com/")
386
+ }
387
+ end
388
+
389
+ class TestingException < OpenID::FetchingError; end
390
+
391
+ class NoSSLSupportConnection
392
+ def supports_ssl?
393
+ false
394
+ end
395
+
396
+ def start
397
+ yield
398
+ end
399
+
400
+ def request_get(*args)
401
+ raise TestingException
402
+ end
403
+
404
+ def post_connection_check(hostname)
405
+ raise RuntimeError
406
+ end
407
+
408
+ def use_ssl?
409
+ true
410
+ end
411
+ end
412
+
413
+ class NoUseSSLConnection < NoSSLSupportConnection
414
+ def use_ssl?
415
+ false
416
+ end
417
+ end
418
+
419
+ def test_post_connection_check_no_support_ssl
420
+ f = OpenID::StandardFetcher.new
421
+
422
+ f.extend(OpenID::InstanceDefExtension)
423
+ f.instance_def(:make_connection) do |uri|
424
+ NoSSLSupportConnection.new
425
+ end
426
+
427
+ # post_connection_check should not be called.
428
+ assert_raise(TestingException) {
429
+ f.fetch("https://bogus.com/")
430
+ }
431
+ end
432
+
433
+ def test_post_connection_check_no_use_ssl
434
+ f = OpenID::StandardFetcher.new
435
+
436
+ f.extend(OpenID::InstanceDefExtension)
437
+ f.instance_def(:make_connection) do |uri|
438
+ NoUseSSLConnection.new
439
+ end
440
+
441
+ # post_connection_check should not be called.
442
+ assert_raise(TestingException) {
443
+ f.fetch("https://bogus.com/")
444
+ }
445
+ end
446
+
447
+ class PostConnectionCheckException < OpenID::FetchingError; end
448
+
449
+ class UseSSLConnection < NoSSLSupportConnection
450
+ def use_ssl?
451
+ true
452
+ end
453
+
454
+ def post_connection_check(hostname)
455
+ raise PostConnectionCheckException
456
+ end
457
+ end
458
+
459
+ def test_post_connection_check
460
+ f = OpenID::StandardFetcher.new
461
+
462
+ f.extend(OpenID::InstanceDefExtension)
463
+ f.instance_def(:make_connection) do |uri|
464
+ UseSSLConnection.new
465
+ end
466
+
467
+ f.instance_def(:supports_ssl?) do |conn|
468
+ true
469
+ end
470
+
471
+ # post_connection_check should be called.
472
+ assert_raise(PostConnectionCheckException) {
473
+ f.fetch("https://bogus.com/")
474
+ }
475
+ end
476
+ end
477
+
478
+ class DefaultFetcherTest < Test::Unit::TestCase
479
+ def setup
480
+ OpenID.fetcher = nil
481
+ end
482
+
483
+ def test_default_fetcher
484
+ assert(OpenID.fetcher.is_a?(OpenID::StandardFetcher))
485
+
486
+ # A custom fetcher can be set
487
+ OpenID.fetcher = BogusFetcher.new
488
+
489
+ # A test fetch should call the new fetcher
490
+ assert(OpenID.fetch('not-a-url') == BogusFetcher::RESPONSE)
491
+
492
+ # Set the fetcher to nil again
493
+ OpenID.fetcher = nil
494
+ assert(OpenID.fetcher.is_a?(OpenID::StandardFetcher))
495
+ end
496
+ end
497
+
498
+ class ProxyTest < Test::Unit::TestCase
499
+ def test_proxy_unreachable
500
+ begin
501
+ f = OpenID::StandardFetcher.new('127.0.0.1', 1)
502
+ # If this tries to connect to the proxy (on port 1), I expect
503
+ # a 'connection refused' error. If it tries to contact the below
504
+ # URI first, it will get some other sort of error.
505
+ f.fetch("http://unittest.invalid")
506
+ rescue OpenID::FetchingError => why
507
+ # XXX: Is this a translatable string that is going to break?
508
+ if why.message =~ /Connection refused/
509
+ return
510
+ end
511
+ raise why
512
+ end
513
+ flunk "expected Connection Refused, but it passed."
514
+ end
515
+
516
+ def test_proxy_env
517
+ ENV['http_proxy'] = 'http://127.0.0.1:3128/'
518
+ OpenID.fetcher_use_env_http_proxy
519
+
520
+ # make_http just to give us something with readable attributes to inspect.
521
+ conn = OpenID.fetcher.make_http(URI.parse('http://127.0.0.2'))
522
+ assert_equal('127.0.0.1', conn.proxy_address)
523
+ assert_equal(3128, conn.proxy_port)
524
+ end
525
+ # These aren't fully automated tests, but if you start a proxy
526
+ # on port 8888 (tinyproxy's default) and check its logs...
527
+ # def test_proxy
528
+ # f = OpenID::StandardFetcher.new('127.0.0.1', 8888)
529
+ # result = f.fetch("http://www.example.com/")
530
+ # assert_match(/RFC.*2606/, result.body)
531
+ # end
532
+
533
+ # def test_proxy_https
534
+ # f = OpenID::StandardFetcher.new('127.0.0.1', 8888)
535
+ # result = f.fetch("https://www.myopenid.com/")
536
+ # assert_match(/myOpenID/, result.body)
537
+ # end
538
+ end