This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Faraday::Request::Authorization do
|
||||
let(:conn) do
|
||||
Faraday.new do |b|
|
||||
b.request :authorization, auth_type, *auth_config
|
||||
b.adapter :test do |stub|
|
||||
stub.get('/auth-echo') do |env|
|
||||
[200, {}, env[:request_headers]['Authorization']]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
shared_examples 'does not interfere with existing authentication' do
|
||||
context 'and request already has an authentication header' do
|
||||
let(:response) { conn.get('/auth-echo', nil, authorization: 'OAuth oauth_token') }
|
||||
|
||||
it 'does not interfere with existing authorization' do
|
||||
expect(response.body).to eq('OAuth oauth_token')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
let(:response) { conn.get('/auth-echo') }
|
||||
|
||||
describe 'basic_auth' do
|
||||
let(:auth_type) { :basic }
|
||||
|
||||
context 'when passed correct params' do
|
||||
let(:auth_config) { %w[aladdin opensesame] }
|
||||
|
||||
it { expect(response.body).to eq('Basic YWxhZGRpbjpvcGVuc2VzYW1l') }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
|
||||
context 'when passed very long values' do
|
||||
let(:auth_config) { ['A' * 255, ''] }
|
||||
|
||||
it { expect(response.body).to eq("Basic #{'QUFB' * 85}Og==") }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
end
|
||||
|
||||
describe 'authorization' do
|
||||
let(:auth_type) { :Bearer }
|
||||
|
||||
context 'when passed a string' do
|
||||
let(:auth_config) { ['custom'] }
|
||||
|
||||
it { expect(response.body).to eq('Bearer custom') }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
|
||||
context 'when passed a proc' do
|
||||
let(:auth_config) { [-> { 'custom_from_proc' }] }
|
||||
|
||||
it { expect(response.body).to eq('Bearer custom_from_proc') }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
|
||||
context 'when passed a callable' do
|
||||
let(:callable) { double('Callable Authorizer', call: 'custom_from_callable') }
|
||||
let(:auth_config) { [callable] }
|
||||
|
||||
it { expect(response.body).to eq('Bearer custom_from_callable') }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
|
||||
context 'with an argument' do
|
||||
let(:response) { conn.get('/auth-echo', nil, 'middle' => 'crunchy surprise') }
|
||||
|
||||
context 'when passed a proc' do
|
||||
let(:auth_config) { [proc { |env| "proc #{env.request_headers['middle']}" }] }
|
||||
|
||||
it { expect(response.body).to eq('Bearer proc crunchy surprise') }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
|
||||
context 'when passed a lambda' do
|
||||
let(:auth_config) { [->(env) { "lambda #{env.request_headers['middle']}" }] }
|
||||
|
||||
it { expect(response.body).to eq('Bearer lambda crunchy surprise') }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
|
||||
context 'when passed a callable with an argument' do
|
||||
let(:callable) do
|
||||
Class.new do
|
||||
def call(env)
|
||||
"callable #{env.request_headers['middle']}"
|
||||
end
|
||||
end.new
|
||||
end
|
||||
let(:auth_config) { [callable] }
|
||||
|
||||
it { expect(response.body).to eq('Bearer callable crunchy surprise') }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
end
|
||||
|
||||
context 'when passed too many arguments' do
|
||||
let(:auth_config) { %w[baz foo] }
|
||||
|
||||
it { expect { response }.to raise_error(ArgumentError) }
|
||||
|
||||
include_examples 'does not interfere with existing authentication'
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,74 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Faraday::Request::Instrumentation do
|
||||
class FakeInstrumenter
|
||||
attr_reader :instrumentations
|
||||
|
||||
def initialize
|
||||
@instrumentations = []
|
||||
end
|
||||
|
||||
def instrument(name, env)
|
||||
@instrumentations << [name, env]
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
let(:config) { {} }
|
||||
let(:options) { Faraday::Request::Instrumentation::Options.from config }
|
||||
let(:instrumenter) { FakeInstrumenter.new }
|
||||
let(:conn) do
|
||||
Faraday.new do |f|
|
||||
f.request :instrumentation, config.merge(instrumenter: instrumenter)
|
||||
f.adapter :test do |stub|
|
||||
stub.get '/' do
|
||||
[200, {}, 'ok']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it { expect(options.name).to eq('request.faraday') }
|
||||
it 'defaults to ActiveSupport::Notifications' do
|
||||
res = options.instrumenter
|
||||
rescue NameError => e
|
||||
expect(e.to_s).to match('ActiveSupport')
|
||||
else
|
||||
expect(res).to eq(ActiveSupport::Notifications)
|
||||
end
|
||||
|
||||
it 'instruments with default name' do
|
||||
expect(instrumenter.instrumentations.size).to eq(0)
|
||||
|
||||
res = conn.get '/'
|
||||
expect(res.body).to eq('ok')
|
||||
expect(instrumenter.instrumentations.size).to eq(1)
|
||||
|
||||
name, env = instrumenter.instrumentations.first
|
||||
expect(name).to eq('request.faraday')
|
||||
expect(env[:url].path).to eq('/')
|
||||
end
|
||||
|
||||
context 'with custom name' do
|
||||
let(:config) { { name: 'custom' } }
|
||||
|
||||
it { expect(options.name).to eq('custom') }
|
||||
it 'instruments with custom name' do
|
||||
expect(instrumenter.instrumentations.size).to eq(0)
|
||||
|
||||
res = conn.get '/'
|
||||
expect(res.body).to eq('ok')
|
||||
expect(instrumenter.instrumentations.size).to eq(1)
|
||||
|
||||
name, env = instrumenter.instrumentations.first
|
||||
expect(name).to eq('custom')
|
||||
expect(env[:url].path).to eq('/')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with custom instrumenter' do
|
||||
let(:config) { { instrumenter: :custom } }
|
||||
|
||||
it { expect(options.instrumenter).to eq(:custom) }
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,199 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
RSpec.describe Faraday::Request::Json do
|
||||
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }) }
|
||||
|
||||
def process(body, content_type = nil)
|
||||
env = { body: body, request_headers: Faraday::Utils::Headers.new }
|
||||
env[:request_headers]['content-type'] = content_type if content_type
|
||||
middleware.call(Faraday::Env.from(env)).env
|
||||
end
|
||||
|
||||
def result_body
|
||||
result[:body]
|
||||
end
|
||||
|
||||
def result_type
|
||||
result[:request_headers]['content-type']
|
||||
end
|
||||
|
||||
context 'no body' do
|
||||
let(:result) { process(nil) }
|
||||
|
||||
it "doesn't change body" do
|
||||
expect(result_body).to be_nil
|
||||
end
|
||||
|
||||
it "doesn't add content type" do
|
||||
expect(result_type).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'empty body' do
|
||||
let(:result) { process('') }
|
||||
|
||||
it "doesn't change body" do
|
||||
expect(result_body).to be_empty
|
||||
end
|
||||
|
||||
it "doesn't add content type" do
|
||||
expect(result_type).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'string body' do
|
||||
let(:result) { process('{"a":1}') }
|
||||
|
||||
it "doesn't change body" do
|
||||
expect(result_body).to eq('{"a":1}')
|
||||
end
|
||||
|
||||
it 'adds content type' do
|
||||
expect(result_type).to eq('application/json')
|
||||
end
|
||||
end
|
||||
|
||||
context 'object body' do
|
||||
let(:result) { process(a: 1) }
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('{"a":1}')
|
||||
end
|
||||
|
||||
it 'adds content type' do
|
||||
expect(result_type).to eq('application/json')
|
||||
end
|
||||
end
|
||||
|
||||
context 'empty object body' do
|
||||
let(:result) { process({}) }
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('{}')
|
||||
end
|
||||
end
|
||||
|
||||
context 'true body' do
|
||||
let(:result) { process(true) }
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('true')
|
||||
end
|
||||
|
||||
it 'adds content type' do
|
||||
expect(result_type).to eq('application/json')
|
||||
end
|
||||
end
|
||||
|
||||
context 'false body' do
|
||||
let(:result) { process(false) }
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('false')
|
||||
end
|
||||
|
||||
it 'adds content type' do
|
||||
expect(result_type).to eq('application/json')
|
||||
end
|
||||
end
|
||||
|
||||
context 'object body with json type' do
|
||||
let(:result) { process({ a: 1 }, 'application/json; charset=utf-8') }
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('{"a":1}')
|
||||
end
|
||||
|
||||
it "doesn't change content type" do
|
||||
expect(result_type).to eq('application/json; charset=utf-8')
|
||||
end
|
||||
end
|
||||
|
||||
context 'object body with vendor json type' do
|
||||
let(:result) { process({ a: 1 }, 'application/vnd.myapp.v1+json; charset=utf-8') }
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('{"a":1}')
|
||||
end
|
||||
|
||||
it "doesn't change content type" do
|
||||
expect(result_type).to eq('application/vnd.myapp.v1+json; charset=utf-8')
|
||||
end
|
||||
end
|
||||
|
||||
context 'object body with incompatible type' do
|
||||
let(:result) { process({ a: 1 }, 'application/xml; charset=utf-8') }
|
||||
|
||||
it "doesn't change body" do
|
||||
expect(result_body).to eq(a: 1)
|
||||
end
|
||||
|
||||
it "doesn't change content type" do
|
||||
expect(result_type).to eq('application/xml; charset=utf-8')
|
||||
end
|
||||
end
|
||||
|
||||
context 'with encoder' do
|
||||
let(:encoder) do
|
||||
double('Encoder').tap do |e|
|
||||
allow(e).to receive(:dump) { |s, opts| JSON.generate(s, opts) }
|
||||
end
|
||||
end
|
||||
|
||||
let(:result) { process(a: 1) }
|
||||
|
||||
context 'when encoder is passed as object' do
|
||||
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }, { encoder: encoder }) }
|
||||
|
||||
it 'calls specified JSON encoder\'s dump method' do
|
||||
expect(encoder).to receive(:dump).with({ a: 1 })
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('{"a":1}')
|
||||
end
|
||||
|
||||
it 'adds content type' do
|
||||
expect(result_type).to eq('application/json')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when encoder is passed as an object-method pair' do
|
||||
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }, { encoder: [encoder, :dump] }) }
|
||||
|
||||
it 'calls specified JSON encoder' do
|
||||
expect(encoder).to receive(:dump).with({ a: 1 })
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('{"a":1}')
|
||||
end
|
||||
|
||||
it 'adds content type' do
|
||||
expect(result_type).to eq('application/json')
|
||||
end
|
||||
end
|
||||
|
||||
context 'when encoder is not passed' do
|
||||
let(:middleware) { described_class.new(->(env) { Faraday::Response.new(env) }) }
|
||||
|
||||
it 'calls JSON.generate' do
|
||||
expect(JSON).to receive(:generate).with({ a: 1 })
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
it 'encodes body' do
|
||||
expect(result_body).to eq('{"a":1}')
|
||||
end
|
||||
|
||||
it 'adds content type' do
|
||||
expect(result_type).to eq('application/json')
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,93 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'stringio'
|
||||
|
||||
RSpec.describe Faraday::Request::UrlEncoded do
|
||||
let(:conn) do
|
||||
Faraday.new do |b|
|
||||
b.request :url_encoded
|
||||
b.adapter :test do |stub|
|
||||
stub.post('/echo') do |env|
|
||||
posted_as = env[:request_headers]['Content-Type']
|
||||
body = env[:body]
|
||||
if body.respond_to?(:read)
|
||||
body = body.read
|
||||
end
|
||||
[200, { 'Content-Type' => posted_as }, body]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it 'does nothing without payload' do
|
||||
response = conn.post('/echo')
|
||||
expect(response.headers['Content-Type']).to be_nil
|
||||
expect(response.body.empty?).to be_truthy
|
||||
end
|
||||
|
||||
it 'ignores custom content type' do
|
||||
response = conn.post('/echo', { some: 'data' }, 'content-type' => 'application/x-foo')
|
||||
expect(response.headers['Content-Type']).to eq('application/x-foo')
|
||||
expect(response.body).to eq(some: 'data')
|
||||
end
|
||||
|
||||
it 'works with no headers' do
|
||||
response = conn.post('/echo', fruit: %w[apples oranges])
|
||||
expect(response.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
|
||||
expect(response.body).to eq('fruit%5B%5D=apples&fruit%5B%5D=oranges')
|
||||
end
|
||||
|
||||
it 'works with with headers' do
|
||||
response = conn.post('/echo', { 'a' => 123 }, 'content-type' => 'application/x-www-form-urlencoded')
|
||||
expect(response.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
|
||||
expect(response.body).to eq('a=123')
|
||||
end
|
||||
|
||||
it 'works with nested params' do
|
||||
response = conn.post('/echo', user: { name: 'Mislav', web: 'mislav.net' })
|
||||
expect(response.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
|
||||
expected = { 'user' => { 'name' => 'Mislav', 'web' => 'mislav.net' } }
|
||||
expect(Faraday::Utils.parse_nested_query(response.body)).to eq(expected)
|
||||
end
|
||||
|
||||
it 'works with non nested params' do
|
||||
response = conn.post('/echo', dimensions: %w[date location]) do |req|
|
||||
req.options.params_encoder = Faraday::FlatParamsEncoder
|
||||
end
|
||||
expect(response.headers['Content-Type']).to eq('application/x-www-form-urlencoded')
|
||||
expected = { 'dimensions' => %w[date location] }
|
||||
expect(Faraday::Utils.parse_query(response.body)).to eq(expected)
|
||||
expect(response.body).to eq('dimensions=date&dimensions=location')
|
||||
end
|
||||
|
||||
it 'works with unicode' do
|
||||
err = capture_warnings do
|
||||
response = conn.post('/echo', str: 'eé cç aã aâ')
|
||||
expect(response.body).to eq('str=e%C3%A9+c%C3%A7+a%C3%A3+a%C3%A2')
|
||||
end
|
||||
expect(err.empty?).to be_truthy
|
||||
end
|
||||
|
||||
it 'works with nested keys' do
|
||||
response = conn.post('/echo', 'a' => { 'b' => { 'c' => ['d'] } })
|
||||
expect(response.body).to eq('a%5Bb%5D%5Bc%5D%5B%5D=d')
|
||||
end
|
||||
|
||||
it 'works with files' do
|
||||
response = conn.post('/echo', StringIO.new('str=apple'))
|
||||
expect(response.body).to eq('str=apple')
|
||||
end
|
||||
|
||||
context 'customising default_space_encoding' do
|
||||
around do |example|
|
||||
Faraday::Utils.default_space_encoding = '%20'
|
||||
example.run
|
||||
Faraday::Utils.default_space_encoding = nil
|
||||
end
|
||||
|
||||
it 'uses the custom character to encode spaces' do
|
||||
response = conn.post('/echo', str: 'apple banana')
|
||||
expect(response.body).to eq('str=apple%20banana')
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user