This repository has been archived by the owner on Sep 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server_test.rb
110 lines (87 loc) · 3.04 KB
/
server_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
require 'test_helper'
require 'rack/test'
class ServerTest < Test::Unit::TestCase
include Rack::Test::Methods
def app
Copy::Server
end
setup do
app.config do
set :views, File.dirname(File.expand_path(__FILE__)) + '/sample_app/views'
end
end
test "GET index" do
get '/'
assert last_response.ok?
assert_equal 'text/html;charset=utf-8', last_response.headers['Content-Type']
assert_match "<title>I'm the layout!</title>", last_response.body
assert_match "<p>I'm the index!</p>", last_response.body
end
test "GET path with index in folder" do
%w(/about /about/).each do |path|
get path
assert last_response.ok?
assert_match "<p>About!</p>", last_response.body
end
end
test "GET path to template in folder" do
%w(/about/us /about/us/).each do |path|
get path
assert last_response.ok?
assert_match "<p>About us!</p>", last_response.body
end
end
test "GET non-existent path" do
get '/nope'
assert last_response.status == 404
end
test "cache_time setting sets a Cache-Control header" do
app.config { set :cache_time, 456 }
get '/'
assert_equal 'public, max-age=456', last_response.headers['Cache-Control']
[0, nil, false].each do |time|
app.config { set :cache_time, time }
get '/'
assert_equal 'no-cache', last_response.headers['Cache-Control']
end
end
test "GET csv" do
get 'data/people.csv'
assert last_response.ok?
assert_equal 'text/csv;charset=utf-8', last_response.headers['Content-Type']
assert_equal File.read(app.settings.views + '/data/people.csv.erb'), last_response.body
end
test "GET xml" do
get 'data/people.xml'
assert last_response.ok?
assert_equal 'application/xml;charset=utf-8', last_response.headers['Content-Type']
assert_equal File.read(app.settings.views + '/data/people.xml.erb'), last_response.body
end
test "connects to storage when setting present" do
connection_url = 'redis://localhost:1234'
app.config { set :storage, connection_url }
Copy::Storage.expects(:connect!).with(connection_url).once.returns(true)
get '/'
assert last_response.ok?
end
test "copy helper displays content from storage" do
Copy::Storage.expects(:connected?).twice.returns(true)
Copy::Storage.expects(:get).with(:facts).returns("truth")
get 'with_copy_helper'
assert last_response.ok?
assert_match "truth", last_response.body
end
test "copy helper shows default text when content is not in storage" do
Copy::Storage.expects(:connected?).twice.returns(true)
Copy::Storage.expects(:get).with(:facts).returns(nil)
get 'with_copy_helper'
assert last_response.ok?
assert_match "Default Text", last_response.body
end
test "copy helper shows default text when not connected" do
Copy::Storage.expects(:connected?).twice.returns(false)
get 'with_copy_helper'
assert last_response.ok?
assert_match "Default Text", last_response.body
end
end