Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 93 lines (71 sloc) 2.277 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
require 'helper'
require 'faraday_middleware/request/method_override'

describe FaradayMiddleware::MethodOverride do

  let(:middleware) { described_class.new(lambda {|env| env }, *options) }
  let(:env) { middleware.call request_env(request_method) }

  def request_env(method)
    { :method => method,
      :request_headers => Faraday::Utils::Headers.new
    }
  end

  shared_examples "overrides method" do |method|
    it "sets physical method to POST" do
      expect(env[:method]).to eq(:post)
    end

    it "sets header to #{method}" do
      expect(env[:request_headers]['X-Http-Method-Override']).to eq(method)
    end
  end

  shared_examples "doesn't override method" do |method|
    it "keeps original method" do
      expect(env[:method]).to eq(method)
    end

    it "doesn't set header value" do
      expect(env[:request_headers]).not_to have_key('X-Http-Method-Override')
    end

  end

  context "with default options" do
    let(:options) { nil }

    context "GET" do
      let(:request_method) { :get }
      include_examples "doesn't override method", :get
    end

    context "POST" do
      let(:request_method) { :post }
      include_examples "doesn't override method", :post
    end

    context "PUT" do
      let(:request_method) { :put }
      include_examples "overrides method", 'PUT'
    end
  end

  context "configured to rewrite [:patch, :delete]" do
    let(:options) { [{ :rewrite => [:patch, :delete] }] }

    context "PUT" do
      let(:request_method) { :put }
      include_examples "doesn't override method", :put
    end

    context "PATCH" do
      let(:request_method) { :patch }
      include_examples "overrides method", 'PATCH'
    end

    context "DELETE" do
      let(:request_method) { :delete }
      include_examples "overrides method", 'DELETE'
    end
  end

  context "configured to rewrite ['PATCH']" do
    let(:options) { [{ :rewrite => %w[PATCH] }] }

    context "PATCH" do
      let(:request_method) { :patch }
      include_examples "overrides method", 'PATCH'
    end
  end

  context "with invalid option" do
    let(:options) { [{ :hello => 'world' }] }
    let(:request_method) { :get }

    it "raises key error" do
      expect{ env }.to raise_error(IndexError, /key not found/)
    end
  end

end
Something went wrong with that request. Please try again.