/
net_http_spec.rb
56 lines (48 loc) · 1.71 KB
/
net_http_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
require File.join(File.dirname(File.expand_path(__FILE__)), 'spec_helper')
describe Net::HTTP do
##############################################################################
# Setup
##############################################################################
before do
SlowWeb.reset
FakeWeb.allow_net_connect = false
end
##############################################################################
# Tests
##############################################################################
it 'should show limit exceeded' do
FakeWeb.register_uri(:get, 'http://github.com', :body => 'foo')
SlowWeb.limit('github.com', 3, 60)
open('http://github.com')
open('http://github.com')
open('http://github.com')
SlowWeb.limit_exceeded?('github.com').should be_true
end
it 'should not show limit exceeded after waiting' do
FakeWeb.register_uri(:get, 'http://github.com', :body => 'foo')
SlowWeb.limit('github.com', 3, 1)
open('http://github.com')
open('http://github.com')
open('http://github.com')
sleep(1)
SlowWeb.limit_exceeded?('github.com').should be_false
end
it 'should wait for additional requests' do
FakeWeb.register_uri(:get, 'http://github.com', :body => 'foo')
SlowWeb.limit('github.com', 3, 1)
t = Time.now
open('http://github.com')
open('http://github.com')
open('http://github.com')
open('http://github.com')
(Time.now-t).should > 1
end
it 'should ignore unlimited hosts' do
FakeWeb.register_uri(:get, 'http://google.com', :body => 'bar')
SlowWeb.limit('github.com', 3, 60)
open('http://google.com')
open('http://google.com')
open('http://google.com')
open('http://google.com')
end
end