Code Monkey home page Code Monkey logo

Comments (4)

yykamei avatar yykamei commented on August 15, 2024

Hi, thank you for reporting the issue.

There might be problems on Faraday::Utils::Headers; it can't handle Symbol header including - here.

def []=(key, val)
key = KeyMap[key]
key = (@names[key.downcase] ||= key)
# join multiple values with a comma
val = val.to_ary.join(', ') if val.respond_to?(:to_ary)
super(key, val)
end

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.

grjones avatar grjones commented on August 15, 2024

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.

grjones avatar grjones commented on August 15, 2024

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.

yykamei avatar yykamei commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.