-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
125 lines (98 loc) · 2.97 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
ENV['RACK_ENV'] = 'test'
require 'rack/grid_serve'
require 'rack/test'
require 'minitest/autorun'
Mongo::Logger.logger.level = Logger::ERROR
MONGO = Mongo::Client.new([ '127.0.0.1:27017' ], :database => 'rack-grid-serve-test')
DB = MONGO.database
DB.drop
DB.fs.open_upload_stream('tmnt.css', content_type: 'text/css') do |stream|
stream.write '.cowabunga {}'
end
DB.fs.open_upload_stream('amazing tmnt.css', content_type: 'text/css') do |stream|
stream.write '.cowabunga {}'
end
DB.fs.open_upload_stream('/slash-tmnt.css', content_type: 'text/css') do |stream|
stream.write '.cowabunga {}'
end
FILE = DB.fs.find({filename: 'tmnt.css'}).first
module Helpers
def inner_app
lambda {|env|
[200, {'Content-Type'=>'text/plain'}, ["Inner"]]
}
end
def assert_file_found
assert_equal 200, last_response.status
assert_equal FILE['contentType'], last_response.headers['Content-Type']
assert_equal 13, last_response.body.size
assert_equal FILE['md5'], last_response.headers['ETag']
assert_equal Time.at(FILE['uploadDate'].to_i).httpdate, last_response.headers['Last-Modified']
assert_equal 'no-cache', last_response.headers['Cache-Control']
end
end
class TestRackGridServe < MiniTest::Test
include Rack::Test::Methods
include Helpers
def app
Rack::Lint.new(Rack::GridServe.new(inner_app, db: DB).freeze).freeze
end
def test_finds_file_by_id
get "/gridfs/#{FILE['_id']}"
assert_file_found
end
def test_finds_file_by_name
get '/gridfs/tmnt.css'
assert_file_found
end
def test_finds_file_by_name_with_url_encoding
get '/gridfs/amazing%20tmnt.css'
assert_file_found
end
def test_finds_file_by_name_with_slash
get '/gridfs/slash-tmnt.css'
assert_file_found
end
def test_uses_conditional_get
get '/gridfs/tmnt.css', {}, {'HTTP_IF_NONE_MATCH'=>FILE['md5']}
assert_equal 304, last_response.status
end
def test_not_found_if_filename_has_no_match
get '/gridfs/unexisting-id'
assert_equal 404, last_response.status
end
def test_pass_if_prefix_only
get '/gridfs'
assert_equal 200, last_response.status
assert_equal "Inner", last_response.body
end
def test_pass_if_root
get '/'
assert_equal 200, last_response.status
assert_equal "Inner", last_response.body
end
def test_pass_if_prefix_not_match
get '/wrong-prefix/1234'
assert_equal 200, last_response.status
assert_equal "Inner", last_response.body
end
def test_pass_if_prefix_not_at_the_begining
get '/before/gridfs/1234'
assert_equal 200, last_response.status
assert_equal "Inner", last_response.body
end
end
class TestRackGridServePrefix < MiniTest::Test
include Rack::Test::Methods
include Helpers
def app
Rack::Lint.new(
Rack::GridServe.new(inner_app, db: DB, prefix: '/attachment/prefix/').freeze
).freeze
end
def test_finds_file_with_custom_prefix
# prefix = "/attachment/prefix/"
get '/attachment/prefix/tmnt.css'
assert_file_found
end
end