forked from technoweenie/guillotine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_test.rb
252 lines (211 loc) · 7.41 KB
/
service_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
require File.expand_path('../helper', __FILE__)
module Guillotine
class ServiceTest < TestCase
def setup
@db = MemoryAdapter.new
@service = Service.new @db
end
def test_adding_a_link_returns_code
url = 'http://github.com'
status, head, body = @service.create(url)
assert_equal 201, status
assert code_url = head['Location']
code = code_url.gsub(/.*\//, '')
status, head, body = @service.get(code)
assert_equal 302, status
assert_equal url, head['Location']
end
def test_adding_a_link_with_query_param_returns_code
url = 'http://github.com?a=1'
status, head, body = @service.create(url)
assert_equal 201, status
assert code_url = head['Location']
code = code_url.gsub(/.*\//, '')
status, head, body = @service.get(code)
assert_equal 302, status
assert_equal url, head['Location']
end
def test_adding_duplicate_link_returns_same_code
url = 'http://github.com'
code = @db.add url
status, head, body = @service.create(url + '#a=1')
assert code_url = head['Location']
assert_equal code, code_url.gsub(/.*\//, '')
end
def test_adds_url_with_custom_code
url = 'http://github.com/abc'
status, head, body = @service.create(url, 'code')
assert code_url = head['Location']
assert_equal 'code', code_url
status, head, body = @service.get('code')
assert_equal 302, status
assert_equal url, head['Location']
end
def test_adds_url_with_custom_unicode
url = 'http://github.com/abc'
status, head, body = @service.create(url, '%E2%9C%88')
assert code_url = head['Location']
assert_match /^%E2%9C%88$/, code_url
status, head, body = @service.get("%E2%9C%88")
assert_equal 302, status
assert_equal url, head['Location']
end
def test_redirects_to_split_url
url = "http://abc.com\nhttp//def.com"
@db.hash['split'] = url
@db.urls[url] = 'split'
status, head, body = @service.get('split')
assert_equal "http://abc.comhttp//def.com", head['Location']
end
def test_clashing_urls_raises_error
code = @db.add 'http://github.com/123'
status, head, body = @service.create('http://github.com/456', code)
assert_equal 422, status
end
def test_add_without_url
status, head, body = @service.create(nil)
assert_equal 422, status
end
def test_add_url_with_linebreak
status, head, body = @service.create("https://abc.com\n")
assert_equal 'SWtBvQ', head['Location']
end
def test_adds_split_url
status, head, body = @service.create("https://abc.com\nhttp://abc.com")
assert_equal 'cb5CNA', head['Location']
assert_equal 'https://abc.comhttp//abc.com', @db.find('cb5CNA')
end
def test_rejects_non_http_urls
status, head, body = @service.create('ftp://abc.com')
assert_equal 422, status
end
def test_reject_shortened_url_from_other_domain
service = Service.new @db, 'abc.com'
status, head, body = service.create('http://github.com')
assert_equal 422, status
assert_match /must be from abc\.com/, body
status, head, body = service.create('http://abc.com/def')
assert_equal 201, status
end
def test_reject_shortened_url_from_other_domain_by_regex
service = Service.new @db, /abc\.com$/
status, head, body = service.create('http://github.com')
assert_equal 422, status
assert_match /must match \/abc\\.com/, body
status, head, body = service.create('http://abc.com/def')
assert_equal 201, status
status, head, body = service.create('http://www.abc.com/def')
assert_equal 201, status
end
def test_options_from_nil
options = Service::Options.from(nil)
assert_nil options.required_host
assert_nil options.strip_query
assert_nil options.strip_anchor
assert_nil options.length
assert_nil options.charset
assert_nil options.default_url
end
def test_options_from_empty_string
options = Service::Options.from("")
assert_nil options.required_host
assert_nil options.strip_query
assert_nil options.strip_anchor
assert_nil options.length
assert_nil options.charset
assert_nil options.default_url
end
def test_options_from_string
options = Service::Options.from("ok")
assert_equal "ok", options.required_host
assert_nil options.strip_query
assert_nil options.strip_anchor
assert_nil options.length
assert_nil options.charset
assert_nil options.default_url
end
def test_options_from_hash
options = Service::Options.from(
:required_host => "ok",
:strip_query => "a",
:strip_anchor => "b",
:length => "c",
:charset => "d",
:default_url => "e"
)
assert_equal "ok", options.required_host
assert_equal "a", options.strip_query
assert_equal "b", options.strip_anchor
assert_equal "c", options.length
assert_equal "d", options.charset
assert_equal "e", options.default_url
end
def test_options_from_options
options = Service::Options.from(
:required_host => "ok",
:strip_query => "a",
:strip_anchor => "b",
:length => "c",
:charset => "d",
:default_url => "e"
)
options = Service::Options.from(options)
assert_equal "ok", options.required_host
assert_equal "a", options.strip_query
assert_equal "b", options.strip_anchor
assert_equal "c", options.length
assert_equal "d", options.charset
assert_equal "e", options.default_url
end
def test_options_from_options_sub_class
klass = Class.new(Service::Options)
options = klass.from(
:required_host => "ok",
:strip_query => "a",
:strip_anchor => "b",
:length => "c",
:charset => "d",
:default_url => "e"
)
options = Service::Options.from(options)
assert_equal "ok", options.required_host
assert_equal "a", options.strip_query
assert_equal "b", options.strip_anchor
assert_equal "c", options.length
assert_equal "d", options.charset
assert_equal "e", options.default_url
end
def test_options_to_options_sub_class
klass = Class.new(Service::Options)
options = Service::Options.from(
:required_host => "ok",
:strip_query => "a",
:strip_anchor => "b",
:length => "c",
:charset => "d",
:default_url => "e"
)
options = klass.from(options)
assert_equal "ok", options.required_host
assert_equal "a", options.strip_query
assert_equal "b", options.strip_anchor
assert_equal "c", options.length
assert_equal "d", options.charset
assert_equal "e", options.default_url
end
def test_fixed_charset_code
@db = MemoryAdapter.new
length = 4
char_set = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
@service = Service.new @db, :length => length, :charset => char_set
url = 'http://github.com'
status, head, body = @service.create(url)
assert_equal 201, status
assert code_url = head['Location']
assert_equal 4, code_url.length
code_url.each_char do |c|
assert char_set.include?(c)
end
end
end
end