Post on 10-May-2015
description
Testando a integração com APIs e serviços
externosDicas sobre como testar integrações.
@rafaelsshttp://rafaelss.com - http://softa.com.br
1
Ter que esperar uma suite de testes gigante
fazer todos os requests necessários ao rodar os testes é
muito ruim
11
“OmniAuth is an authentication framework that separates the concept of authentication from the concept of identity, providing simple
hooks for any application to have one or multiple authentication providers for a user”
https://rubygems.org/gems/omniauth
OmniAuth
14
1 # config/initializers/omniauth.rb 2 Rails.application.config.middleware.use OmniAuth::Builder do 3 provider :twitter, "key", "secret" 4 provider :facebook, "key", "secret", :scope => "email" 5 provider :linked_in, "key", "secret" 6 end 7 8 # test/test_helper.rb 9 OmniAuth.config.test_mode = true10 11 OmniAuth.config.mock_auth[:twitter] = {12 'uid' => '123545'13 # etc.14 }15 16 # test/functional/signup_test.rb17 test "..." do18 get "/auth/twitter"19 # ...20 end21 22 # test/functional/anything_test.rb23 setup do24 OmniAuth.config.add_mock(:facebook, :uid => '54321')25 end
15
WebMock
“WebMock allows stubbing HTTP requests and setting expectations on HTTP requests”
https://rubygems.org/gems/webmock
19
1 stub_request( 2 :get, 3 "https://graph.facebook.com/oauth/access_token" 4 ).with( 5 :query => { 6 :client_id => "client id", 7 :client_secret => "client secret", 8 :grant_type => "client_credentials" 9 }10 ).to_return(11 :body => "access_token=a1b2c3du4e5"12 )13 14 stub_request(15 :post,16 "https://api.twitter.com/1/statuses/update.json"17 ).with(18 :query => {19 :status => "hello #rsonrails"20 }21 ).to_return(22 :status => 200,23 :body => "{\"id\":83174481385556480}"24 )25 26 stub_request(27 :put,28 /s3\.amazonaws\.com/29 )
20
1 Twitter2 .should_receive(:update)3 .with("hello @rsonrails")4 .and_return('{"id":"1231313121313"}')
22
WebMock::NetConnectNotAllowedError: Real HTTP connections are disabled. Unregistered request: GET http://graph.facebook.com:443/oauth/access_token?client_id=112121312&client_secret=56757657655&grant_type=client_credentials with headers {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}
You can stub this request with the following snippet:
stub_request(:get, "http://graph.facebook.com:443/oauth/access_token?client_id=112121312&client_secret=56757657655&grant_type=client_credentials"). with(:headers => {'Accept'=>'*/*', 'User-Agent'=>'Ruby'}). to_return(:status => 200, :body => "", :headers => {})
24
Apigee
Free API tools for developers and providers
https://apigee.com/console
28
VCR
“VCR provides a simple API to record and replay your test suite's HTTP interactions. It works with a variety of HTTP client libraries,
HTTP stubbing libraries and testing frameworks”
https://rubygems.org/gems/vcr
31
1 # test/test_helper.rb 2 VCR.config do |c| 3 c.cassette_library_dir = 'fixtures/vcr_cassettes' 4 c.stub_with :webmock 5 end 6 7 # test/integration/signup_test.rb 8 VCR.use_cassette('facebook_access_token') do 9 uri = URI('https://graph.facebook.com/oauth/access_token')10 response = Net::HTTP.get_response(uri)11 assert_match /access_token=.+/, response.body12 end
32
1 A_WEEK = 7 * 24 * 60 * 602 3 VCR.use_cassette('my_cassette', :re_record_interval => A_WEEK) do4 # make an HTTP request5 end
36
“Replaces Net::HTTP with a subclass that routes all requests to a Rack application”
https://rubygems.org/gems/artifice
Artifice
37
1 # test/support/signup_app.rb 2 class SignupApp 3 def call(env) 4 if env["PATH_INFO"] == "/oauth/access_token" 5 [200, {}, "access_token=a1b2c3d4e5"] 6 end 7 end 8 end 9 10 # test/integration/signup_test.rb11 Artifice.activate_with(SignupApp) do12 uri = URI('https://graph.facebook.com/oauth/access_token')13 response = Net::HTTP.get_response(uri)14 assert_match /access_token=.+/, response.body15 end
38