marcamillion
marcamillion

Reputation: 33775

How do I test a response from Faraday?

I am writing a Ruby wrapper for a JSON API. I am expecting a specific type of response when a successful post is sent to that endpoint...but I am not sure how to actually get the response.

I am using Faraday like this:

  @connection ||= Faraday.new do |f|
    f.url_prefix = "https://myapiurl.com/api/auth"
    f.adapter :net_http

    f.headers['User-Agent'] = "Some-Awesome-User-Agent"
    f.headers['Content-Type'] = content_type
    f.headers['Accept'] = api_version
    f.params['API-KEY'] = api_key if api_key

    f.response :json, content_type: /\bjson$/
  end

However, when I do @connection.post, I keep getting an SSL error:

@connection.post
Faraday::SSLError: SSL_connect returned=1 errno=0 state=error: certificate verify failed

If I am not mistaken, once I can fix the SSL error, I should receive some response object that I can then act upon right?

Edit 1

I am using OpenSSL 0.9.8zh 14 Jan 2016, also ruby 2.3.1, along with rvm 1.27.0 (latest)

This is what my Gemfile.lock looks like:

PATH
  remote: .
  specs:
    mygem (0.1.0)

GEM
  remote: https://rubygems.org/
  specs:
    coderay (1.1.1)
    diff-lcs (1.2.5)
    faraday (0.9.2)
      multipart-post (>= 1.2, < 3)
    faraday_middleware (0.10.0)
      faraday (>= 0.7.4, < 0.10)
    json (2.0.2)
    method_source (0.8.2)
    multipart-post (2.0.0)
    pry (0.10.4)
      coderay (~> 1.1.0)
      method_source (~> 0.8.1)
      slop (~> 3.4)
    rake (10.5.0)
    rspec (3.5.0)
      rspec-core (~> 3.5.0)
      rspec-expectations (~> 3.5.0)
      rspec-mocks (~> 3.5.0)
    rspec-core (3.5.1)
      rspec-support (~> 3.5.0)
    rspec-expectations (3.5.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.5.0)
    rspec-mocks (3.5.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.5.0)
    rspec-support (3.5.0)
    slop (3.6.0)

PLATFORMS
  ruby

DEPENDENCIES
  bundler (~> 1.11)
  faraday (~> 0.9.2)
  faraday_middleware (~> 0.10.0)
  json (~> 2.0, >= 2.0.2)
  mygem
  pry (~> 0.10.4)
  rake (~> 10.0)
  rspec (~> 3.0)
  rspec-expectations (~> 3.5)

BUNDLED WITH
   1.12.5

Upvotes: 1

Views: 2668

Answers (1)

Aetherus
Aetherus

Reputation: 8898

You get this error because your SSL cert is not signed by a trusted certificate authority (CA), or is not signed at all. You can pass the option {ssl: {verify: false}} to Faraday.new to force it skip CA verification.

@connection ||= Faraday.new(ssl: {verify: false}) do |f|
  # ...
end

This is okay for testing your own web api before buying an SSL cert, but don't do this when calling api's other than your own because without a CA, you can never tell whether the cert of that site is a fake.

Upvotes: 2

Related Questions