-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathtest_app.rb
More file actions
146 lines (113 loc) · 3.66 KB
/
test_app.rb
File metadata and controls
146 lines (113 loc) · 3.66 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# frozen_string_literal: true
require 'sinatra/base'
require 'json'
class TestApp < Sinatra::Base
set :environment, :test
set :server, 'webrick'
disable :protection
set :counter, 0
set :requests, 0
set :yesterday, (Date.today - 1).httpdate
get '/ping' do
'PONG'
end
get '/clear' do
settings.counter = 0
settings.requests = 0
status 204
end
get '/json' do
json = JSON.dump(count: increment_counter.to_i)
[200, { 'Cache-Control' => 'max-age=400', 'Content-Type' => 'application/json' }, json]
end
get '/image' do
image = File.expand_path('empty.png', __dir__)
data = IO.binread(image)
[200, { 'Cache-Control' => 'max-age=400', 'Content-Type' => 'image/png' }, data]
end
post '/post' do
[200, { 'Cache-Control' => 'max-age=400' }, increment_counter]
end
get '/broken' do
[500, { 'Cache-Control' => 'max-age=400' }, increment_counter]
end
get '/counter' do
[200, { 'Cache-Control' => 'max-age=200' }, increment_counter]
end
post '/counter' do
end
put '/counter' do
end
delete '/counter' do
end
patch '/counter' do
end
get '/get' do
[200, { 'Cache-Control' => 'max-age=200' }, increment_counter]
end
get '/stale-while-revalidate' do
[200, { 'Cache-Control' => 'max-age=0, stale-while-revalidate=120', 'Date' => Time.now.httpdate, 'ETag' => 'stale' }, increment_counter]
end
get '/stale-while-revalidate-expired' do
if env['HTTP_IF_NONE_MATCH'] == '1'
[304, {}, '']
else
[200, { 'Cache-Control' => 'max-age=0, stale-while-revalidate=1', 'Date' => settings.yesterday, 'ETag' => '1' }, increment_counter]
end
end
post '/delete-with-location' do
[200, { 'Location' => "#{request.base_url}/get" }, '']
end
post '/delete-with-content-location' do
[200, { 'Content-Location' => "#{request.base_url}/get" }, '']
end
post '/get' do
halt 405
end
get '/private' do
[200, { 'Cache-Control' => 'private, max-age=100' }, increment_counter]
end
get '/dontstore' do
[200, { 'Cache-Control' => 'no-store' }, increment_counter]
end
get '/expires' do
[200, { 'Expires' => (Time.now + 10).httpdate }, increment_counter]
end
get '/yesterday' do
[200, { 'Date' => settings.yesterday, 'Expires' => settings.yesterday }, increment_counter]
end
get '/must-revalidate' do
[200, { 'Date' => Time.now.httpdate, 'Cache-Control' => 'public, max-age=23880, must-revalidate, no-transform' }, increment_counter]
end
get '/timestamped' do
settings.counter += 1
header = settings.counter > 2 ? '1' : '2'
if env['HTTP_IF_MODIFIED_SINCE'] == header
[304, {}, '']
else
[200, { 'Last-Modified' => header }, increment_counter]
end
end
get '/etag' do
settings.counter += 1
tag = settings.counter > 2 ? '1' : '2'
if env['HTTP_IF_NONE_MATCH'] == tag
[304, { 'ETag' => tag, 'Cache-Control' => 'max-age=200', 'Date' => Time.now.httpdate, 'Expires' => (Time.now + 200).httpdate, 'Vary' => '*' }, '']
else
[200, { 'ETag' => tag, 'Cache-Control' => 'max-age=0', 'Date' => settings.yesterday, 'Expires' => Time.now.httpdate, 'Vary' => 'Accept' }, increment_counter]
end
end
get '/no_cache' do
[200, { 'Cache-Control' => 'max-age=200, no-cache', 'ETag' => settings.counter.to_s }, increment_counter]
end
get '/vary' do
[200, { 'Cache-Control' => 'max-age=50', 'Vary' => 'User-Agent' }, increment_counter]
end
get '/vary-wildcard' do
[200, { 'Cache-Control' => 'max-age=50', 'Vary' => '*' }, increment_counter]
end
# Increments the 'requests' counter to act as a newly processed response.
def increment_counter
(settings.requests += 1).to_s
end
end