Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update testing example #515

Merged
merged 3 commits into from
May 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Tesla

[![Build Status](https://github.com/teamon/tesla/workflows/Test/badge.svg?branch=master)](https://github.com/teamon/tesla/actions)
[![Hex.pm](https://img.shields.io/hexpm/v/tesla.svg)](http://hex.pm/packages/tesla)
[![Hex.pm](https://img.shields.io/hexpm/v/tesla.svg)](https://hex.pm/packages/tesla)
[![Hexdocs.pm](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/tesla/)
[![Hex.pm](https://img.shields.io/hexpm/dt/tesla.svg)](https://hex.pm/packages/tesla)
[![Hex.pm](https://img.shields.io/hexpm/dw/tesla.svg)](https://hex.pm/packages/tesla)
[![codecov](https://codecov.io/gh/teamon/tesla/branch/master/graph/badge.svg)](https://codecov.io/gh/teamon/tesla)
[![Inline docs](https://inch-ci.org/github/teamon/tesla.svg)](http://inch-ci.org/github/teamon/tesla)
[![Inline docs](https://inch-ci.org/github/teamon/tesla.svg)](https://inch-ci.org/github/teamon/tesla)

Tesla is an HTTP client loosely based on [Faraday](https://github.com/lostisland/faraday).
It embraces the concept of middleware when processing the request/response cycle.

> Note that this README refers to the `master` branch of Tesla, not the latest
released version on Hex. See [the documentation](http://hexdocs.pm/tesla) for
released version on Hex. See [the documentation](https://hexdocs.pm/tesla) for
the documentation of the version you're using.

For the list of changes, checkout the latest [release notes](https://github.com/teamon/tesla/releases).
Expand Down Expand Up @@ -139,7 +139,7 @@ This is very similar to how [Plug Router](https://github.com/elixir-plug/plug#th
## Runtime middleware

All HTTP functions, such as `Tesla.get/3` and `Tesla.post/4`, can take a dynamic client as the first argument.
This allow to use convenient syntax for modifying the behaviour in runtime.
This allows to use convenient syntax for modifying the behaviour in runtime.

Consider the following case: GitHub API can be accessed using OAuth token authorization.

Expand Down Expand Up @@ -186,7 +186,7 @@ client |> GitHub.get("/me")

Tesla supports multiple HTTP adapter that do the actual HTTP request processing.

- [`Tesla.Adapter.Httpc`](https://hexdocs.pm/tesla/Tesla.Adapter.Httpc.html) - the default, built-in erlang [httpc](http://erlang.org/doc/man/httpc.html) adapter
- [`Tesla.Adapter.Httpc`](https://hexdocs.pm/tesla/Tesla.Adapter.Httpc.html) - the default, built-in erlang [httpc](https://erlang.org/doc/man/httpc.html) adapter
- [`Tesla.Adapter.Hackney`](https://hexdocs.pm/tesla/Tesla.Adapter.Hackney.html) - [hackney](https://github.com/benoitc/hackney), "simple HTTP client in Erlang"
- [`Tesla.Adapter.Ibrowse`](https://hexdocs.pm/tesla/Tesla.Adapter.Ibrowse.html) - [ibrowse](https://github.com/cmullaparthi/ibrowse), "Erlang HTTP client"
- [`Tesla.Adapter.Gun`](https://hexdocs.pm/tesla/Tesla.Adapter.Gun.html) - [gun](https://github.com/ninenines/gun), "HTTP/1.1, HTTP/2 and Websocket client for Erlang/OTP"
Expand Down Expand Up @@ -243,7 +243,7 @@ Tesla.get(client, "/", opts: [adapter: [recv_timeout: 30_000]])

## Streaming

If adapter supports it, you can pass a [Stream](http://elixir-lang.org/docs/stable/elixir/Stream.html) as body, e.g.:
If adapter supports it, you can pass a [Stream](https://elixir-lang.org/docs/stable/elixir/Stream.html) as body, e.g.:

```elixir
defmodule ElasticSearch do
Expand Down Expand Up @@ -279,7 +279,7 @@ mp =
|> Multipart.add_file("test/tesla/multipart_test_file.sh", name: "foobar")
|> Multipart.add_file_content("sample file content", "sample.txt")

{:ok, response} = MyApiClient.post("http://httpbin.org/post", mp)
{:ok, response} = MyApiClient.post("https://httpbin.org/post", mp)
```

## Testing
Expand All @@ -304,18 +304,18 @@ defmodule MyAppTest do

setup do
mock(fn
%{method: :get, url: "http://example.com/hello"} ->
%{method: :get, url: "https://example.com/hello"} ->
%Tesla.Env{status: 200, body: "hello"}

%{method: :post, url: "http://example.com/world"} ->
%{method: :post, url: "https://example.com/world"} ->
json(%{"my" => "data"})
end)

:ok
end

test "list things" do
assert {:ok, %Tesla.Env{} = env} = MyApp.get("/hello")
assert {:ok, %Tesla.Env{} = env} = MyApi.get("https://example.com/hello")
assert env.status == 200
assert env.body == "hello"
end
Expand Down Expand Up @@ -414,7 +414,7 @@ This however won’t include any middleware.

```elixir
# Example get request
{:ok, response} = Tesla.get("http://httpbin.org/ip")
{:ok, response} = Tesla.get("https://httpbin.org/ip")

response.status
# => 200
Expand All @@ -425,11 +425,11 @@ response.body
response.headers
# => [{"content-type", "application/json" ...}]

{:ok, response} = Tesla.get("http://httpbin.org/get", query: [a: 1, b: "foo"])
{:ok, response} = Tesla.get("https://httpbin.org/get", query: [a: 1, b: "foo"])

# Example post request
{:ok, response} =
Tesla.post("http://httpbin.org/post", "data", headers: [{"content-type", "application/json"}])
Tesla.post("https://httpbin.org/post", "data", headers: [{"content-type", "application/json"}])
```

## Cheatsheet
Expand Down