-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_spec.rb
executable file
·64 lines (41 loc) · 1.35 KB
/
code_spec.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
describe Shorty::Code do
CODE_REGEX = /^[0-9a-zA-Z_]{6}$/
let(:shorty) { Shorty::Code.new }
it 'creates a specified short code' do
result = shorty.shorten_url('http://github.com/opensourceame', 'abc123')
expect(result).to eq 'abc123'
end
it 'creates a generated short code' do
result = shorty.shorten_url('http://www.redhotpawn.com')
expect(result).to match(CODE_REGEX)
end
it 'gets code for existing URL to increment hits' do
result = shorty.stats('abc123')
expect(result).to be_a(Hash)
expect(result['hits'].to_i).to eq 2
end
it 'tries to create a code for an existing URL' do
result = shorty.shorten_url('http://github.com/opensourceame')
expect(result).to eq 'abc123'
end
it 'tries to create a code that already exists' do
expect{
shorty.shorten_url('http://bbcgoodfood.com', 'abc123')
}.to raise_error('code exists')
end
it 'tries to shorten an invalid URL' do
expect {
shorty.shorten_url('eat_my_shorts', 'abc123')
}.to raise_error(ArgumentError)
end
it 'tries to create an invalid short code' do
expect{
shorty.shorten_url('http://www.urbandictionary.com', 'iShortYouNot!')
}.to raise_error(ArgumentError)
end
it 'tries to fetch a nonexistent short code' do
expect{
shorty.get('Where?')
}.to raise_error(IndexError)
end
end