test/client_test.rb in transprt-0.2.1 vs test/client_test.rb in transprt-0.2.2

- old
+ new

@@ -1,10 +1,9 @@ require 'test_helper' require 'pp' class ClientTest < Minitest::Test - def setup WebMock.reset! @client = Transprt::Client.new end @@ -16,12 +15,12 @@ assert locations.length == 1 first_location = locations.first - assert first_location['id'] == "008501008" - assert first_location['name'] == "Genève" + assert first_location['id'] == '008501008' + assert first_location['name'] == 'Genève' end def test_connections stub_response('client_test_test_connections.json') @@ -36,18 +35,43 @@ def test_stationboard skip 'feel free to add a test for Client#stationboard' end def test_escaping - # This makes sure the umlaut below gets escaped, if not we'll see an URI::InvalidURIError - stub_request(:get, /.*/).to_return(status: 200, body: {'connections': nil}.to_json) + stub_request(:get, /.*/).to_return( + status: 200, + body: { connections: nil }.to_json) - connections = @client.connections from: 'Lausanne', to: 'Zürich' + # The following line fails with an URI::InvalidURIError + # should the umlaut in 'Zurich' not get escaped. + @client.connections from: 'Lausanne', to: 'Zürich' end - def stub_response(name, url=/.*/, method=:get) - # Uncomment lines below should you feel the urge to test against the live API - # as the stubbing isn't very thorough as of now. (e.g. URLs requested aren't checked) + def test_rate_limit + request_count = 0 + + stub_request(:get, /.*/).to_return do + request_count += 1 + + if request_count == 1 # First request fails. + # Mock "rate limit hit" response code + { status: 429, + headers: { 'X-Rate-Limit-Reset' => Time.now.to_i.to_s } } + elsif request_count == 2 # Second request succeeds. + { status: 200, body: { connections: nil }.to_json } + else + raise "Did not expect request_count to be #{request_count}" + end + end + + @client.connections from: 'Lausanne', to: 'Bern' + assert request_count == 2 + end + + def stub_response(name, url = /.*/, method = :get) + # Uncomment lines below should you feel the urge to test against the live + # API as the stubbing isn't very thorough as of now. (e.g. URLs requested + # aren't checked) # WebMock.allow_net_connect! # return dirname = File.dirname(__FILE__)