Comments (4)
Hi, thank you for reporting the issue.
There might be problems on Faraday::Utils::Headers
; it can't handle Symbol
header including -
here.
faraday/lib/faraday/utils/headers.rb
Lines 57 to 63 in f6c3868
Also, testing adapter does not type-cast non-String
body as String
, so stub.post('/api/push', {}, headers)
should be stub.post('/api/push', '{}', headers)
(I think testing adapter can cast the value 😅 )
I think you don't have to explicitly set Content-Type
in the block of Faraday.new
. Instead, the workaround is like this:
headers = { 'Content-Type' => 'application/json' }
client.adapter(:test) do |stub|
stub.post('/api/push', '{}', headers) { [200, {}, {}] }
stub.strict_mode = true
end
Here is the change:
diff --git a/a.rb b/a.rb
index a0a1cba..ef6599a 100644
--- a/a.rb
+++ b/a.rb
@@ -1,6 +1,6 @@
-headers = { 'Content-Type': 'application/json' }
+headers = { 'Content-Type' => 'application/json' }
client.adapter(:test) do |stub|
- stub.post('/api/push', {}, headers) { [200, {}, {}] }
+ stub.post('/api/push', '{}', headers) { [200, {}, {}] }
stub.strict_mode = true
end
from faraday.
Hi @yykamei. Yes, that also works. The bug though is that you'd want request(:json)
to be able to work, which should be taking care of encoding the request body as json and setting the correct content-type. So, yours is also a workaround but without the request(:json)
, you'll need to encode as json your self. Eg:
stub.post('/api/push', payload.to_json, headers) { [200, {}, {}] }
from faraday.
Actually, we're both partly right. It is indeed that symbols aren't supported in the testing framework. So your headers change works. But, f.request(:json)
also works. So, my working version is:
client = Faraday.new(url: <your_base_url>) do |f|
f.request(:json)
end
headers = { 'Content-Type' => 'application/json' } # no longer a symbol
client.adapter(:test) do |stub|
stub.post('/api/push', {}, headers) { [200, {}, {}] }
stub.strict_mode = true
end
Thanks for your help. I don't know if this should just be closed? Maybe others would get tripped up by the symbol comparison.
from faraday.
Maybe, I understood your point; the testing adapter should consider the JSON middleware when comparing the actually used request header with expected one. If developers use request(:json)
, testing adapter should also encode the passed stub body as JSON. Is it correct?
from faraday.
Related Issues (20)
- Block private ips (allow_private_ips) HOT 3
- Multiple "already initialized constant" warnings HOT 2
- NoMethodError: undefined method `method=' for #<Faraday::RequestOptions (empty) HOT 5
- Faraday::Response - argument order causes unexpected behaviour HOT 3
- Incompatible with Firebase-admin-sdk HOT 1
- Streaming Stop with Connection Close? HOT 3
- `Faraday::Request::Json` doesn't encode the value `false` while it encodes `true` to `"true"` HOT 2
- Lots of "warning: method redefined; discarding old" printed during testing HOT 7
- Faraday using PUT with Transfer-Encoding: chunked
- Faraday::ParsingError.response_status doesn't return expected value when stubbing response HOT 5
- Missing HTTP 408 Request Timeout specific Faraday ClientError? HOT 3
- Dead link to Faraday Team in README and docs HOT 1
- Strange behaviour with response HOT 1
- Setting ActiveRecord DatabaseResolver to enable multi database does not work with Faraday adapter HOT 3
- Unable to use webdav verbs HOT 3
- Change connection proxy HOT 2
- Drop Ruby < 3.0 support HOT 1
- `no_proxy` Parameter in Manual Proxy Configuration HOT 4
- connection headers are ignored when using build_response directly HOT 5
- Add tests and documentation to make sure adapters support streaming requests HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from faraday.