rufus-verbs 0.10 → 1.0.0

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.
@@ -7,32 +7,30 @@
7
7
  # Tue Feb 12 23:10:54 JST 2008
8
8
  #
9
9
 
10
- require 'test/unit'
11
-
12
- require 'rufus/verbs'
10
+ require File.dirname(__FILE__) + '/base.rb'
13
11
 
14
12
 
15
13
  class EscapeTest < Test::Unit::TestCase
16
14
 
17
- include Rufus::Verbs
15
+ include Rufus::Verbs
18
16
 
19
17
 
20
- def test_0
18
+ def test_0
21
19
 
22
- req = put(
23
- :dry_run => true,
24
- :uri => "http://localhost:7777/items/1",
25
- :query => { "a" => "hontou ni ?" })
20
+ req = put(
21
+ :dry_run => true,
22
+ :uri => "http://localhost:7777/items/1",
23
+ :query => { "a" => "hontou ni ?" })
26
24
 
27
- assert_equal "/items/1?a=hontou%20ni%20?", req.path
25
+ assert_equal "/items/1?a=hontou%20ni%20?", req.path
28
26
 
29
- req = put(
30
- :dry_run => true,
31
- :uri => "http://localhost:7777/items/1",
32
- :params => { "a" => "hontou ni ?" },
33
- :no_escape => true)
27
+ req = put(
28
+ :dry_run => true,
29
+ :uri => "http://localhost:7777/items/1",
30
+ :params => { "a" => "hontou ni ?" },
31
+ :no_escape => true)
34
32
 
35
- assert_equal "/items/1?a=hontou ni ?", req.path
36
- # would fail anyway...
37
- end
33
+ assert_equal "/items/1?a=hontou ni ?", req.path
34
+ # would fail anyway...
35
+ end
38
36
  end
@@ -7,54 +7,52 @@
7
7
  # Mon Feb 18 13:00:36 JST 2008
8
8
  #
9
9
 
10
- require 'test/unit'
11
- require 'testbase'
12
10
 
13
- require 'rufus/verbs'
11
+ require File.dirname(__FILE__) + '/base.rb'
14
12
 
15
13
 
16
14
  class UriTest < Test::Unit::TestCase
17
- include TestBaseMixin
15
+ include TestBaseMixin
18
16
 
19
- include Rufus::Verbs
17
+ include Rufus::Verbs
20
18
 
21
- def test_0
19
+ def test_0
22
20
 
23
- uri = "http://localhost:7777/items"
21
+ uri = "http://localhost:7777/items"
24
22
 
25
- res = fopen uri
26
- assert_equal 200, res.code.to_i
27
- assert_equal "{}", res.body.strip
23
+ res = fopen uri
24
+ assert_equal 200, res.code.to_i
25
+ assert_equal "{}", res.body.strip
28
26
 
29
- res = fopen "file:CHANGELOG.txt"
30
- assert_kind_of String, res.read
27
+ res = fopen "file:CHANGELOG.txt"
28
+ assert_kind_of String, res.read
31
29
 
32
- res = fopen "CHANGELOG.txt"
33
- assert_kind_of String, res.read
30
+ res = fopen "CHANGELOG.txt"
31
+ assert_kind_of String, res.read
34
32
 
35
- res = fopen "http://localhost:7777/things"
36
- assert_equal 200, res.code.to_i
37
- assert_equal "{}", res.body.strip
38
- #
39
- # it follows redirections :)
33
+ res = fopen "http://localhost:7777/things"
34
+ assert_equal 200, res.code.to_i
35
+ assert_equal "{}", res.body.strip
36
+ #
37
+ # it follows redirections :)
40
38
 
41
- res = fopen "http://localhost:7777/things", :noredir => true
42
- assert_equal 303, res.code.to_i
39
+ res = fopen "http://localhost:7777/things", :noredir => true
40
+ assert_equal 303, res.code.to_i
43
41
 
44
- fopen "CHANGELOG.txt" do |f|
45
- assert_kind_of String, f.read
46
- end
42
+ fopen "CHANGELOG.txt" do |f|
43
+ assert_kind_of String, f.read
44
+ end
47
45
 
48
- fopen "http://localhost:7777/things" do |res|
49
- assert_equal 200, res.code.to_i
50
- assert_equal "{}", res.body.strip
51
- end
46
+ fopen "http://localhost:7777/things" do |res|
47
+ assert_equal 200, res.code.to_i
48
+ assert_equal "{}", res.body.strip
52
49
  end
50
+ end
53
51
 
54
- def test_1
52
+ def test_1
55
53
 
56
- assert_kind_of String, fopen("CHANGELOG.txt").read
57
- assert_kind_of String, fopen("file:CHANGELOG.txt").read
58
- assert_kind_of String, fopen("http://localhost:7777/items").read
59
- end
54
+ assert_kind_of String, fopen("CHANGELOG.txt").read
55
+ assert_kind_of String, fopen("file:CHANGELOG.txt").read
56
+ assert_kind_of String, fopen("http://localhost:7777/items").read
57
+ end
60
58
  end
@@ -7,36 +7,33 @@
7
7
  # Mon Jan 14 00:07:38 JST 2008
8
8
  #
9
9
 
10
- require 'test/unit'
11
- require 'testbase'
12
-
13
- require 'rufus/verbs'
10
+ require File.dirname(__FILE__) + '/base.rb'
14
11
 
15
12
 
16
13
  class HttpsTest < Test::Unit::TestCase
17
- include TestBaseMixin
14
+ include TestBaseMixin
18
15
 
19
- include Rufus::Verbs
16
+ include Rufus::Verbs
20
17
 
21
- def setup
22
- # no need for an items server
23
- end
18
+ def setup
19
+ # no need for an items server
20
+ end
24
21
 
25
- def teardown
26
- end
22
+ def teardown
23
+ end
27
24
 
28
- def test_0
25
+ def test_0
29
26
 
30
- res = expect(
31
- 200,
32
- nil,
33
- get(:uri => "http://jmettraux.wordpress.com/2006/03/31/cvs-down/"))
27
+ res = expect(
28
+ 200,
29
+ nil,
30
+ get(:uri => "https://jmettraux.wordpress.com/2006/03/31/cvs-down/"))
34
31
 
35
- #res.each_header do |h|
36
- # p h
37
- #end
32
+ #res.each_header do |h|
33
+ # p h
34
+ #end
38
35
 
39
- #puts res.body
40
- assert res.body.match "^<!DOCTYPE html PUBLIC"
41
- end
36
+ #puts res.body
37
+ assert res.body.match "^<!DOCTYPE html PUBLIC"
38
+ end
42
39
  end
@@ -7,62 +7,60 @@
7
7
  # Sun Jan 13 12:33:03 JST 2008
8
8
  #
9
9
 
10
- require 'test/unit'
11
- require 'testbase'
12
10
 
13
- require 'rufus/verbs'
11
+ require File.dirname(__FILE__) + '/base.rb'
14
12
 
15
13
 
16
14
  class ItemConditionalTest < Test::Unit::TestCase
17
- include TestBaseMixin
15
+ include TestBaseMixin
18
16
 
19
- include Rufus::Verbs
17
+ include Rufus::Verbs
20
18
 
21
19
 
22
- def test_0
20
+ def test_0
23
21
 
24
- require 'open-uri'
25
- f = open "http://localhost:7777/items"
26
- d = f.read
27
- f.close
22
+ require 'open-uri'
23
+ f = open "http://localhost:7777/items"
24
+ d = f.read
25
+ f.close
28
26
 
29
- assert_equal "{}", d.strip
30
- end
27
+ assert_equal "{}", d.strip
28
+ end
31
29
 
32
- def test_1
30
+ def test_1
33
31
 
34
- res = get :uri => "http://localhost:7777/items"
32
+ res = get :uri => "http://localhost:7777/items"
35
33
 
36
- assert_equal 200, res.code.to_i
37
- assert_equal "{}", res.body.strip
34
+ assert_equal 200, res.code.to_i
35
+ assert_equal "{}", res.body.strip
38
36
 
39
- p res['Etag']
40
- #p res['Last-Modified']
41
- assert res['Etag'].length > 0
42
- assert res['Last-Modified'].length > 0
43
- end
37
+ p res['Etag']
38
+ #p res['Last-Modified']
39
+ assert res['Etag'].length > 0
40
+ assert res['Last-Modified'].length > 0
41
+ end
44
42
 
45
- def test_2
43
+ def test_2
46
44
 
47
- res = get "http://localhost:7777/items"
48
- assert_equal 200, res.code.to_i
45
+ res = get "http://localhost:7777/items"
46
+ assert_equal 200, res.code.to_i
49
47
 
50
- lm = res['Last-Modified']
51
- etag = res['Etag']
48
+ lm = res['Last-Modified']
49
+ etag = res['Etag']
52
50
 
53
- res = get(
54
- "http://localhost:7777/items",
55
- :headers => { 'If-Modified-Since' => lm })
56
- assert_equal 304, res.code.to_i
51
+ res = get(
52
+ "http://localhost:7777/items",
53
+ :headers => { 'If-Modified-Since' => lm })
54
+ assert_equal 304, res.code.to_i
57
55
 
58
- res = get(
59
- "http://localhost:7777/items", :h => { 'If-None-Match' => etag })
60
- assert_equal 304, res.code.to_i
56
+ res = get(
57
+ "http://localhost:7777/items", :h => { 'If-None-Match' => etag })
58
+ assert_equal 304, res.code.to_i
61
59
 
62
- res = get(
63
- "http://localhost:7777/items",
64
- :h => { 'If-Modified-Since' => lm, 'If-None-Match' => etag })
65
- assert_equal 304, res.code.to_i
66
- end
60
+ res = get(
61
+ "http://localhost:7777/items",
62
+ :h => { 'If-Modified-Since' => lm, 'If-None-Match' => etag })
63
+ assert_equal 304, res.code.to_i
64
+ end
67
65
  end
68
66
 
@@ -19,36 +19,36 @@ $dcount = 0 # tracking the number of hits when doing digest auth
19
19
  #
20
20
  class LastModifiedHash
21
21
 
22
- def initialize
22
+ def initialize
23
23
 
24
- @hash = {}
25
- touch
26
- end
24
+ @hash = {}
25
+ touch
26
+ end
27
27
 
28
- def touch (key=nil)
28
+ def touch (key=nil)
29
29
 
30
- now = Time.now.httpdate
31
- @hash[key] = now if key
32
- @hash['__self'] = now
33
- end
30
+ now = Time.now.httpdate
31
+ @hash[key] = now if key
32
+ @hash['__self'] = now
33
+ end
34
34
 
35
- def delete (key)
35
+ def delete (key)
36
36
 
37
- @hash.delete key
38
- @hash['__self'] = Time.now.httpdate
39
- end
37
+ @hash.delete key
38
+ @hash['__self'] = Time.now.httpdate
39
+ end
40
40
 
41
- def last_modified (key)
41
+ def last_modified (key)
42
42
 
43
- key = key || '__self'
44
- @hash[key]
45
- end
43
+ key = key || '__self'
44
+ @hash[key]
45
+ end
46
46
 
47
- def clear
47
+ def clear
48
48
 
49
- @hash.clear
50
- @hash['__self'] = Time.now.httpdate
51
- end
49
+ @hash.clear
50
+ @hash['__self'] = Time.now.httpdate
51
+ end
52
52
  end
53
53
 
54
54
 
@@ -57,173 +57,173 @@ end
57
57
  #
58
58
  class ItemServlet < WEBrick::HTTPServlet::AbstractServlet
59
59
 
60
- @@items = {}
61
- @@last_item_id = -1
60
+ @@items = {}
61
+ @@last_item_id = -1
62
62
 
63
- @@lastmod = LastModifiedHash.new
63
+ @@lastmod = LastModifiedHash.new
64
64
 
65
- @@authenticator = WEBrick::HTTPAuth::DigestAuth.new(
66
- :UserDB => WEBrick::HTTPAuth::Htdigest.new('test/test.htdigest'),
67
- :Realm => 'test_realm')
65
+ @@authenticator = WEBrick::HTTPAuth::DigestAuth.new(
66
+ :UserDB => WEBrick::HTTPAuth::Htdigest.new('test/test.htdigest'),
67
+ :Realm => 'test_realm')
68
68
 
69
- def initialize (server, *options)
69
+ def initialize (server, *options)
70
70
 
71
- super
72
- @auth = server.auth
73
- end
71
+ super
72
+ @auth = server.auth
73
+ end
74
74
 
75
- #
76
- # Overriding the service() method to perform a potential auth check
77
- #
78
- def service (req, res)
75
+ #
76
+ # Overriding the service() method to perform a potential auth check
77
+ #
78
+ def service (req, res)
79
79
 
80
- if @auth == :basic
80
+ if @auth == :basic
81
81
 
82
- WEBrick::HTTPAuth.basic_auth(req, res, "items") do |u, p|
83
- (u != nil and u == p)
84
- end
82
+ WEBrick::HTTPAuth.basic_auth(req, res, "items") do |u, p|
83
+ (u != nil and u == p)
84
+ end
85
85
 
86
- elsif @auth == :digest
86
+ elsif @auth == :digest
87
87
 
88
- $dcount += 1
89
- @@authenticator.authenticate(req, res)
90
- end
91
-
92
- super
88
+ $dcount += 1
89
+ @@authenticator.authenticate(req, res)
93
90
  end
94
91
 
95
- def do_GET (req, res)
92
+ super
93
+ end
94
+
95
+ def do_GET (req, res)
96
96
 
97
- i = item_id req
97
+ i = item_id req
98
98
 
99
- return reply(res, 404, "no item '#{i}'") \
100
- if i and not items[i]
99
+ return reply(res, 404, "no item '#{i}'") \
100
+ if i and not items[i]
101
101
 
102
- representation, et, lm = fetch_representation i
102
+ representation, et, lm = fetch_representation i
103
103
 
104
- since = req['If-Modified-Since']
105
- since = DateTime.parse(since) if since
106
- match = req['If-None-Match']
104
+ since = req['If-Modified-Since']
105
+ since = DateTime.parse(since) if since
106
+ match = req['If-None-Match']
107
107
 
108
- if ((not since and not match) or
109
- (since and (since > DateTime.parse(lm))) or
110
- (match and (match != et)))
108
+ if ((not since and not match) or
109
+ (since and (since > DateTime.parse(lm))) or
110
+ (match and (match != et)))
111
111
 
112
- res['Etag'] = et
113
- res['Last-Modified'] = lm
114
- res.body = representation.inspect + "\n"
112
+ res['Etag'] = et
113
+ res['Last-Modified'] = lm
114
+ res.body = representation.inspect + "\n"
115
115
 
116
- else
116
+ else
117
117
 
118
- reply(res, 304, "Not Modified")
119
- end
118
+ reply(res, 304, "Not Modified")
120
119
  end
120
+ end
121
121
 
122
- def do_POST (req, res)
122
+ def do_POST (req, res)
123
123
 
124
- query = WEBrick::HTTPUtils::parse_query(req.query_string)
125
- m = query['_method']
126
- m = m.downcase if m
127
- return do_PUT(req, res) if m == 'put'
128
- return do_DELETE(req, res) if m == 'delete'
124
+ query = WEBrick::HTTPUtils::parse_query(req.query_string)
125
+ m = query['_method']
126
+ m = m.downcase if m
127
+ return do_PUT(req, res) if m == 'put'
128
+ return do_DELETE(req, res) if m == 'delete'
129
129
 
130
- i = item_id req
130
+ i = item_id req
131
131
 
132
- i = (@@last_item_id += 1) unless i
132
+ i = (@@last_item_id += 1) unless i
133
133
 
134
- items[i] = req.body
135
- lastmod.touch i
134
+ items[i] = req.body
135
+ lastmod.touch i
136
136
 
137
- res['Location'] = "#{@host}/items/#{i}"
138
- reply res, 201, "item created"
139
- end
137
+ res['Location'] = "#{@host}/items/#{i}"
138
+ reply res, 201, "item created"
139
+ end
140
140
 
141
- def do_PUT (req, res)
141
+ def do_PUT (req, res)
142
142
 
143
- i = item_id req
143
+ i = item_id req
144
144
 
145
- return reply(res, 404, "no item '#{i}'") unless items[i]
145
+ return reply(res, 404, "no item '#{i}'") unless items[i]
146
146
 
147
- items[i] = req.body
148
- lastmod.touch i
147
+ items[i] = req.body
148
+ lastmod.touch i
149
149
 
150
- reply res, 200, "item updated"
151
- end
150
+ reply res, 200, "item updated"
151
+ end
152
152
 
153
- def do_DELETE (req, res)
153
+ def do_DELETE (req, res)
154
154
 
155
- i = item_id req
155
+ i = item_id req
156
156
 
157
- return reply(res, 404, "no item '#{i}'") unless items[i]
157
+ return reply(res, 404, "no item '#{i}'") unless items[i]
158
158
 
159
- items.delete i
160
- lastmod.delete i
159
+ items.delete i
160
+ lastmod.delete i
161
161
 
162
- reply res, 200, "item deleted"
163
- end
162
+ reply res, 200, "item deleted"
163
+ end
164
164
 
165
- #
166
- # clears the items
167
- #
168
- def self.flush
165
+ #
166
+ # clears the items
167
+ #
168
+ def self.flush
169
169
 
170
- @@items.clear
171
- @@lastmod.clear
172
- end
170
+ @@items.clear
171
+ @@lastmod.clear
172
+ end
173
173
 
174
- protected
174
+ protected
175
175
 
176
- def items
177
- @@items
178
- end
176
+ def items
177
+ @@items
178
+ end
179
179
 
180
- def lastmod
181
- @@lastmod
182
- end
180
+ def lastmod
181
+ @@lastmod
182
+ end
183
183
 
184
- def is_modified (req, key)
184
+ def is_modified (req, key)
185
185
 
186
- since = req['If-Modified-Since']
187
- match = req['If-None-Match']
186
+ since = req['If-Modified-Since']
187
+ match = req['If-None-Match']
188
188
 
189
- return true unless since or match
189
+ return true unless since or match
190
190
 
191
- #puts
192
- #p [ since, match ]
193
- #puts
191
+ #puts
192
+ #p [ since, match ]
193
+ #puts
194
194
 
195
- (since or match)
196
- end
195
+ (since or match)
196
+ end
197
197
 
198
- #
199
- # Returns representation, etag, last_modified
200
- #
201
- def fetch_representation (key=nil)
198
+ #
199
+ # Returns representation, etag, last_modified
200
+ #
201
+ def fetch_representation (key=nil)
202
202
 
203
- representation = if key
204
- items[key]
205
- else
206
- items
207
- end
203
+ representation = if key
204
+ items[key]
205
+ else
206
+ items
207
+ end
208
208
 
209
- [ representation,
210
- representation.inspect.hash.to_s,
211
- lastmod.last_modified(key) ]
212
- end
209
+ [ representation,
210
+ representation.inspect.hash.to_s,
211
+ lastmod.last_modified(key) ]
212
+ end
213
213
 
214
- def reply (res, code, message)
214
+ def reply (res, code, message)
215
215
 
216
- res.status = code
217
- res.body = message + "\n"
218
- res['Content-type'] = "text/plain"
219
- end
216
+ res.status = code
217
+ res.body = message + "\n"
218
+ res['Content-type'] = "text/plain"
219
+ end
220
220
 
221
- def item_id (req)
221
+ def item_id (req)
222
222
 
223
- p = req.path_info[1..-1]
224
- return nil if not p or p == ''
225
- p.to_i
226
- end
223
+ p = req.path_info[1..-1]
224
+ return nil if not p or p == ''
225
+ p.to_i
226
+ end
227
227
  end
228
228
 
229
229
  #
@@ -231,12 +231,12 @@ end
231
231
  #
232
232
  class ThingServlet < WEBrick::HTTPServlet::AbstractServlet
233
233
 
234
- def do_GET (req, res)
234
+ def do_GET (req, res)
235
235
 
236
- res.set_redirect(
237
- WEBrick::HTTPStatus[303],
238
- "http://localhost:7777/items")
239
- end
236
+ res.set_redirect(
237
+ WEBrick::HTTPStatus[303],
238
+ "http://localhost:7777/items")
239
+ end
240
240
  end
241
241
 
242
242
  #
@@ -244,35 +244,35 @@ end
244
244
  #
245
245
  class CookieServlet < WEBrick::HTTPServlet::AbstractServlet
246
246
 
247
- @@sessions = {}
247
+ @@sessions = {}
248
248
 
249
- def do_GET (req, res)
249
+ def do_GET (req, res)
250
250
 
251
- res.body = get_session(req, res).inspect
252
- end
251
+ res.body = get_session(req, res).inspect
252
+ end
253
253
 
254
- def do_POST (req, res)
254
+ def do_POST (req, res)
255
255
 
256
- get_session(req, res) << req.body.strip
257
- res.body = "ok."
258
- end
256
+ get_session(req, res) << req.body.strip
257
+ res.body = "ok."
258
+ end
259
259
 
260
- protected
260
+ protected
261
261
 
262
- def get_session (req, res)
262
+ def get_session (req, res)
263
263
 
264
- c = req.cookies.find { |c| c.name == 'tcookie' }
264
+ c = req.cookies.find { |c| c.name == 'tcookie' }
265
265
 
266
- if c
267
- @@sessions[c.value]
268
- else
269
- s = []
270
- key = (Time.now.to_f * 100000).to_i.to_s
271
- @@sessions[key] = s
272
- res.cookies << WEBrick::Cookie.new('tcookie', key)
273
- s
274
- end
275
- end
266
+ if c
267
+ @@sessions[c.value]
268
+ else
269
+ s = []
270
+ key = (Time.now.to_f * 100000).to_i.to_s
271
+ @@sessions[key] = s
272
+ res.cookies << WEBrick::Cookie.new('tcookie', key)
273
+ s
274
+ end
275
+ end
276
276
  end
277
277
 
278
278
  #
@@ -280,10 +280,10 @@ end
280
280
  #
281
281
  class LostServlet < WEBrick::HTTPServlet::AbstractServlet
282
282
 
283
- def do_GET (req, res)
283
+ def do_GET (req, res)
284
284
 
285
- sleep 200
286
- end
285
+ sleep 200
286
+ end
287
287
  end
288
288
 
289
289
  #
@@ -292,42 +292,42 @@ end
292
292
  #
293
293
  class ItemServer
294
294
 
295
- def initialize (args={})
295
+ def initialize (args={})
296
296
 
297
- port = args[:port] || 7777
297
+ port = args[:port] || 7777
298
298
 
299
- #al = [
300
- # [ "", WEBrick::AccessLog::COMMON_LOG_FORMAT ],
301
- # [ "", WEBrick::AccessLog::REFERER_LOG_FORMAT ]]
299
+ #al = [
300
+ # [ "", WEBrick::AccessLog::COMMON_LOG_FORMAT ],
301
+ # [ "", WEBrick::AccessLog::REFERER_LOG_FORMAT ]]
302
302
 
303
- @server = WEBrick::HTTPServer.new :Port => port, :AccessLog => nil
303
+ @server = WEBrick::HTTPServer.new :Port => port, :AccessLog => nil
304
304
 
305
- class << @server
306
- attr_accessor :auth
307
- end
305
+ class << @server
306
+ attr_accessor :auth
307
+ end
308
308
 
309
- @server.auth = args[:auth]
309
+ @server.auth = args[:auth]
310
310
 
311
- @server.mount "/items", ItemServlet
312
- @server.mount "/things", ThingServlet
313
- @server.mount "/cookie", CookieServlet
314
- @server.mount "/lost", LostServlet
311
+ @server.mount "/items", ItemServlet
312
+ @server.mount "/things", ThingServlet
313
+ @server.mount "/cookie", CookieServlet
314
+ @server.mount "/lost", LostServlet
315
315
 
316
- [ 'INT', 'TERM' ].each do |signal|
317
- trap(signal) { shutdown }
318
- end
316
+ [ 'INT', 'TERM' ].each do |signal|
317
+ trap(signal) { shutdown }
319
318
  end
319
+ end
320
320
 
321
- def start
321
+ def start
322
322
 
323
- Thread.new { @server.start }
324
- # else the server and the test lock each other
325
- end
323
+ Thread.new { @server.start }
324
+ # else the server and the test lock each other
325
+ end
326
326
 
327
- def shutdown
327
+ def shutdown
328
328
 
329
- ItemServlet.flush
330
- @server.shutdown
331
- end
329
+ ItemServlet.flush
330
+ @server.shutdown
331
+ end
332
332
  end
333
333