Page MenuHomePhorge

No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/tesla/middleware/method_override.ex b/lib/tesla/middleware/method_override.ex
new file mode 100644
index 0000000..375b913
--- /dev/null
+++ b/lib/tesla/middleware/method_override.ex
@@ -0,0 +1,37 @@
+defmodule Tesla.Middleware.MethodOverride do
+ @moduledoc """
+ Middleware that adds X-Http-Method-Override header with original request
+ method and sends the request as post.
+
+ Useful when there's an issue with sending non-post request.
+
+ Available options:
+ - `:override` - list of http methods that should be overriden,
+ everything except get and post if not specified
+ """
+
+ def call(env, next, opts) do
+ if overridable?(env, opts) do
+ env
+ |> override(opts)
+ |> Tesla.run(next)
+ else
+ env
+ |> Tesla.run(next)
+ end
+ end
+
+ def override(env, opts) do
+ env
+ |> Tesla.Middleware.Headers.call([], %{"X-Http-Method-Override" => "#{env.method}"})
+ |> Map.put(:method, :post)
+ end
+
+ def overridable?(env, opts) do
+ if opts[:override] do
+ env.method in opts[:override]
+ else
+ not env.method in [:get, :post]
+ end
+ end
+end
diff --git a/test/tesla/middleware/method_override_test.exs b/test/tesla/middleware/method_override_test.exs
new file mode 100644
index 0000000..3ce279a
--- /dev/null
+++ b/test/tesla/middleware/method_override_test.exs
@@ -0,0 +1,74 @@
+defmodule MethodOverrideTest do
+ use ExUnit.Case
+
+ use Tesla.Middleware.TestCase, middleware: Tesla.Middleware.MethodOverride
+
+
+ defmodule Client do
+ use Tesla
+
+ plug Tesla.Middleware.MethodOverride
+
+ adapter fn (env) ->
+ status = case env do
+ %{method: :get} -> 200
+ %{method: :post} -> 201
+ %{method: _} -> 400
+ end
+
+ %{env | status: status}
+ end
+ end
+
+ test "when method is get" do
+ response = Client.get("/")
+
+ assert response.status == 200
+ refute response.headers["x-http-method-override"]
+ end
+
+ test "when method is post" do
+ response = Client.post("/", "")
+
+ assert response.status == 201
+ refute response.headers["x-http-method-override"]
+ end
+
+ test "when method isn't get or post" do
+ response = Client.put("/", "")
+
+ assert response.status == 201
+ assert response.headers["x-http-method-override"] == "put"
+ end
+
+ defmodule CustomClient do
+ use Tesla
+
+ plug Tesla.Middleware.MethodOverride, override: ~w(put)a
+
+ adapter fn (env) ->
+ status = case env do
+ %{method: :get} -> 200
+ %{method: :post} -> 201
+ %{method: _} -> 400
+ end
+
+ %{env | status: status}
+ end
+ end
+
+ test "when method in override list" do
+ response = CustomClient.put("/", "")
+
+ assert response.status == 201
+ assert response.headers["x-http-method-override"] == "put"
+ end
+
+ test "when method not in override list" do
+ response = CustomClient.patch("/", "")
+
+ assert response.status == 400
+ refute response.headers["x-http-method-override"]
+ end
+
+end

File Metadata

Mime Type
text/x-diff
Expires
Fri, Nov 22, 3:39 PM (13 h, 26 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
38692
Default Alt Text
(2 KB)

Event Timeline