rufus-jig 0.1.23 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.rspec +1 -0
  2. data/CHANGELOG.txt +8 -0
  3. data/README.rdoc +41 -11
  4. data/Rakefile +37 -12
  5. data/TODO.txt +6 -3
  6. data/lib/rufus/jig/adapters/em.rb +21 -24
  7. data/lib/rufus/jig/adapters/net.rb +3 -4
  8. data/lib/rufus/jig/adapters/net_persistent.rb +26 -7
  9. data/lib/rufus/jig/adapters/patron.rb +25 -10
  10. data/lib/rufus/jig/couch.rb +199 -36
  11. data/lib/rufus/jig/http.rb +183 -90
  12. data/lib/rufus/jig/path.rb +4 -4
  13. data/lib/rufus/jig/version.rb +1 -1
  14. data/rufus-jig.gemspec +55 -34
  15. data/spec/couch/attachements_spec.rb +113 -0
  16. data/spec/couch/basic_auth_spec.rb +75 -0
  17. data/spec/couch/conditional_spec.rb +178 -0
  18. data/spec/couch/continuous.rb +97 -0
  19. data/spec/couch/couch_spec.rb +64 -0
  20. data/spec/couch/db_spec.rb +366 -0
  21. data/{test → spec/couch}/tweet.png +0 -0
  22. data/spec/couch/views_spec.rb +326 -0
  23. data/spec/couch_url.txt +2 -0
  24. data/spec/jig/basic_auth_spec.rb +51 -0
  25. data/spec/jig/conditional_spec.rb +76 -0
  26. data/spec/jig/delete_spec.rb +32 -0
  27. data/spec/jig/get_spec.rb +116 -0
  28. data/spec/jig/misc_spec.rb +120 -0
  29. data/spec/jig/new_spec.rb +95 -0
  30. data/spec/jig/parse_uri_spec.rb +139 -0
  31. data/spec/jig/post_spec.rb +79 -0
  32. data/spec/jig/prefix_spec.rb +51 -0
  33. data/spec/jig/put_spec.rb +68 -0
  34. data/spec/jig/timeout_spec.rb +94 -0
  35. data/{test → spec}/server.rb +14 -4
  36. data/spec/spec_helper.rb +61 -0
  37. data/spec/support/couch_helper.rb +14 -0
  38. data/spec/support/server_helper.rb +32 -0
  39. metadata +98 -43
  40. data/lib/rufus/jig/adapters/net_response.rb +0 -42
  41. data/test/base.rb +0 -53
  42. data/test/bm/bm0.rb +0 -49
  43. data/test/bm/bm1.rb +0 -43
  44. data/test/conc/put_vs_delete.rb +0 -28
  45. data/test/couch_base.rb +0 -52
  46. data/test/couch_url.txt +0 -1
  47. data/test/ct_0_couch.rb +0 -64
  48. data/test/ct_1_couchdb.rb +0 -204
  49. data/test/ct_2_couchdb_options.rb +0 -50
  50. data/test/ct_3_couchdb_views.rb +0 -106
  51. data/test/ct_4_attachments.rb +0 -126
  52. data/test/ct_5_couchdb_continuous.rb +0 -92
  53. data/test/cut_0_auth_couch.rb +0 -62
  54. data/test/test.rb +0 -28
  55. data/test/to.sh +0 -25
  56. data/test/tt_0_get_timeout.rb +0 -92
  57. data/test/ut_0_http_get.rb +0 -191
  58. data/test/ut_1_http_post.rb +0 -81
  59. data/test/ut_2_http_delete.rb +0 -42
  60. data/test/ut_3_http_put.rb +0 -105
  61. data/test/ut_4_http_prefix.rb +0 -50
  62. data/test/ut_5_http_misc.rb +0 -65
  63. data/test/ut_6_args.rb +0 -98
  64. data/test/ut_7_parse_uri.rb +0 -79
  65. data/test/ut_8_auth.rb +0 -37
@@ -0,0 +1,79 @@
1
+
2
+ #
3
+ # specifiying rufus-jig
4
+ #
5
+ # Tue Nov 30 21:09:24 JST 2010
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
9
+
10
+
11
+ describe Rufus::Jig::Http do
12
+
13
+ before(:each) do
14
+ purge_server
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
16
+ end
17
+ after(:each) do
18
+ @h.close
19
+ end
20
+
21
+ def location
22
+ l = @h.last_response.headers['Location']
23
+ l.match(/^http:\/\//) ? l = '/' + l.split('/')[3..-1].join('/') : l
24
+ end
25
+
26
+ describe '#post' do
27
+
28
+ it 'returns the body of the server response' do
29
+
30
+ @h.post('/documents', 'nada').should == 'created.'
31
+ end
32
+
33
+ it 'encodes hashes as JSON by default' do
34
+
35
+ @h.post('/documents', { 'hello' => 'world' })
36
+
37
+ @h.get(location, :accept => :json).should == { 'hello' => 'world' }
38
+ end
39
+
40
+ it 'encodes arrays as JSON by default' do
41
+
42
+ @h.post('/documents', %w[ a b c ])
43
+
44
+ @h.get(location, :accept => :json).should == %w[ a b c ]
45
+ end
46
+
47
+ it 'passes strings as text/plain by default' do
48
+
49
+ @h.post('/documents', 'nada')
50
+
51
+ @h.get(location, :accept => :json).should == 'nada'
52
+ end
53
+
54
+ it 'decodes the body of the server response' do
55
+
56
+ b = @h.post(
57
+ '/documents?mirror=true',
58
+ '{"msg":"hello world"}',
59
+ :content_type => :json)
60
+
61
+ b.should == { 'msg' => 'hello world' }
62
+ end
63
+
64
+ it "by default, doesn't cache" do
65
+
66
+ @h.post('/documents?etag=true', 'nada')
67
+
68
+ @h.cache.size.should == 0
69
+ end
70
+
71
+ it 'caches when :cache => true' do
72
+
73
+ @h.post('/documents?etag=true', 'nada', :cache => true)
74
+
75
+ @h.cache.size.should == 1
76
+ end
77
+ end
78
+ end
79
+
@@ -0,0 +1,51 @@
1
+
2
+ #
3
+ # specifiying rufus-jig
4
+ #
5
+ # Wed Dec 1 09:36:50 JST 2010
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
9
+
10
+
11
+ describe Rufus::Jig::Http do
12
+
13
+ before(:each) do
14
+ purge_server
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :prefix => '/a/b/')
16
+ end
17
+ after(:each) do
18
+ @h.close
19
+ end
20
+
21
+ context "with :prefix => '/a/b'" do
22
+
23
+ it 'gets' do
24
+
25
+ @h.get('c').should == 'C'
26
+ end
27
+
28
+ it 'caches the full path' do
29
+
30
+ @h.get('c')
31
+
32
+ @h.cache.should == { '/a/b/c' => [ '"123456123456"', 'C' ] }
33
+ end
34
+
35
+ it 'posts' do
36
+
37
+ @h.post('c', 'X').should == 'post'
38
+ end
39
+
40
+ it 'puts' do
41
+
42
+ @h.put('c', 'X').should == 'put'
43
+ end
44
+
45
+ it 'deletes' do
46
+
47
+ @h.delete('c').should == 'delete'
48
+ end
49
+ end
50
+ end
51
+
@@ -0,0 +1,68 @@
1
+
2
+ #
3
+ # specifiying rufus-jig
4
+ #
5
+ # Wed Dec 1 09:12:40 JST 2010
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
9
+
10
+
11
+ describe Rufus::Jig::Http do
12
+
13
+ before(:each) do
14
+ purge_server
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
16
+ end
17
+ after(:each) do
18
+ @h.close
19
+ end
20
+
21
+ describe '#put' do
22
+
23
+ it "puts" do
24
+
25
+ @h.put(
26
+ '/documents/1234',
27
+ '{"msg":"hello"}',
28
+ :content_type => 'application/json')
29
+
30
+ @h.get('/documents/1234').should == { 'msg' => 'hello' }
31
+ end
32
+
33
+ it 'puts and decodes the JSON reply' do
34
+
35
+ b = @h.put(
36
+ '/documents/yyyy?mirror=true',
37
+ '{"msg":"hello world"}',
38
+ :content_type => :json)
39
+
40
+ b.should == { 'msg' => 'hello world' }
41
+ end
42
+
43
+ it 'returns true in case of conflict' do
44
+
45
+ @h.put('/conflict', '').should == true
46
+ end
47
+
48
+ it "by default, doesn't cache" do
49
+
50
+ @h.put(
51
+ '/documents/1234',
52
+ '{"msg":"hello"}',
53
+ :content_type => 'application/json')
54
+
55
+ @h.cache.size.should == 0
56
+ end
57
+
58
+ it 'caches when :cache => true' do
59
+
60
+ @h.put(
61
+ '/documents/yyyy?etag=true',
62
+ '{"msg":"hello world"}', :cache => true)
63
+
64
+ @h.cache.size.should == 1
65
+ end
66
+ end
67
+ end
68
+
@@ -0,0 +1,94 @@
1
+
2
+ #
3
+ # specifiying rufus-jig
4
+ #
5
+ # Wed Dec 1 16:13:45 JST 2010
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
9
+
10
+
11
+ describe Rufus::Jig::Http do
12
+
13
+ after(:each) do
14
+ @h.close
15
+ end
16
+
17
+ context 'with a timeout of 1 second' do
18
+
19
+ before(:each) do
20
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :timeout => 1)
21
+ end
22
+
23
+ it 'times out' do
24
+
25
+ lambda { @h.get('/later') }.should raise_error(Rufus::Jig::TimeoutError)
26
+ end
27
+
28
+ it 'times out after 1 second' do
29
+
30
+ t = Time.now
31
+
32
+ @h.get('/later') rescue nil
33
+
34
+ (Time.now - t).should be < (@h.variant == :em ? 4.0 : 3.0)
35
+ end
36
+ end
37
+
38
+ context 'with a timeout of -1' do
39
+
40
+ before(:each) do
41
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :timeout => -1)
42
+ end
43
+
44
+ it 'does not time out' do
45
+
46
+ @h.get('/later').should == 'later'
47
+ end
48
+ end
49
+
50
+ context 'with the default timeout' do
51
+
52
+ before(:each) do
53
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
54
+ end
55
+
56
+ it 'times out' do
57
+
58
+ lambda { @h.get('/later') }.should raise_error(Rufus::Jig::TimeoutError)
59
+ end
60
+
61
+ it 'times out after 5s' do
62
+
63
+ t = Time.now
64
+
65
+ @h.get('/later') rescue nil
66
+
67
+ (Time.now - t).should be < (@h.variant == :em ? 7.0 : 6.0)
68
+ end
69
+ end
70
+
71
+ context 'with a request defined timeout' do
72
+
73
+ before(:each) do
74
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :timeout => 15)
75
+ end
76
+
77
+ it 'times out' do
78
+
79
+ lambda {
80
+ @h.get('/later', :timeout => 1)
81
+ }.should raise_error(Rufus::Jig::TimeoutError)
82
+ end
83
+
84
+ it 'times out after 1 second' do
85
+
86
+ t = Time.now
87
+
88
+ @h.get('/later', :timeout => 1) rescue nil
89
+
90
+ (Time.now - t).should be < (@h.variant == :em ? 4.0 : 3.0)
91
+ end
92
+ end
93
+ end
94
+
@@ -1,5 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
+ log = File.open('server.log', 'wb')
4
+ STDOUT.reopen(log)
5
+ STDERR.reopen(log)
6
+
7
+
3
8
  # a test sinata server
4
9
 
5
10
  require 'rubygems'
@@ -7,6 +12,11 @@ require 'sinatra'
7
12
  require 'json'
8
13
 
9
14
 
15
+ # change the port here if needed
16
+
17
+ set :port, 4567
18
+
19
+
10
20
  #
11
21
  # BASIC
12
22
  #
@@ -106,7 +116,7 @@ post '/documents' do
106
116
 
107
117
  if params[:mirror] || params[:etag]
108
118
  response['Etag'] = "\"#{did}\"" if params[:etag]
109
- content_type request.content_type
119
+ content_type request.content_type || 'text/plain'
110
120
  doc
111
121
  else
112
122
  'created.'
@@ -125,7 +135,7 @@ put '/documents/:id' do
125
135
 
126
136
  if params[:mirror] || params[:etag]
127
137
  response['Etag'] = "\"#{params[:id]}\"" if params[:etag]
128
- content_type request.content_type
138
+ content_type request.content_type || 'text/plain'
129
139
  doc
130
140
  else
131
141
  'created.'
@@ -143,7 +153,7 @@ get '/documents/:id' do
143
153
 
144
154
  if doc = DOCS[params[:id]]
145
155
 
146
- content_type doc.first
156
+ content_type(doc.first || 'text/plain')
147
157
 
148
158
  doc.last
149
159
  else
@@ -212,7 +222,7 @@ end
212
222
 
213
223
  get '/later' do
214
224
 
215
- sleep 7
225
+ sleep(7)
216
226
  'later'
217
227
  end
218
228
 
@@ -0,0 +1,61 @@
1
+
2
+ # Our default
3
+ lib = ENV['JIG_LIB'] || 'net/http'
4
+
5
+ lib = case lib
6
+ when 'net' then 'net/http'
7
+ when 'em' then 'em-http'
8
+ when 'netp' then 'net/http/persistent'
9
+ when 'patron' then 'patron'
10
+ else lib
11
+ end
12
+
13
+ require('openssl') if lib == 'em-http'
14
+ require(lib)
15
+
16
+ unless $advertised
17
+
18
+ if lib == 'em-http'
19
+ Thread.new { EM.run {} }
20
+ Thread.pass until EM.reactor_running?
21
+ end
22
+
23
+ puts
24
+ puts "JIG_LIB lib is '#{lib}' (net/netp/patron/em)"
25
+ $advertised = true
26
+ end
27
+
28
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
29
+
30
+ require 'yajl'
31
+ require 'rufus/jig'
32
+
33
+
34
+ # Requires supporting ruby files with custom matchers and macros, etc,
35
+ # in spec/support/ and its subdirectories.
36
+ #
37
+ Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |pa|
38
+ require(pa)
39
+ }
40
+
41
+
42
+ RSpec.configure do |config|
43
+
44
+ # == Mock Framework
45
+ #
46
+ # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
47
+ #
48
+ # config.mock_with :mocha
49
+ # config.mock_with :flexmock
50
+ # config.mock_with :rr
51
+ config.mock_with :rspec
52
+
53
+ config.include ServerHelper
54
+ config.include CouchHelper
55
+ end
56
+
57
+ unless $server
58
+ ServerHelper.fork_server
59
+ Kernel.at_exit { ServerHelper.kill_server }
60
+ end
61
+
@@ -0,0 +1,14 @@
1
+
2
+ module CouchHelper
3
+
4
+ def couch_url
5
+
6
+ File.readlines(
7
+ File.join(File.dirname(__FILE__), '../couch_url.txt')
8
+ ).find { |line|
9
+ line = line.strip
10
+ line.match(/^http/)
11
+ }
12
+ end
13
+ end
14
+
@@ -0,0 +1,32 @@
1
+
2
+ module ServerHelper
3
+
4
+ def purge_server
5
+
6
+ Rufus::Jig::Http.new('127.0.0.1', 4567).delete('/documents') rescue nil
7
+ end
8
+
9
+ def self.fork_server
10
+
11
+ server = File.expand_path(
12
+ File.join(File.dirname(__FILE__), '..', 'server.rb'))
13
+
14
+ $server = Process.fork do
15
+ exec 'ruby', server
16
+ end
17
+
18
+ sleep 1.0
19
+
20
+ $server
21
+ end
22
+
23
+ def self.kill_server
24
+
25
+ Process.kill('SIGINT', $server) #rescue nil
26
+ Process.wait($server)
27
+ $server = nil
28
+
29
+ nil
30
+ end
31
+ end
32
+