Rakefile: kill raa_update task
[metropolis.git] / test / rack_read_write.rb
blob5cf102c8f741193e90db187dfba697ef0ae8d2ea
1 # -*- encoding: binary -*-
2 require 'test/unit'
3 require 'stringio'
4 require 'tempfile'
5 require 'rack'
7 module TestRackReadWrite
8   attr_reader :app
10   def test_rack_read_write_suffix_mime
11     @app = Metropolis.new(@app_opts.merge(:encoding => :deflate,
12                                           :use => Metropolis::SuffixMime))
13     basic_rest
15     o = { :lint => true, :fatal => true }
16     req = Rack::MockRequest.new(@app)
17     r = req.put("/asdf.jpg", o.merge(:input => "ASDF"))
18     assert_equal 201, r.status
19     assert_equal "text/plain", r.headers["Content-Type"]
20     assert_equal "Created\n", r.body
22     r = req.get("/asdf.jpg")
23     assert_equal 200, r.status
24     assert_equal "image/jpeg", r.headers["Content-Type"]
25     assert_equal "ASDF", r.body
27     r = req.request("HEAD", "/asdf.jpg")
28     assert_equal 200, r.status
29     assert_equal "image/jpeg", r.headers["Content-Type"]
30     assert_equal "", r.body
31   end
33   def test_rack_read_write_deflated
34     @app = Metropolis.new(@app_opts.merge(:encoding => :deflate))
35     basic_rest
37     blob = "." * 1024 * 1024
38     o = { :lint => true, :fatal => true }
39     req = Rack::MockRequest.new(app)
41     r = req.put("/asdf", o.merge(:input => blob))
42     assert_equal 201, r.status
43     assert_equal "Created\n", r.body
45     r = req.get("/asdf", o.merge("HTTP_ACCEPT_ENCODING" => "deflate"))
46     assert_equal 200, r.status
47     assert_equal "deflate", r.headers['Content-Encoding']
48     assert r.body.size < blob.size
50     r = req.get("/asdf", o.merge("HTTP_ACCEPT_ENCODING" => "gzip"))
51     assert_equal 200, r.status
52     assert_nil r.headers['Content-Encoding']
53     assert_equal blob, r.body
54   end
56   def test_rack_read_write_gzipped
57     @app = Metropolis.new(@app_opts.merge(:encoding => :gzip))
58     basic_rest
60     blob = "." * 1024 * 1024
61     o = { :lint => true, :fatal => true }
62     req = Rack::MockRequest.new(app)
64     r = req.put("/asdf", o.merge(:input => blob))
65     assert_equal 201, r.status
66     assert_equal "Created\n", r.body
68     r = req.get("/asdf", o.merge("HTTP_ACCEPT_ENCODING" => "gzip"))
69     assert_equal 200, r.status
70     assert_equal "gzip", r.headers['Content-Encoding']
71     assert r.body.size < blob.size
73     r = req.get("/asdf", o.merge("HTTP_ACCEPT_ENCODING" => "deflate"))
74     assert_equal 200, r.status
75     assert_nil r.headers['Content-Encoding']
76     assert_equal blob, r.body
77   end
79   def test_rack_read_write
80     @app = Metropolis.new(@app_opts)
81     basic_rest
82   end
84   def basic_rest
85     o = { :lint => true, :fatal => true }
86     req = Rack::MockRequest.new(app)
88     r = req.put("/asdf", o.merge(:input=>"ASDF"))
89     assert_equal 201, r.status
90     assert_equal "Created\n", r.body
92     r = req.get("/asdf")
93     assert_equal 200, r.status
94     assert_equal "ASDF", r.body
96     r = req.request("HEAD", "/asdf", {})
97     assert_equal 200, r.status
98     assert_equal "", r.body
100     r = req.delete("/asdf", {})
101     assert_equal 200, r.status
102     assert_equal "OK\n", r.body
104     r = req.get("/asdf")
105     assert_equal 404, r.status
106     assert_equal "Not Found\n", r.body
108     r = req.delete("/asdf", {})
109     assert_equal 404, r.status
110     assert_equal "Not Found\n", r.body
112     r = req.request("HEAD", "/asdf", {})
113     assert_equal 404, r.status
114     assert_equal "", r.body
115   end
117   def test_rack_readonly
118     tmp = Metropolis.new(@app_opts)
119     tmp.close!
120     @app = Metropolis.new(@app_opts.merge(:readonly => true))
121     basic_rest_readonly
122   end
124   def basic_rest_readonly
125     o = { :lint => true, :fatal => true }
126     req = Rack::MockRequest.new(app)
128     r = req.put("/asdf", o.merge(:input=>"ASDF"))
129     assert_equal 403, r.status
131     r = req.get("/asdf")
132     assert_equal 404, r.status
134     r = req.request("HEAD", "/asdf", {})
135     assert_equal 404, r.status
137     r = req.delete("/asdf", {})
138     assert_equal 403, r.status
139   end