diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..7e68299 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,66 @@ +# Ruby CircleCI 2.0 configuration file +# +# Check https://circleci.com/docs/2.0/language-ruby/ for more details +# +version: 2 +jobs: + build: + docker: + # specify the version you desire here + - image: circleci/ruby:2.4.1-node-browsers + environment: + BUNDLER_VERSION: 2.0.1 + + # Specify service dependencies here if necessary + # CircleCI maintains a library of pre-built images + # documented at https://circleci.com/docs/2.0/circleci-images/ + # - image: circleci/postgres:9.4 + + working_directory: ~/repo + + steps: + - checkout + + # Download and cache dependencies + - restore_cache: + keys: + - v1-dependencies-{{ checksum "Gemfile.lock" }} + # fallback to using the latest cache if no exact match is found + - v1-dependencies- + + - run: + name: install bundler 2.0 + command: | + gem install bundler + + - run: + name: install dependencies + command: | + bundle install --jobs=4 --retry=3 --path vendor/bundle + + - save_cache: + paths: + - ./vendor/bundle + key: v1-dependencies-{{ checksum "Gemfile.lock" }} + + # run tests! + - run: + name: run tests + command: | + mkdir /tmp/test-results + TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | \ + circleci tests split --split-by=timings)" + + bundle exec rspec \ + --format progress \ + --format RspecJunitFormatter \ + --out /tmp/test-results/rspec.xml \ + --format progress \ + $TEST_FILES + + # collect reports + - store_test_results: + path: /tmp/test-results + - store_artifacts: + path: /tmp/test-results + destination: test-results \ No newline at end of file diff --git a/.gitignore b/.gitignore index 54dacf9..0bcdfc6 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ tmp .rvmrc .ruby-version .yardoc +.rspec_status ## BUNDLER *.gem diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..bb69742 --- /dev/null +++ b/.rspec @@ -0,0 +1,3 @@ +--require spec_helper +--format documentation +--color \ No newline at end of file diff --git a/Gemfile b/Gemfile index 2bb707e..5c95a18 100644 --- a/Gemfile +++ b/Gemfile @@ -4,3 +4,5 @@ source 'https://rubygems.org' # Specify your gem's dependencies in faraday-http.gemspec gemspec + +gem 'faraday', github: 'lostisland/faraday' diff --git a/Gemfile.lock b/Gemfile.lock index dddda74..8afda38 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,19 +1,47 @@ +GIT + remote: git://github.com/lostisland/faraday.git + revision: 224dd98ebedd35de284b19893e0f34f3fc4b289f + specs: + faraday (0.15.3) + multipart-post (>= 1.2, < 3) + PATH remote: . specs: faraday-http (0.1.0) + http (~> 3.0) GEM remote: https://rubygems.org/ specs: + addressable (2.6.0) + public_suffix (>= 2.0.2, < 4.0) ast (2.4.0) + crack (0.4.3) + safe_yaml (~> 1.0.0) diff-lcs (1.3) + docile (1.3.1) + domain_name (0.5.20180417) + unf (>= 0.0.5, < 1.0.0) + hashdiff (0.3.8) + http (3.3.0) + addressable (~> 2.3) + http-cookie (~> 1.0) + http-form_data (~> 2.0) + http_parser.rb (~> 0.6.0) + http-cookie (1.0.3) + domain_name (~> 0.5) + http-form_data (2.1.1) + http_parser.rb (0.6.0) jaro_winkler (1.5.2) + json (2.2.0) + multipart-post (2.0.0) parallel (1.14.0) parser (2.6.0.0) ast (~> 2.4.0) powerpack (0.1.2) psych (3.1.0) + public_suffix (3.0.3) rainbow (3.0.0) rake (10.5.0) rspec (3.8.0) @@ -29,6 +57,8 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.8.0) rspec-support (3.8.0) + rspec_junit_formatter (0.4.1) + rspec-core (>= 2, < 4, != 2.12.0) rubocop (0.65.0) jaro_winkler (~> 1.5.1) parallel (~> 1.10) @@ -39,17 +69,34 @@ GEM ruby-progressbar (~> 1.7) unicode-display_width (~> 1.4.0) ruby-progressbar (1.10.0) + safe_yaml (1.0.5) + simplecov (0.16.1) + docile (~> 1.1) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.2) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.5) unicode-display_width (1.4.1) + webmock (3.5.1) + addressable (>= 2.3.6) + crack (>= 0.3.2) + hashdiff PLATFORMS ruby DEPENDENCIES bundler (~> 2.0) + faraday! faraday-http! rake (~> 10.0) rspec (~> 3.0) + rspec_junit_formatter (~> 0.4) rubocop (~> 0.65) + simplecov + webmock (~> 3.4) BUNDLED WITH 2.0.1 diff --git a/faraday-http.gemspec b/faraday-http.gemspec index ce94f77..d3657a5 100644 --- a/faraday-http.gemspec +++ b/faraday-http.gemspec @@ -37,8 +37,13 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] + spec.add_dependency 'http', '~> 3.0' + spec.add_development_dependency 'bundler', '~> 2.0' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'rspec', '~> 3.0' + spec.add_development_dependency 'rspec_junit_formatter', '~> 0.4' spec.add_development_dependency 'rubocop', '~> 0.65' + spec.add_development_dependency 'simplecov' + spec.add_development_dependency 'webmock', '~> 3.4' end diff --git a/lib/faraday/adapter/http.rb b/lib/faraday/adapter/http.rb new file mode 100644 index 0000000..ac440aa --- /dev/null +++ b/lib/faraday/adapter/http.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +module Faraday + class Adapter + # HTTP.rb adapter. + class HTTP < Faraday::Adapter + # Takes the environment and performs the request. + # + # @param env [Faraday::Env] the request environment. + # + # @return [Faraday::Response] the response. + def call(env) + super + perform_request(env) + @app.call(env) + end + + private + + def perform_request(env) + conn = setup_connection(env) + + resp = conn.request env[:method], env[:url], body: env[:body] + save_response(env, resp.code, resp.body.to_s, resp.headers, resp.status.reason) + rescue ::HTTP::TimeoutError + raise Faraday::TimeoutError, $ERROR_INFO + rescue ::HTTP::ConnectionError + raise Faraday::ConnectionFailed, $ERROR_INFO + end + + def setup_connection(env) + conn = ::HTTP + + request_config(conn, env[:request]) if env[:request] + conn.headers(env.request_headers) + end + + def request_config(conn, config) + if (timeout = config[:timeout]) + conn = conn.timeout(connect: timeout, read: timeout, write: timeout) + end + + if (timeout = config[:open_timeout]) + conn = conn.timeout(connect: timeout, write: timeout) + end + + if (proxy = config[:proxy]) + conn = conn.via(proxy.uri.host, proxy.uri.port, proxy.user, proxy.password) + end + + conn + end + end + end +end diff --git a/lib/faraday/http.rb b/lib/faraday/http.rb index b895b90..c159e95 100644 --- a/lib/faraday/http.rb +++ b/lib/faraday/http.rb @@ -1,10 +1,15 @@ # frozen_string_literal: true +require 'faraday' require 'faraday/http/version' +require 'faraday/adapter/http' +# Extend the main Faraday module. module Faraday module Http class Error < StandardError; end # Your code goes here... end + + Faraday::Adapter.register_middleware http: Faraday::Adapter::HTTP end diff --git a/spec/faraday/http/adapter_spec.rb b/spec/faraday/http/adapter_spec.rb new file mode 100644 index 0000000..3c2baa3 --- /dev/null +++ b/spec/faraday/http/adapter_spec.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +RSpec.describe Faraday::Adapter::HTTP do + features :request_body_on_query_methods, :reason_phrase_parse, :trace_method, :connect_method, + :skip_response_body_on_head, :local_socket_binding + + it_behaves_like 'an adapter' +end diff --git a/spec/faraday/http_spec.rb b/spec/faraday/http_spec.rb index ce97546..bec07e3 100644 --- a/spec/faraday/http_spec.rb +++ b/spec/faraday/http_spec.rb @@ -4,8 +4,4 @@ it 'has a version number' do expect(Faraday::Http::VERSION).not_to be nil end - - it 'does something useful' do - expect(false).to eq(true) - end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 6780e87..7322143 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,7 +1,15 @@ # frozen_string_literal: true +require 'simplecov' +SimpleCov.start do + add_filter '/spec/' + minimum_coverage 95 + minimum_coverage_by_file 80 +end + require 'bundler/setup' require 'faraday/http' +require 'faraday_specs_setup' RSpec.configure do |config| # Enable flags like --only-failures and --next-failure @@ -13,4 +21,7 @@ config.expect_with :rspec do |c| c.syntax = :expect end + + config.order = :random + Kernel.srand config.seed end