-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule Tesla.Middleware.KeepRequest do | ||
@behaviour Tesla.Middleware | ||
|
||
@moduledoc """ | ||
Store request body & headers into opts. | ||
### Example | ||
``` | ||
defmodule MyClient do | ||
use Tesla | ||
plug Tesla.Middleware.KeepRequest | ||
end | ||
{:ok, env} = MyClient.post("/", "request-data") | ||
env.body # => "response-data" | ||
env.opts[:req_body] # => "request-data" | ||
``` | ||
""" | ||
def call(env, next, _opts) do | ||
env | ||
|> Tesla.put_opt(:req_body, env.body) | ||
|> Tesla.put_opt(:req_headers, env.headers) | ||
|> Tesla.run(next) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule Tesla.Middleware.KeepRequestTest do | ||
use ExUnit.Case | ||
|
||
@middleware Tesla.Middleware.KeepRequest | ||
|
||
test "put request body & headers into opts" do | ||
env = %Tesla.Env{body: "reqbody", headers: [{"x-request", "header"}]} | ||
assert {:ok, env} = @middleware.call(env, [], []) | ||
assert env.opts[:req_body] == "reqbody" | ||
assert env.opts[:req_headers] == [{"x-request", "header"}] | ||
end | ||
end |