Code Monkey home page Code Monkey logo

Comments (3)

gurgeous avatar gurgeous commented on June 30, 2024

I wrote some code to handle the charset (see below). I really have no idea how to write the tests, unfortunately. I haven't used RSpec before. I see a bunch of nested tests but I'm not sure how to stub responses inside an adapter test. Any pointers?

      def encoded_body(http_response)
        body = http_response.body || +''
        if encoding = encoding_for(http_response)
          if body.encoding != encoding
            body = body.dup if body.frozen?
            body.force_encoding(encoding)
          end
        end
        body
      end

      def encoding_for(http_response)
        if content_type = http_response['Content-Type']
          if encoding = content_type[/\bcharset=\s*(.+?)\s*(;|$)/, 1]
            Encoding.find(encoding) rescue nil
          end
        end
      end

from faraday-net_http.

gurgeous avatar gurgeous commented on June 30, 2024

Also, I am unfamiliar with this syntax http_response.body || +''. I copied it from the code above, but what does it do? :)

from faraday-net_http.

iMacTia avatar iMacTia commented on June 30, 2024

Hi @gurgeous thanks for raising this and doing some initial research!
We can take this from here and incorporate your code with some RSpec tests in a PR, I'll just need to find some time to work on it (@olleolleolle @JanDintel feel free to jump in if you have some time!).

Also, I am unfamiliar with this syntax http_response.body || +''. I copied it from the code above, but what does it do? :)

The only "strange" thing in there I can see is the + prefix to the string literal, which makes the string mutable rather than the immutable default 👍
That together with body = body.dup if body.frozen? is necessary to be able to call force_encoding later

from faraday-net_http.

Related Issues (10)

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.