rufus-jig 0.1.4 → 0.1.5

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.
data/test/server.rb ADDED
@@ -0,0 +1,198 @@
1
+ # encoding: utf-8
2
+
3
+ # a test sinata server
4
+
5
+ require 'rubygems'
6
+ require 'sinatra'
7
+ require 'json'
8
+
9
+
10
+ #
11
+ # BASIC
12
+ #
13
+
14
+ get '/' do
15
+
16
+ content_type 'text/plain'
17
+
18
+ 'hello'
19
+ end
20
+
21
+ get '/document' do
22
+
23
+ content_type 'application/json'
24
+
25
+ '{"car":"Mercedes-Benz"}'
26
+ end
27
+
28
+ get '/document_accept' do
29
+
30
+ if env['HTTP_ACCEPT'] == 'application/json'
31
+ content_type 'application/json'
32
+ else
33
+ content_type 'text/plain'
34
+ end
35
+
36
+ '{"car":"Saab"}'
37
+ end
38
+
39
+ get '/document_utf8' do
40
+
41
+ content_type 'application/json', :charset => 'utf-8'
42
+
43
+ '{"car":"トヨタ"}'
44
+ end
45
+
46
+ get '/document_with_etag' do
47
+
48
+ etag = '"123456123456"'
49
+
50
+ if env['HTTP_IF_NONE_MATCH'] == etag
51
+
52
+ halt 304, 'not modified'
53
+
54
+ else
55
+
56
+ content_type 'application/json'
57
+ response['Etag'] = '"123456123456"'
58
+
59
+ '{"car":"Peugeot"}'
60
+ end
61
+ end
62
+
63
+ get '/server_error' do
64
+
65
+ halt 500, 'internal server error'
66
+ end
67
+
68
+ get '/params' do
69
+
70
+ content_type 'application/json'
71
+ params.to_json
72
+ end
73
+
74
+
75
+ #
76
+ # DOCUMENTS
77
+ #
78
+
79
+ DOCS = {}
80
+
81
+ get '/documents' do
82
+
83
+ content_type 'application/json'
84
+
85
+ DOCS.to_json
86
+ end
87
+
88
+ post '/documents' do
89
+
90
+ did = (Time.now.to_f * 1000).to_i.to_s
91
+ doc = env['rack.input'].read
92
+
93
+ DOCS[did] = [ request.content_type, doc ]
94
+
95
+ response.status = 201
96
+ response['Location'] = "/documents/#{did}"
97
+
98
+ if params[:mirror] || params[:etag]
99
+ response['Etag'] = "\"#{did}\"" if params[:etag]
100
+ content_type request.content_type
101
+ doc
102
+ else
103
+ 'created.'
104
+ end
105
+ end
106
+
107
+ put '/documents/:id' do
108
+
109
+ doc = env['rack.input'].read
110
+
111
+ DOCS[params[:id]] = [ request.content_type, doc ]
112
+
113
+ response.status = 201
114
+
115
+ if params[:mirror] || params[:etag]
116
+ response['Etag'] = "\"#{params[:id]}\"" if params[:etag]
117
+ content_type request.content_type
118
+ doc
119
+ else
120
+ 'created.'
121
+ end
122
+ end
123
+
124
+ put '/conflict' do
125
+
126
+ response.status = 409
127
+
128
+ 'conflict'
129
+ end
130
+
131
+ get '/documents/:id' do
132
+
133
+ if doc = DOCS[params[:id]]
134
+
135
+ content_type doc.first
136
+
137
+ doc.last
138
+ else
139
+
140
+ halt 404, { 'error' => 'not found', 'id' => params[:id] }.to_json
141
+ end
142
+ end
143
+
144
+ delete '/documents/:id' do
145
+
146
+ if doc = DOCS.delete(params[:id])
147
+
148
+ content_type 'application/json'
149
+
150
+ { 'deleted' => params[:id] }.to_json
151
+ else
152
+
153
+ halt 404, { 'error' => 'not found', 'id' => params[:id] }.to_json
154
+ end
155
+ end
156
+
157
+ delete '/documents' do
158
+
159
+ DOCS.clear
160
+
161
+ 'ok'
162
+ end
163
+
164
+
165
+ #
166
+ # PREFIX
167
+ #
168
+
169
+ get '/a/b/c' do
170
+
171
+ content_type 'text/plain'
172
+ response['Etag'] = '"123456123456"'
173
+ 'C'
174
+ end
175
+
176
+ get '/c' do
177
+
178
+ content_type 'text/plain'
179
+ 'c'
180
+ end
181
+
182
+ put '/a/b/c' do
183
+
184
+ response.status = 201
185
+ 'put'
186
+ end
187
+
188
+ post '/a/b/c' do
189
+
190
+ response.status = 201
191
+ 'post'
192
+ end
193
+
194
+ delete '/a/b/c' do
195
+
196
+ 'delete'
197
+ end
198
+
@@ -0,0 +1,165 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Fri Oct 30 17:57:15 JST 2009
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+
11
+ class UtHttpGetTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
16
+ end
17
+
18
+ def test_get_root
19
+
20
+ r = @h.get('/')
21
+
22
+ assert_equal 'hello', r
23
+ end
24
+
25
+ def test_get
26
+
27
+ r = @h.get('/document')
28
+
29
+ assert_equal Hash, r.class
30
+ assert_equal 'Mercedes-Benz', r['car']
31
+
32
+ assert_equal 0, @h.cache.size
33
+ end
34
+
35
+ def test_get_raw
36
+
37
+ r = @h.get('/document', :raw => true)
38
+
39
+ assert_equal 200, r.status
40
+ assert_equal "{\"car\":\"Mercedes-Benz\"}", r.body
41
+ end
42
+
43
+ def test_get_404
44
+
45
+ r = @h.get('/missing')
46
+
47
+ assert_nil r
48
+ end
49
+
50
+ def test_get_404_raw
51
+
52
+ r = @h.get('/missing', :raw => true)
53
+
54
+ assert_equal 404, r.status
55
+ end
56
+
57
+ def test_get_500
58
+
59
+ assert_raise Rufus::Jig::HttpError do
60
+ @h.get('/server_error')
61
+ end
62
+
63
+ assert 500, @h.last_response.status
64
+ end
65
+
66
+ def test_get_500_raw
67
+
68
+ r = @h.get('/server_error', :raw => true)
69
+
70
+ assert 500, r.status
71
+ end
72
+
73
+ def test_get_with_accept
74
+
75
+ r = @h.get('/document_accept', :accept => 'text/plain')
76
+
77
+ assert_equal '{"car":"Saab"}', r
78
+
79
+ r = @h.get('/document_accept', :accept => 'application/json')
80
+
81
+ assert_equal Hash, r.class
82
+ assert_equal 'Saab', r['car']
83
+
84
+ assert_equal 0, @h.cache.size
85
+ end
86
+
87
+ def test_conditional_get
88
+
89
+ b = @h.get('/document_with_etag')
90
+
91
+ etag = @h.last_response.headers['Etag']
92
+
93
+ assert_equal Hash, b.class
94
+ assert_equal 'Peugeot', b['car']
95
+
96
+ assert_equal 200, @h.last_response.status
97
+
98
+ assert_equal({"/document_with_etag"=>["\"123456123456\"", {"car"=>"Peugeot"}]}, @h.cache)
99
+
100
+ r = @h.get('/document_with_etag', :etag => etag)
101
+
102
+ assert_equal Hash, r.class
103
+ assert_equal 'Peugeot', r['car']
104
+
105
+ assert_equal 304, @h.last_response.status
106
+
107
+ assert_equal 1, @h.cache.size
108
+
109
+ r = @h.get('/document_with_etag', :etag => etag, :raw => true)
110
+
111
+ assert_equal 304, r.status
112
+ end
113
+
114
+ def test_cget_dup_body
115
+
116
+ b = @h.get('/document_with_etag')
117
+ etag = @h.last_response.headers['Etag']
118
+ b['year'] = 1977
119
+
120
+ assert_equal({"/document_with_etag"=>["\"123456123456\"", {"car"=>"Peugeot"}]}, @h.cache)
121
+
122
+ b = @h.get('/document_with_etag', :etag => etag)
123
+
124
+ assert_equal 304, @h.last_response.status
125
+
126
+ b['year'] = 1977
127
+
128
+ assert_equal({"/document_with_etag"=>["\"123456123456\"", {"car"=>"Peugeot"}]}, @h.cache)
129
+ end
130
+
131
+ def test_cget_dont_cache
132
+
133
+ b = @h.get('/document_with_etag', :cache => false)
134
+
135
+ assert_equal 0, @h.cache.size
136
+ end
137
+
138
+ def test_get_params
139
+
140
+ assert_equal(
141
+ {},
142
+ @h.get('/params'))
143
+ assert_equal(
144
+ { 'a' => 'b' },
145
+ @h.get('/params?a=b'))
146
+ assert_equal(
147
+ { 'a' => 'b', 'c' => 'd' },
148
+ @h.get('/params?a=b', :params => { :c => 'd' }))
149
+ assert_equal(
150
+ { 'a' => 'b', 'c' => 'd' },
151
+ @h.get('/params', :params => { 'a' => :b, :c => 'd' }))
152
+ assert_equal(
153
+ {},
154
+ @h.get('/params', :params => {}))
155
+ end
156
+
157
+ def test_etag_but_missing
158
+
159
+ b = @h.get('/document_with_etag', :etag => '"123456123456"')
160
+
161
+ assert_equal 'Peugeot', b['car']
162
+ assert_equal 200, @h.last_response.status
163
+ end
164
+ end
165
+
@@ -0,0 +1,56 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Sat Oct 31 23:27:02 JST 2009
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+
11
+ class UtHttpPostTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
16
+
17
+ @h.delete('/documents')
18
+ end
19
+
20
+ def test_post
21
+
22
+ b = @h.post(
23
+ '/documents', '{"msg":"hello"}', :content_type => 'application/json')
24
+
25
+ r = @h.last_response
26
+
27
+ l = r.headers['Location']
28
+
29
+ assert_equal 'created.', b
30
+ assert_equal 201, r.status
31
+ assert_not_nil l
32
+
33
+ assert_equal({ 'msg' => 'hello' }, @h.get(l))
34
+ end
35
+
36
+ def test_post_and_decode_body
37
+
38
+ b = @h.post(
39
+ '/documents?mirror=true', '{"msg":"hello world"}', :content_type => :json)
40
+
41
+ assert_equal({ 'msg' => 'hello world' }, b)
42
+
43
+ assert_equal 0, @h.cache.size
44
+ end
45
+
46
+ def test_post_and_cache
47
+
48
+ b = @h.post(
49
+ '/documents?etag=true', '{"msg":"hello world"}', :content_type => :json)
50
+
51
+ assert_equal({ 'msg' => 'hello world' }, b)
52
+
53
+ assert_equal 1, @h.cache.size
54
+ end
55
+ end
56
+
@@ -0,0 +1,37 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Sun Nov 1 11:59:20 JST 2009
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+
11
+ class UtHttpDeleteTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
16
+
17
+ r = @h.put(
18
+ '/documents/xyz', 'data', :content_type => 'text/plain', :raw => true)
19
+ end
20
+
21
+ def test_delete
22
+
23
+ b = @h.delete('/documents/xyz')
24
+
25
+ assert_equal 200, @h.last_response.status
26
+ assert_equal({ 'deleted' => 'xyz' }, b)
27
+ end
28
+
29
+ def test_delete_raw
30
+
31
+ r = @h.delete('/documents/xyz', :raw => true)
32
+
33
+ assert_equal 200, r.status
34
+ assert_equal "{\"deleted\":\"xyz\"}", r.body
35
+ end
36
+ end
37
+
@@ -0,0 +1,87 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Sat Oct 31 23:27:02 JST 2009
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+
11
+ class UtHttpPutTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
16
+
17
+ @h.delete('/documents')
18
+ end
19
+
20
+ def test_clean
21
+
22
+ assert_nil @h.get('/documents/1234')
23
+ end
24
+
25
+ def test_put
26
+
27
+ b = @h.put('/documents/1234', '{"msg":"hello"}', :content_type => 'application/json')
28
+
29
+ assert_equal 201, @h.last_response.status
30
+
31
+ assert_equal({ 'msg' => 'hello' }, @h.get('/documents/1234'))
32
+ end
33
+
34
+ def test_put_json
35
+
36
+ r = @h.put(
37
+ '/documents/5678',
38
+ { 'msg' => 'hello' },
39
+ :content_type => 'application/json')
40
+
41
+ assert_equal 201, @h.last_response.status
42
+
43
+ assert_equal({ 'msg' => 'hello' }, @h.get('/documents/5678'))
44
+ end
45
+
46
+ def test_put_colon_json
47
+
48
+ b = @h.put(
49
+ '/documents/abcd',
50
+ { 'msg' => 'hello' },
51
+ :content_type => :json)
52
+
53
+ assert_equal 201, @h.last_response.status
54
+
55
+ assert_equal({ 'msg' => 'hello' }, @h.get('/documents/abcd'))
56
+ end
57
+
58
+ def test_put_and_decode_body
59
+
60
+ b = @h.put(
61
+ '/documents/yyyy?mirror=true',
62
+ '{"msg":"hello world"}',
63
+ :content_type => :json)
64
+
65
+ assert_equal({ 'msg' => 'hello world' }, b)
66
+ assert_equal 0, @h.cache.size
67
+ end
68
+
69
+ def test_put_and_cache
70
+
71
+ b = @h.put(
72
+ '/documents/yyyy?etag=true',
73
+ '{"msg":"hello world"}',
74
+ :content_type => :json)
75
+
76
+ assert_equal({ 'msg' => 'hello world' }, b)
77
+ assert_equal 1, @h.cache.size
78
+ end
79
+
80
+ def test_put_conflict
81
+
82
+ r = @h.put('/conflict', '')
83
+
84
+ assert_equal true, r
85
+ end
86
+ end
87
+
@@ -0,0 +1,48 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Sun Nov 1 13:00:46 JST 2009
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+
11
+ class UtHttpPrefixTest < Test::Unit::TestCase
12
+
13
+ def setup
14
+
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :prefix => '/a/b/')
16
+ end
17
+
18
+ def test_get
19
+
20
+ b = @h.get('c')
21
+
22
+ assert_equal 'C', b
23
+
24
+ assert_equal({"/a/b/c"=>["\"123456123456\"", "C"]}, @h.cache)
25
+ end
26
+
27
+ def test_put
28
+
29
+ b = @h.put('c', 'data')
30
+
31
+ assert_equal 201, @h.last_response.status
32
+ end
33
+
34
+ def test_post
35
+
36
+ b = @h.post('c', 'data')
37
+
38
+ assert_equal 201, @h.last_response.status
39
+ end
40
+
41
+ def test_delete
42
+
43
+ b = @h.delete('c')
44
+
45
+ assert_equal 200, @h.last_response.status
46
+ end
47
+ end
48
+
@@ -0,0 +1,49 @@
1
+
2
+ #
3
+ # testing rufus-jig
4
+ #
5
+ # Sun Nov 1 13:00:46 JST 2009
6
+ #
7
+
8
+ require File.join(File.dirname(__FILE__), 'base')
9
+
10
+
11
+ class UtHttpMiscTest < Test::Unit::TestCase
12
+
13
+ def test_prefix
14
+
15
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :prefix => '/a/b')
16
+
17
+ assert_equal 'c', @h.get('/c')
18
+ assert_equal 'C', @h.get('c')
19
+
20
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :prefix => 'a/b')
21
+
22
+ assert_equal 'c', @h.get('/c')
23
+ assert_equal 'C', @h.get('c')
24
+
25
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567)
26
+
27
+ assert_equal 'C', @h.get('/a/b/c')
28
+ assert_equal 'C', @h.get('a/b/c')
29
+
30
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :prefix => '/')
31
+
32
+ assert_equal 'C', @h.get('/a/b/c')
33
+ assert_equal 'C', @h.get('a/b/c')
34
+
35
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :prefix => '')
36
+
37
+ assert_equal 'C', @h.get('/a/b/c')
38
+ assert_equal 'C', @h.get('a/b/c')
39
+ end
40
+
41
+ def test_no_prefix
42
+
43
+ @h = Rufus::Jig::Http.new('127.0.0.1', 4567, :prefix => '/a/b')
44
+
45
+ assert_equal({ 'car' => 'Mercedes-Benz' }, @h.get('/document'))
46
+ assert_equal(nil, @h.get('document'))
47
+ end
48
+ end
49
+