|
1 | 1 | require 'spec_helper' |
2 | 2 | require 'ostruct' |
3 | 3 |
|
| 4 | +WebMock.enable! |
| 5 | + |
4 | 6 | describe 'Intercom::Request' do |
5 | 7 | it 'raises an error when a html error page rendered' do |
6 | 8 | response = OpenStruct.new(:code => 500) |
|
14 | 16 | proc {req.parse_body('<html>somethjing</html>', response)}.must_raise(Intercom::RateLimitExceeded) |
15 | 17 | end |
16 | 18 |
|
| 19 | + describe 'Intercom::Client' do |
| 20 | + let (:client) { Intercom::Client.new(token: 'foo', handle_rate_limit: true) } |
| 21 | + let (:uri) {"https://api.intercom.io/users"} |
| 22 | + |
| 23 | + it 'should have handle_rate_limit set' do |
| 24 | + client.handle_rate_limit.must_equal(true) |
| 25 | + end |
| 26 | + |
| 27 | + it 'should call sleep for rate limit error three times' do |
| 28 | + # Use webmock to mock the HTTP request |
| 29 | + stub_request(:any, uri).\ |
| 30 | + to_return(status: [429, "Too Many Requests"], headers: { 'X-RateLimit-Reset' => Time.now.utc + 10 }) |
| 31 | + req = Intercom::Request.get(uri, "") |
| 32 | + req.handle_rate_limit=true |
| 33 | + req.expects(:sleep).times(3).with(any_parameters) |
| 34 | + req.execute(target_base_url=uri, username: "ted", secret: "") |
| 35 | + end |
| 36 | + |
| 37 | + it 'should not call sleep for rate limit error' do |
| 38 | + # Use webmock to mock the HTTP request |
| 39 | + stub_request(:any, uri).\ |
| 40 | + to_return(status: [200, "OK"], headers: { 'X-RateLimit-Reset' => Time.now.utc + 10 }) |
| 41 | + req = Intercom::Request.get(uri, "") |
| 42 | + req.handle_rate_limit=true |
| 43 | + req.expects(:sleep).never.with(any_parameters) |
| 44 | + req.execute(target_base_url=uri, username: "ted", secret: "") |
| 45 | + end |
| 46 | + |
| 47 | + it 'should call sleep for rate limit error just once' do |
| 48 | + # Use webmock to mock the HTTP request |
| 49 | + stub_request(:any, uri).\ |
| 50 | + to_return(status: [429, "Too Many Requests"], headers: { 'X-RateLimit-Reset' => Time.now.utc + 10 }).\ |
| 51 | + then.to_return(status: [200, "OK"]) |
| 52 | + req = Intercom::Request.get(uri, "") |
| 53 | + req.handle_rate_limit=true |
| 54 | + req.expects(:sleep).with(any_parameters) |
| 55 | + req.execute(target_base_url=uri, username: "ted", secret: "") |
| 56 | + end |
| 57 | + |
| 58 | + end |
| 59 | + |
17 | 60 | it 'parse_body returns nil if decoded_body is nil' do |
18 | 61 | response = OpenStruct.new(:code => 500) |
19 | 62 | req = Intercom::Request.new('path/', 'GET') |
20 | | - req.parse_body(nil, response).must_equal(nil) |
| 63 | + assert_nil(req.parse_body(nil, response)) |
21 | 64 | end |
22 | 65 | end |
0 commit comments