Sha256: 2828883dfbc691a28f478096cda9b5f427080ebc43103bc210e5c0ac8ead9c93

Contents?: true

Size: 1.58 KB

Versions: 10

Compression:

Stored size: 1.58 KB

Contents

require File.expand_path('../../../spec_helper', __FILE__)

RSpec.describe Wechat::Api::Client do
  subject { described_class.new('APPID', 'APPSECRET') }

  before do
    subject.logger.level = Logger::DEBUG
  end

  it :refresh do
    token_body = '{"access_token":"ACCESS_TOKEN","expires_in":7200}'
    stub_request(:get, /token/).to_return(body: token_body)
    expect(subject.refresh).not_to be_nil
  end

  it :refresh_error do
    token_body = '{"errcode": 40013}'
    stub_request(:get, /token/).to_return(body: token_body)
    expect { subject.refresh }.to raise_error(Wechat::Api::ResponseError)
  end

  it :refresh_token_error do
    token_body = '{"errcode": 40001}'
    stub_request(:get, /token/).to_return(body: token_body)
    expect { subject.refresh }
      .to raise_error(Wechat::Api::AccessTokenExpiredError)
  end

  it :access_token do
    token_body = '{"access_token":"ACCESS_TOKEN","expires_in":7200}'
    expect(File).to receive(:read).and_return(token_body)
    expect(subject.access_token).to eq('ACCESS_TOKEN')
  end

  it :get_twice do
    expect(subject).to receive(:access_token).twice.and_return('Token')
    expect(subject).to receive(:refresh).and_return('Token')
    stub_request(:get, /some/)
      .with(query: { access_token: 'Token' })
      .to_return(body: '{"errcode": 40001}').then
      .to_return(body: '{}')
    subject.get 'some'
  end

  it :post do
    expect(subject).to receive(:access_token).and_return('Token')
    stub_request(:post, /some/)
      .with(query: { access_token: 'Token' })
      .to_return(body: '{}')

    subject.post 'some', '{}'
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
wechat-api-0.4.3 spec/wechat/api/client_spec.rb
wechat-api-0.4.2 spec/wechat/api/client_spec.rb
wechat-api-0.4.1 spec/wechat/api/client_spec.rb
wechat-api-0.4.0 spec/wechat/api/client_spec.rb
wechat-api-0.3.0 spec/wechat/api/client_spec.rb
wechat-api-0.2.1 spec/wechat/api/client_spec.rb
wechat-api-0.2.0 spec/wechat/api/client_spec.rb
wechat-api-0.1.3 spec/wechat/api/client_spec.rb
wechat-api-0.1.1 spec/wechat/api/client_spec.rb
wechat-api-0.1.0 spec/wechat/api/client_spec.rb