Skip to content

Instantly share code, notes, and snippets.

@jhjguxin
Created December 19, 2013 12:45
Show Gist options
  • Save jhjguxin/8038592 to your computer and use it in GitHub Desktop.
Save jhjguxin/8038592 to your computer and use it in GitHub Desktop.
how oauth2 stubbing API Request with faraday

How auth2 test with faraday

resources

the problems

 let!(:error_value) {'invalid_token'}
  let!(:error_description_value) {'bad bad token'}

  subject do
    OAuth2::Client.new('abc', 'def', :site => 'https://api.example.com') do |builder|
      builder.adapter :test do |stub|
        stub.get('/success')      {|env| [200, {'Content-Type' => 'text/awesome'}, 'yay']}
        stub.get('/reflect')      {|env| [200, {}, env[:body]]}
        stub.post('/reflect')     {|env| [200, {}, env[:body]]}
        # skip
      end
    end
  end

then you can use those stub request by bellow:

  describe "#request" do
    it "works with a null response body" do
      expect(subject.request(:get, 'empty_get').body).to eq('')
    end

    it "returns on a successful response" do
      response = subject.request(:get, '/success')
      expect(response.body).to eq('yay')
      expect(response.status).to eq(200)
      expect(response.headers).to eq({'Content-Type' => 'text/awesome'})
    end

    it "posts a body" do
      response = subject.request(:post, '/reflect', :body => 'foo=bar')
      expect(response.body).to eq('foo=bar')
    end

    # skip
  end

follow the upon result, I try

  let(:client) { OpenWeixin::Client.from_hash(:access_token => "ACCESS_TOKEN", :expires_in => 7200) }

  let(:user_info) do
    MultiJson.encode({
      openid: "OPENID",
      nickname: "NICKNAME",
      sex: "1",
      province: "PROVINCE",
      city: "CITY"
    })
  end

  let(:snsapi) do
    OpenWeixin::Interface::Snsapi.new(client) do |builder|
      builder.request :url_encoded
      builder.adapter :test do |stub|
        stub.get('/sns/userinfo') { [200, {}, user_info] }
      end
    end
  end

and then use upon stub request

  context "#userinfo" do
    it "return user info"do
      expect(snsapi.userinfo.body).to eq(user_info)
    end
  end

throw bellow exception

  1) OpenWeixin::Interface::Snsapi#userinfo return user info
     Failure/Error: expect(snsapi.userinfo.body).to eq(user_info)
     Faraday::Adapter::Test::Stubs::NotFound:
       no stubbed request for get /sns/userinfo?access_token=ACCESS_TOKEN&openid=12341432134&scope=snsapi_base

and change to bellow will pass the test

  let(:snsapi_connect) do
    snsapi_connect = Faraday.new do |builder|
      builder.request :url_encoded
      builder.adapter :test do |stub|
        stub.get("/sns/userinfo") { [200, {}, user_info] }
        stub.get("/great") { [200, {}, "hellow"] }
      end
    end
  end
  let(:snsapi) do
    client.snsapi.stub(:userinfo) { snsapi_connect.get("/sns/userinfo") }
    client.snsapi
  end

  it "snsapi_connect can great" do
    expect(snsapi_connect.get("/great").body).to eq("hellow")
  end

  context "#userinfo" do
    it "return user info"do
      expect(snsapi.userinfo.body).to eq(user_info)
    end
  end

and check OAuth2::Client class we found

    # The Faraday connection object
    def connection
      @connection ||= begin
        conn = Faraday.new(site, options[:connection_opts])
        conn.build do |b|
          options[:connection_build].call(b)
        end if options[:connection_build]
        conn
      end
    end

so we can and must define all http stub inside 'client' instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment