/
test_helper.rb
81 lines (67 loc) · 2.34 KB
/
test_helper.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
require 'minitest/autorun'
require 'shoulda/context'
require 'mocha/mini_test'
require 'rack/builder'
require 'rack/mock'
require 'rack/test'
require 'rack/gridfs'
class Hash
def except(*keys)
rejected = Set.new(respond_to?(:convert_key) ? keys.map { |key| convert_key(key) } : keys)
reject { |key,| rejected.include?(key) }
end
end
module Rack
class GridFS
module Test
module Methods
def stub_mongodb_connection
Rack::GridFS::Endpoint.any_instance.stubs(:connect!).returns(true)
end
def test_database_options
{ :hostname => 'localhost', :port => 27017, :database => 'test', :prefix => 'gridfs' }
end
def db
@db ||= Mongo::Connection.new(test_database_options[:hostname], test_database_options[:port]).db(test_database_options[:database])
end
def setup_middleware(opts={})
gridfs_opts = test_database_options.merge(opts)
Rack::Builder.new do
use Rack::ConditionalGet
use Rack::GridFS, gridfs_opts
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
end
end
def setup_endpoint(opts={})
endpoint_opts = test_database_options.except(:prefix).merge(opts)
Rack::Builder.new do
use Rack::ConditionalGet
map '/gridfs' do
run Rack::GridFS::Endpoint.new(endpoint_opts)
end
map '/' do
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ["Hello, World!"]] }
end
end
end
def load_artifact(filename, content_type, path=nil)
contents = ::File.read(::File.join(::File.dirname(__FILE__), 'artifacts', filename))
if path
grid = Mongo::GridFileSystem.new(db)
file = [path, filename].join('/')
grid.open(file, 'w') { |f| f.write contents }
grid.open(file, 'r')
else
Mongo::Grid.new(db).put(contents, :filename => filename, :content_type => content_type)
end
end
def assert_cache_control(cache_control)
assert_equal_header cache_control, "Cache-Control"
end
def assert_equal_header(expected, header)
assert_equal expected, last_response.headers[header]
end
end
end
end
end