rufus-jig 0.1.21 → 0.1.22

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,7 +13,7 @@ class CtCouchDbTest < Test::Unit::TestCase
13
13
 
14
14
  def setup
15
15
 
16
- h = Rufus::Jig::Http.new('127.0.0.1', 5984)
16
+ h = Rufus::Jig::Http.new(couch_url)
17
17
 
18
18
  begin
19
19
  h.delete('/rufus_jig_test')
@@ -26,7 +26,7 @@ class CtCouchDbTest < Test::Unit::TestCase
26
26
 
27
27
  h.close
28
28
 
29
- @c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'rufus_jig_test')
29
+ @c = Rufus::Jig::Couch.new(couch_url, 'rufus_jig_test')
30
30
  end
31
31
 
32
32
  def teardown
@@ -46,7 +46,7 @@ class CtCouchDbTest < Test::Unit::TestCase
46
46
 
47
47
  assert_nil r
48
48
 
49
- doc = Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test/coffee0')
49
+ doc = Rufus::Jig::Http.new(couch_url).get('/rufus_jig_test/coffee0')
50
50
 
51
51
  assert_not_nil doc['_rev']
52
52
  end
@@ -131,7 +131,7 @@ class CtCouchDbTest < Test::Unit::TestCase
131
131
  assert_nil r
132
132
 
133
133
  assert_nil(
134
- Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test/coffee1'))
134
+ Rufus::Jig::Http.new(couch_url).get('/rufus_jig_test/coffee1'))
135
135
  end
136
136
 
137
137
  def test_delete_2_args
@@ -141,7 +141,7 @@ class CtCouchDbTest < Test::Unit::TestCase
141
141
  @c.delete(doc['_id'], doc['_rev'])
142
142
 
143
143
  assert_nil(
144
- Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test/coffee1'))
144
+ Rufus::Jig::Http.new(couch_url).get('/rufus_jig_test/coffee1'))
145
145
  end
146
146
 
147
147
  def test_delete_conflict
@@ -196,7 +196,7 @@ class CtCouchDbTest < Test::Unit::TestCase
196
196
  assert_nil r
197
197
 
198
198
  doc =
199
- Rufus::Jig::Http.new('127.0.0.1', 5984).get('/rufus_jig_test/コーヒー')
199
+ Rufus::Jig::Http.new(couch_url).get('/rufus_jig_test/コーヒー')
200
200
 
201
201
  assert_not_nil doc['_rev']
202
202
  end
@@ -12,7 +12,7 @@ class CtCouchDbOptionsTest < Test::Unit::TestCase
12
12
 
13
13
  def setup
14
14
 
15
- h = Rufus::Jig::Http.new('127.0.0.1', 5984)
15
+ h = Rufus::Jig::Http.new(couch_url)
16
16
 
17
17
  begin
18
18
  h.delete('/rufus_jig_test')
@@ -25,11 +25,7 @@ class CtCouchDbOptionsTest < Test::Unit::TestCase
25
25
 
26
26
  h.close
27
27
 
28
- @c = Rufus::Jig::Couch.new(
29
- #'127.0.0.1', 5984, 'rufus_jig_test', :re_put_ok => false)
30
- '127.0.0.1', 5984, 'rufus_jig_test')
31
-
32
- # CouchDB >= 0.11 rendered re_put_ok useless
28
+ @c = Rufus::Jig::Couch.new(couch_url, 'rufus_jig_test')
33
29
  end
34
30
 
35
31
  def teardown
@@ -12,7 +12,7 @@ class CtCouchDbViewsTest < Test::Unit::TestCase
12
12
 
13
13
  def setup
14
14
 
15
- h = Rufus::Jig::Http.new('127.0.0.1', 5984)
15
+ h = Rufus::Jig::Http.new(couch_url)
16
16
 
17
17
  begin
18
18
  h.delete('/rufus_jig_test')
@@ -42,7 +42,7 @@ class CtCouchDbViewsTest < Test::Unit::TestCase
42
42
 
43
43
  h.close
44
44
 
45
- @c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'rufus_jig_test')
45
+ @c = Rufus::Jig::Couch.new(couch_url, 'rufus_jig_test')
46
46
  end
47
47
 
48
48
  def teardown
@@ -12,7 +12,7 @@ class CtAttachmentsTest < Test::Unit::TestCase
12
12
 
13
13
  def setup
14
14
 
15
- h = Rufus::Jig::Http.new('127.0.0.1', 5984)
15
+ h = Rufus::Jig::Http.new(couch_url)
16
16
  begin
17
17
  h.delete('/rufus_jig_test')
18
18
  rescue Exception => e
@@ -22,7 +22,7 @@ class CtAttachmentsTest < Test::Unit::TestCase
22
22
  h.put('/rufus_jig_test', '')
23
23
  h.close
24
24
 
25
- @c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'rufus_jig_test')
25
+ @c = Rufus::Jig::Couch.new(couch_url, 'rufus_jig_test')
26
26
 
27
27
  @c.put('_id' => 'thedoc', 'function' => 'recipient for attachements')
28
28
  @d = @c.get('thedoc')
@@ -13,7 +13,7 @@ class CtCouchDbContinuousTest < Test::Unit::TestCase
13
13
 
14
14
  def setup
15
15
 
16
- h = Rufus::Jig::Http.new('127.0.0.1', 5984)
16
+ h = Rufus::Jig::Http.new(couch_url)
17
17
 
18
18
  begin
19
19
  h.delete('/rufus_jig_test')
@@ -23,7 +23,7 @@ class CtCouchDbContinuousTest < Test::Unit::TestCase
23
23
  h.put('/rufus_jig_test', '')
24
24
  h.close
25
25
 
26
- @c = Rufus::Jig::Couch.new('127.0.0.1', 5984, 'rufus_jig_test')
26
+ @c = Rufus::Jig::Couch.new(couch_url, 'rufus_jig_test')
27
27
  end
28
28
 
29
29
  def teardown
@@ -84,7 +84,9 @@ class CtCouchDbContinuousTest < Test::Unit::TestCase
84
84
  sleep 0.154
85
85
  t.kill
86
86
 
87
- assert_equal [["angel4", false], ["angel4", true]], stack
87
+ assert(
88
+ [["angel4", false], ["angel4", true]] == stack ||
89
+ [["angel4", true]] == stack)
88
90
  end
89
91
  end
90
92
 
@@ -0,0 +1,62 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Sun Sep 26 18:26:15 JST 2010
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+
11
+ class UtAuthTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+ @c = Rufus::Jig::Couch.new(
15
+ '127.0.0.1', 4567, 'tcouch', :basic_auth => %w[ admin nimda ])
16
+ end
17
+ def teardown
18
+ @c.close
19
+ end
20
+
21
+ def test_denied
22
+
23
+ c = Rufus::Jig::Couch.new('127.0.0.1', 4567, 'tcouch')
24
+
25
+ assert_raise Rufus::Jig::HttpError do
26
+ c.get('.')
27
+ end
28
+ end
29
+
30
+ def test_authorized
31
+
32
+ assert_equal({ 'id' => 'nada' }, @c.get('.'))
33
+ end
34
+
35
+ def test_on_change
36
+
37
+ res = nil
38
+
39
+ t = Thread.new {
40
+ @c.on_change do |id, deleted, doc|
41
+ res = [ id, deleted, doc ]
42
+ end
43
+ }
44
+
45
+ sleep 0.200
46
+
47
+ t.kill
48
+
49
+ assert_equal [ 'x', false, { 'hello' => 'world' } ], res
50
+ end
51
+
52
+ def test_on_change_denied
53
+
54
+ c = Rufus::Jig::Couch.new('127.0.0.1', 4567, 'tcouch')
55
+
56
+ assert_raise Rufus::Jig::HttpError do
57
+ c.on_change do |id, deleted, doc|
58
+ end
59
+ end
60
+ end
61
+ end
62
+
@@ -216,3 +216,54 @@ get '/later' do
216
216
  'later'
217
217
  end
218
218
 
219
+
220
+ #
221
+ # BASIC AUTH
222
+
223
+ helpers do
224
+
225
+ def basic_auth_required
226
+
227
+ return if authorized?
228
+
229
+ response['WWW-Authenticate'] = 'Basic realm="rufus-jig test"'
230
+ throw :halt, [ 401, "Not authorized\n" ]
231
+ end
232
+
233
+ def authorized?
234
+
235
+ @auth ||= Rack::Auth::Basic::Request.new(request.env)
236
+ @auth.provided? && @auth.basic? && @auth.credentials == [ 'admin', 'nimda' ]
237
+ end
238
+ end
239
+
240
+ get '/protected' do
241
+
242
+ basic_auth_required
243
+
244
+ content_type 'application/json'
245
+ '{ "info": "secretive" }'
246
+ end
247
+
248
+
249
+ #
250
+ # AUTH COUCH
251
+ #
252
+ # simulating a basic authentified couchdb instance
253
+
254
+ get '/tcouch' do
255
+
256
+ basic_auth_required
257
+
258
+ content_type 'application/json'
259
+ '{ "id": "nada" }'
260
+ end
261
+
262
+ get '/tcouch/_changes' do
263
+
264
+ basic_auth_required
265
+
266
+ content_type 'application/json'
267
+ '{ "id": "x", "deleted": false, "doc": { "hello": "world" }' + "\r\n"
268
+ end
269
+
@@ -21,7 +21,7 @@ set = if ARGV.include?('--all')
21
21
  elsif ARGV.include?('--couch')
22
22
  %w[ ct ]
23
23
  else
24
- %w[ ut ]
24
+ %w[ ut cut ]
25
25
  end
26
26
 
27
27
  set.each { |prefix| load_tests(prefix) }
@@ -63,14 +63,14 @@ class UtHttpGetTest < Test::Unit::TestCase
63
63
  @h.get('/server_error')
64
64
  end
65
65
 
66
- assert 500, @h.last_response.status
66
+ assert_equal 500, @h.last_response.status
67
67
  end
68
68
 
69
69
  def test_get_500_raw
70
70
 
71
71
  r = @h.get('/server_error', :raw => true)
72
72
 
73
- assert 500, r.status
73
+ assert_equal 500, r.status
74
74
  end
75
75
 
76
76
  def test_get_with_accept
@@ -10,80 +10,89 @@ require File.join(File.dirname(__FILE__), 'base')
10
10
 
11
11
  class UtArgsTest < Test::Unit::TestCase
12
12
 
13
- def test_extract_args
14
-
15
- # [ http, path, payload, opts ]
16
-
17
- a = eh(false, 'http://127.0.0.1:5984')
18
- assert_equal '127.0.0.1', a[0].host
19
- assert_equal 5984, a[0].port
20
- assert_equal '/', a[1]
21
- assert_equal nil, a[2]
22
- assert_equal '{}', a[3].inspect
23
-
24
- a = eh(false, '127.0.0.1', 5984, '/')
25
- assert_equal '127.0.0.1', a[0].host
26
- assert_equal 5984, a[0].port
27
- assert_equal '/', a[1]
28
- assert_equal nil, a[2]
29
- assert_equal '{}', a[3].inspect
30
-
31
- a = eh(false, '127.0.0.1', 5984, '/', :option => true)
32
- assert_equal '127.0.0.1', a[0].host
33
- assert_equal 5984, a[0].port
34
- assert_equal '/', a[1]
35
- assert_equal nil, a[2]
36
- assert_equal '{:option=>true}', a[3].inspect
13
+ def test_simple_uri
14
+
15
+ h = Rufus::Jig::Http.new('http://127.0.0.1:5984')
16
+
17
+ assert_equal 'http', h.scheme
18
+ assert_equal '127.0.0.1', h.host
19
+ assert_equal 5984, h.port
20
+ assert_equal '', h._path
21
+ assert_equal nil, h._query
22
+ assert_equal nil, h.options[:basic_auth]
37
23
  end
38
24
 
39
- def test_payload_expected
25
+ def test_uri_with_path_and_query
40
26
 
41
- a = eh(true, 'http://127.0.0.1:5984', [ 1, 2, 3 ])
42
- assert_equal '127.0.0.1', a[0].host
43
- assert_equal 5984, a[0].port
44
- assert_equal '/', a[1]
45
- assert_equal [ 1, 2, 3 ], a[2]
46
- assert_equal '{}', a[3].inspect
27
+ h = Rufus::Jig::Http.new('http://127.0.0.1:5984/nada?a=b&c=d')
47
28
 
48
- assert_raise(ArgumentError) {
49
- eh(true, 'http://127.0.0.1:5984', :nada, [ 1, 2, 3 ])
50
- }
29
+ assert_equal 'http', h.scheme
30
+ assert_equal '127.0.0.1', h.host
31
+ assert_equal 5984, h.port
32
+ assert_equal '/nada', h._path
33
+ assert_equal 'a=b&c=d', h._query
34
+ assert_equal nil, h.options[:basic_auth]
51
35
  end
52
36
 
53
- def test_payload_is_a_hash
54
-
55
- a = eh(true, 'http://127.0.0.1:5984', { 'cheese' => 'burger' })
56
- assert_equal '127.0.0.1', a[0].host
57
- assert_equal 5984, a[0].port
58
- assert_equal '/', a[1]
59
- assert_equal '{"cheese"=>"burger"}', a[2].inspect
60
- assert_equal '{}', a[3].inspect
61
-
62
- a = eh(true, 'http://127.0.0.1:5984', { 'cheese' => 'burger' }, { :cache => true })
63
- assert_equal '127.0.0.1', a[0].host
64
- assert_equal 5984, a[0].port
65
- assert_equal '/', a[1]
66
- assert_equal '{"cheese"=>"burger"}', a[2].inspect
67
- assert_equal '{:cache=>true}', a[3].inspect
37
+ def test_uri_with_basic_auth
38
+
39
+ h = Rufus::Jig::Http.new('http://u:p@127.0.0.1:5984')
40
+
41
+ assert_equal 'http', h.scheme
42
+ assert_equal '127.0.0.1', h.host
43
+ assert_equal 5984, h.port
44
+ assert_equal '', h._path
45
+ assert_equal nil, h._query
46
+ assert_equal %w[ u p ], h.options[:basic_auth]
68
47
  end
69
48
 
70
- def test_with_http_instance
49
+ def test_host_port
71
50
 
72
51
  h = Rufus::Jig::Http.new('127.0.0.1', 5984)
73
52
 
74
- a = eh(true, h, '/', { 'cheese' => 'burger' })
75
- assert_equal '127.0.0.1', a[0].host
76
- assert_equal 5984, a[0].port
77
- assert_equal '/', a[1]
78
- assert_equal '{"cheese"=>"burger"}', a[2].inspect
79
- assert_equal '{}', a[3].inspect
53
+ assert_equal 'http', h.scheme
54
+ assert_equal '127.0.0.1', h.host
55
+ assert_equal 5984, h.port
56
+ assert_equal nil, h._path
57
+ assert_equal nil, h._query
58
+ assert_equal nil, h.options[:basic_auth]
59
+ end
60
+
61
+ def test_host_port_path
62
+
63
+ h = Rufus::Jig::Http.new('127.0.0.1', 5984, '/banana')
64
+
65
+ assert_equal 'http', h.scheme
66
+ assert_equal '127.0.0.1', h.host
67
+ assert_equal 5984, h.port
68
+ assert_equal '/banana', h._path
69
+ assert_equal nil, h._query
70
+ assert_equal nil, h.options[:basic_auth]
71
+ end
72
+
73
+ def test_host_port_path_options
74
+
75
+ h = Rufus::Jig::Http.new(
76
+ '127.0.0.1', 5984, '/banana', :basic_auth => %w[ u p ])
77
+
78
+ assert_equal 'http', h.scheme
79
+ assert_equal '127.0.0.1', h.host
80
+ assert_equal 5984, h.port
81
+ assert_equal '/banana', h._path
82
+ assert_equal nil, h._query
83
+ assert_equal %w[ u p ], h.options[:basic_auth]
80
84
  end
81
85
 
82
- protected
86
+ def test_uri_plus_path
83
87
 
84
- def eh (pe, *args)
88
+ h = Rufus::Jig::Http.new('http://127.0.0.1:5984', '/banana')
85
89
 
86
- Rufus::Jig::Http.extract_http(pe, *args)
90
+ assert_equal 'http', h.scheme
91
+ assert_equal '127.0.0.1', h.host
92
+ assert_equal 5984, h.port
93
+ assert_equal '/banana', h._path
94
+ assert_equal nil, h._query
95
+ assert_equal nil, h.options[:basic_auth]
87
96
  end
88
97
  end
89
98
 
@@ -6,7 +6,12 @@
6
6
  # Tue Jun 22 12:31:35 JST 2010
7
7
  #
8
8
 
9
- require File.join(File.dirname(__FILE__), 'base')
9
+ #require File.join(File.dirname(__FILE__), 'base')
10
+ lib = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
11
+ $:.unshift(lib) unless $:.include?(lib)
12
+
13
+ require 'test/unit'
14
+ require 'rufus/jig'
10
15
 
11
16
 
12
17
  class UtParseUriTest < Test::Unit::TestCase
@@ -19,6 +24,12 @@ class UtParseUriTest < Test::Unit::TestCase
19
24
 
20
25
  def test_parse_uri
21
26
 
27
+ assert_equal(
28
+ 'http',
29
+ Rufus::Jig.parse_uri('http://www.unifr.ch').scheme)
30
+ assert_equal(
31
+ 'https',
32
+ Rufus::Jig.parse_uri('https://www.unifr.ch').scheme)
22
33
  assert_equal(
23
34
  'www.unifr.ch',
24
35
  Rufus::Jig.parse_uri('http://www.unifr.ch').host)
@@ -29,7 +40,7 @@ class UtParseUriTest < Test::Unit::TestCase
29
40
  'mufg.jp',
30
41
  Rufus::Jig.parse_uri('http://mufg.jp/大和').host)
31
42
  assert_equal(
32
- '8080',
43
+ 8080,
33
44
  Rufus::Jig.parse_uri('http://mufg.jp:8080/大和').port)
34
45
  assert_equal(
35
46
  '/大和',
@@ -48,5 +59,21 @@ class UtParseUriTest < Test::Unit::TestCase
48
59
  nil,
49
60
  Rufus::Jig.parse_uri('/').host)
50
61
  end
62
+
63
+ def test_parse_uri_with_auth
64
+
65
+ assert_equal(
66
+ 'admin',
67
+ Rufus::Jig.parse_uri('http://admin:nimda@example.com').username)
68
+ assert_equal(
69
+ 'nimda',
70
+ Rufus::Jig.parse_uri('http://admin:nimda@example.com').password)
71
+ assert_equal(
72
+ 'http',
73
+ Rufus::Jig.parse_uri('http://admin:nimda@example.com').scheme)
74
+ assert_equal(
75
+ 'example.com',
76
+ Rufus::Jig.parse_uri('http://admin:nimda@example.com').host)
77
+ end
51
78
  end
52
79