Skip to content

Commit

Permalink
Removing all sushi.com references and change to httpbingo.org (#1384)
Browse files Browse the repository at this point in the history
Fixes #1164
  • Loading branch information
JAugusto42 committed Jan 18, 2022
1 parent 9f80f87 commit 816d824
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 96 deletions.
16 changes: 8 additions & 8 deletions docs/middleware/response/logger.md
Expand Up @@ -17,12 +17,12 @@ It is highly customizable and allows to mask confidential information if necessa
### Basic Usage

```ruby
conn = Faraday.new(url: 'http://sushi.com') do |faraday|
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger # log requests and responses to $stdout
end

conn.get
# => INFO -- request: GET http://sushi.com/
# => INFO -- request: GET http://httpbingo.org/
# => DEBUG -- request: User-Agent: "Faraday v1.0.0"
# => INFO -- response: Status 301
# => DEBUG -- response: date: "Sun, 19 May 2019 16:05:40 GMT"
Expand All @@ -34,7 +34,7 @@ By default, the `Logger` middleware uses the Ruby `Logger.new($stdout)`.
You can customize it to use any logger you want by providing it when you add the middleware to the stack:

```ruby
conn = Faraday.new(url: 'http://sushi.com') do |faraday|
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger, MyLogger.new($stdout)
end
```
Expand All @@ -46,7 +46,7 @@ to log bodies as well, or disable headers logging if you need to. To do so, simp
when you add the middleware to the stack:

```ruby
conn = Faraday.new(url: 'http://sushi.com') do |faraday|
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger, nil, { headers: true, bodies: true }
end
```
Expand All @@ -58,14 +58,14 @@ Please note this only works with the default formatter.
You can filter sensitive information from Faraday logs using a regex matcher:

```ruby
conn = Faraday.new(url: 'http://sushi.com') do |faraday|
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger do | logger |
logger.filter(/(api_key=)([^&]+)/, '\1[REMOVED]')
end
end

conn.get('/', api_key: 'secret')
# => INFO -- request: GET http://sushi.com/?api_key=[REMOVED]
# => INFO -- request: GET http://httpbingo.org/?api_key=[REMOVED]
# => DEBUG -- request: User-Agent: "Faraday v1.0.0"
# => INFO -- response: Status 301
# => DEBUG -- response: date: "Sun, 19 May 2019 16:12:36 GMT"
Expand All @@ -77,7 +77,7 @@ By default, the `logger` middleware logs on the `info` log level. It is possible
the severity by providing the `log_level` option:

```ruby
conn = Faraday.new(url: 'http://sushi.com') do |faraday|
conn = Faraday.new(url: 'http://httpbingo.org') do |faraday|
faraday.response :logger, nil, { bodies: true, log_level: :debug }
end
```
Expand All @@ -103,7 +103,7 @@ class MyFormatter < Faraday::Logging::Formatter
end
end

conn = Faraday.new(url: 'http://sushi.com/api_key=s3cr3t') do |faraday|
conn = Faraday.new(url: 'http://httpbingo.org/api_key=s3cr3t') do |faraday|
faraday.response :logger, nil, formatter: MyFormatter
end
```
36 changes: 18 additions & 18 deletions examples/client_spec.rb
Expand Up @@ -12,10 +12,10 @@ def initialize(conn)
@conn = conn
end

def sushi(jname, params: {})
def httpbingo(jname, params: {})
res = @conn.get("/#{jname}", params)
data = JSON.parse(res.body)
data['name']
data['origin']
end
end

Expand All @@ -24,60 +24,60 @@ def sushi(jname, params: {})
let(:conn) { Faraday.new { |b| b.adapter(:test, stubs) } }
let(:client) { Client.new(conn) }

it 'parses name' do
stubs.get('/ebi') do |env|
it 'parses origin' do
stubs.get('/ip') do |env|
# optional: you can inspect the Faraday::Env
expect(env.url.path).to eq('/ebi')
expect(env.url.path).to eq('/ip')
[
200,
{ 'Content-Type': 'application/javascript' },
'{"name": "shrimp"}'
'{"origin": "127.0.0.1"}'
]
end

# uncomment to trigger stubs.verify_stubbed_calls failure
# stubs.get('/unused') { [404, {}, ''] }

expect(client.sushi('ebi')).to eq('shrimp')
expect(client.httpbingo('ip')).to eq('127.0.0.1')
stubs.verify_stubbed_calls
end

it 'handles 404' do
stubs.get('/ebi') do
stubs.get('/api') do
[
404,
{ 'Content-Type': 'application/javascript' },
'{}'
]
end
expect(client.sushi('ebi')).to be_nil
expect(client.httpbingo('api')).to be_nil
stubs.verify_stubbed_calls
end

it 'handles exception' do
stubs.get('/ebi') do
stubs.get('/api') do
raise Faraday::ConnectionFailed
end

expect { client.sushi('ebi') }.to raise_error(Faraday::ConnectionFailed)
expect { client.httpbingo('api') }.to raise_error(Faraday::ConnectionFailed)
stubs.verify_stubbed_calls
end

context 'When the test stub is run in strict_mode' do
let(:stubs) { Faraday::Adapter::Test::Stubs.new(strict_mode: true) }

it 'verifies the all parameter values are identical' do
stubs.get('/ebi?abc=123') do
stubs.get('/api?abc=123') do
[
200,
{ 'Content-Type': 'application/javascript' },
'{"name": "shrimp"}'
'{"origin": "127.0.0.1"}'
]
end

# uncomment to raise Stubs::NotFound
# expect(client.sushi('ebi', params: { abc: 123, foo: 'Kappa' })).to eq('shrimp')
expect(client.sushi('ebi', params: { abc: 123 })).to eq('shrimp')
# expect(client.httpbingo('api', params: { abc: 123, foo: 'Kappa' })).to eq('127.0.0.1')
expect(client.httpbingo('api', params: { abc: 123 })).to eq('127.0.0.1')
stubs.verify_stubbed_calls
end
end
Expand All @@ -86,11 +86,11 @@ def sushi(jname, params: {})
let(:conn) { Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) { |b| b.adapter(:test, stubs) } }

it 'handles the same multiple URL parameters' do
stubs.get('/ebi?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"name": "shrimp"}'] }
stubs.get('/api?a=x&a=y&a=z') { [200, { 'Content-Type' => 'application/json' }, '{"origin": "127.0.0.1"}'] }

# uncomment to raise Stubs::NotFound
# expect(client.sushi('ebi', params: { a: %w[x y] })).to eq('shrimp')
expect(client.sushi('ebi', params: { a: %w[x y z] })).to eq('shrimp')
# expect(client.httpbingo('api', params: { a: %w[x y] })).to eq('127.0.0.1')
expect(client.httpbingo('api', params: { a: %w[x y z] })).to eq('127.0.0.1')
stubs.verify_stubbed_calls
end
end
Expand Down
42 changes: 21 additions & 21 deletions examples/client_test.rb
Expand Up @@ -13,38 +13,38 @@ def initialize(conn)
@conn = conn
end

def sushi(jname, params: {})
def httpbingo(jname, params: {})
res = @conn.get("/#{jname}", params)
data = JSON.parse(res.body)
data['name']
data['origin']
end
end

# Example API client test
class ClientTest < Test::Unit::TestCase
def test_sushi_name
def test_httpbingo_name
stubs = Faraday::Adapter::Test::Stubs.new
stubs.get('/ebi') do |env|
stubs.get('/api') do |env|
# optional: you can inspect the Faraday::Env
assert_equal '/ebi', env.url.path
assert_equal '/api', env.url.path
[
200,
{ 'Content-Type': 'application/javascript' },
'{"name": "shrimp"}'
'{"origin": "127.0.0.1"}'
]
end

# uncomment to trigger stubs.verify_stubbed_calls failure
# stubs.get('/unused') { [404, {}, ''] }

cli = client(stubs)
assert_equal 'shrimp', cli.sushi('ebi')
assert_equal '127.0.0.1', cli.httpbingo('api')
stubs.verify_stubbed_calls
end

def test_sushi_not_found
def test_httpbingo_not_found
stubs = Faraday::Adapter::Test::Stubs.new
stubs.get('/ebi') do
stubs.get('/api') do
[
404,
{ 'Content-Type': 'application/javascript' },
Expand All @@ -53,59 +53,59 @@ def test_sushi_not_found
end

cli = client(stubs)
assert_nil cli.sushi('ebi')
assert_nil cli.httpbingo('api')
stubs.verify_stubbed_calls
end

def test_sushi_exception
def test_httpbingo_exception
stubs = Faraday::Adapter::Test::Stubs.new
stubs.get('/ebi') do
stubs.get('/api') do
raise Faraday::ConnectionFailed
end

cli = client(stubs)
assert_raise Faraday::ConnectionFailed do
cli.sushi('ebi')
cli.httpbingo('api')
end
stubs.verify_stubbed_calls
end

def test_strict_mode
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
stubs.get('/ebi?abc=123') do
stubs.get('/api?abc=123') do
[
200,
{ 'Content-Type': 'application/javascript' },
'{"name": "shrimp"}'
'{"origin": "127.0.0.1"}'
]
end

cli = client(stubs)
assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123 })
assert_equal '127.0.0.1', cli.httpbingo('api', params: { abc: 123 })

# uncomment to raise Stubs::NotFound
# assert_equal 'shrimp', cli.sushi('ebi', params: { abc: 123, foo: 'Kappa' })
# assert_equal '127.0.0.1', cli.httpbingo('api', params: { abc: 123, foo: 'Kappa' })
stubs.verify_stubbed_calls
end

def test_non_default_params_encoder
stubs = Faraday::Adapter::Test::Stubs.new(strict_mode: true)
stubs.get('/ebi?a=x&a=y&a=z') do
stubs.get('/api?a=x&a=y&a=z') do
[
200,
{ 'Content-Type': 'application/javascript' },
'{"name": "shrimp"}'
'{"origin": "127.0.0.1"}'
]
end
conn = Faraday.new(request: { params_encoder: Faraday::FlatParamsEncoder }) do |builder|
builder.adapter :test, stubs
end

cli = Client.new(conn)
assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y z] })
assert_equal '127.0.0.1', cli.httpbingo('api', params: { a: %w[x y z] })

# uncomment to raise Stubs::NotFound
# assert_equal 'shrimp', cli.sushi('ebi', params: { a: %w[x y] })
# assert_equal '127.0.0.1', cli.httpbingo('api', params: { a: %w[x y] })
stubs.verify_stubbed_calls
end

Expand Down
14 changes: 7 additions & 7 deletions lib/faraday/connection.rb
Expand Up @@ -6,9 +6,9 @@ module Faraday
#
# @example
#
# conn = Faraday::Connection.new 'http://sushi.com'
# conn = Faraday::Connection.new 'http://httpbingo.org'
#
# # GET http://sushi.com/nigiri
# # GET http://httpbingo.org/nigiri
# conn.get 'nigiri'
# # => #<Faraday::Response>
#
Expand Down Expand Up @@ -349,11 +349,11 @@ def proxy=(new_value)
# @example
#
# conn = Faraday::Connection.new { ... }
# conn.url_prefix = "https://sushi.com/api"
# conn.url_prefix = "https://httpbingo.org/api"
# conn.scheme # => https
# conn.path_prefix # => "/api"
#
# conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
# conn.get("nigiri?page=2") # accesses https://httpbingo.org/api/nigiri
def url_prefix=(url, encoder = nil)
uri = @url_prefix = Utils.URI(url)
self.path_prefix = uri.path
Expand Down Expand Up @@ -395,15 +395,15 @@ def path_prefix=(value)
#
# @example
# conn = Faraday::Connection.new { ... }
# conn.url_prefix = "https://sushi.com/api?token=abc"
# conn.url_prefix = "https://httpbingo.org/api?token=abc"
# conn.scheme # => https
# conn.path_prefix # => "/api"
#
# conn.build_url("nigiri?page=2")
# # => https://sushi.com/api/nigiri?token=abc&page=2
# # => https://httpbingo.org/api/nigiri?token=abc&page=2
#
# conn.build_url("nigiri", page: 2)
# # => https://sushi.com/api/nigiri?token=abc&page=2
# # => https://httpbingo.org/api/nigiri?token=abc&page=2
#
def build_url(url = nil, extra_params = nil)
uri = build_exclusive_url(url)
Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/rack_builder.rb
Expand Up @@ -8,7 +8,7 @@ module Faraday
# middleware stack (heavily inspired by Rack).
#
# @example
# Faraday::Connection.new(url: 'http://sushi.com') do |builder|
# Faraday::Connection.new(url: 'http://httpbingo.org') do |builder|
# builder.request :url_encoded # Faraday::Request::UrlEncoded
# builder.adapter :net_http # Faraday::Adapter::NetHttp
# end
Expand Down

0 comments on commit 816d824

Please sign in to comment.