Skip to content

Commit ab24d46

Browse files
committed
Add module from readme and @doc for functions
1 parent 2a3b499 commit ab24d46

File tree

2 files changed

+90
-93
lines changed

2 files changed

+90
-93
lines changed

README.md

Lines changed: 21 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,17 @@
22

33
[![Build Status](https://travis-ci.org/PSPDFKit-labs/bypass.svg?branch=master)](https://travis-ci.org/PSPDFKit-labs/bypass)
44

5-
Bypass provides a quick way to create a custom plug that can be put in place instead of an actual
5+
[Online Documentation](https://hexdocs.pm/bypass).
6+
7+
<!-- MDOC !-->
8+
9+
`Bypass` provides a quick way to create a custom plug that can be put in place instead of an actual
610
HTTP server to return prebaked responses to client requests. This is most useful in tests, when you
711
want to create a mock HTTP server and test how your HTTP client handles different types of
812
responses from the server.
913

10-
## Installation
11-
12-
Add bypass to your list of dependencies in mix.exs:
13-
14-
```elixir
15-
def deps do
16-
[
17-
{:bypass, "~> 1.0", only: :test}
18-
]
19-
end
20-
```
21-
22-
We do not recommended adding `:bypass` to the list of applications in your `mix.exs`. See below
23-
for usage info.
24-
2514
Bypass supports Elixir 1.6 and OTP 20 and up. It works with Cowboy 1 and 2.
2615

27-
2816
## Usage
2917

3018
To use Bypass in a test case, open a connection and use its port to connect your client to it.
@@ -42,75 +30,6 @@ You can take any of the following approaches:
4230
* a combination of the above, where the routes will be used first, and then the generic version
4331
will be used as default
4432

45-
#### expect/2 (bypass_instance, function)
46-
47-
Must be called at least once.
48-
49-
```elixir
50-
Bypass.expect(bypass, fn conn ->
51-
assert "/1.1/statuses/update.json" == conn.request_path
52-
assert "POST" == conn.method
53-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
54-
end)
55-
```
56-
57-
#### expect_once/2 (bypass_instance, function)
58-
59-
Must be called exactly once.
60-
61-
```elixir
62-
Bypass.expect_once(bypass, fn conn ->
63-
assert "/1.1/statuses/update.json" == conn.request_path
64-
assert "POST" == conn.method
65-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
66-
end)
67-
```
68-
69-
#### expect/4 (bypass_instance, method, path, function)
70-
71-
Must be called at least once.
72-
73-
`method` is one of `["GET", "POST", "HEAD", "PUT", "PATCH", "DELETE", "OPTIONS", "CONNECT"]`
74-
75-
`path` is the endpoint.
76-
77-
```elixir
78-
Bypass.expect(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
79-
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
80-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
81-
end)
82-
```
83-
84-
#### expect_once/4 (bypass_instance, method, path, function)
85-
86-
Must be called exactly once.
87-
88-
`method` is one of `["GET", "POST", "HEAD", "PUT", "PATCH", "DELETE", "OPTIONS", "CONNECT"]`
89-
90-
`path` is the endpoint.
91-
92-
```elixir
93-
Bypass.expect_once(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
94-
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
95-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
96-
end)
97-
```
98-
99-
#### stub/4 (bypass_instance, method, path, function)
100-
101-
May be called none or more times.
102-
103-
`method` is one of `["GET", "POST", "HEAD", "PUT", "PATCH", "DELETE", "OPTIONS", "CONNECT"]`
104-
105-
`path` is the endpoint.
106-
107-
```elixir
108-
Bypass.stub(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
109-
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
110-
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
111-
end)
112-
```
113-
11433
### Example
11534

11635
In the following example `TwitterClient.start_link()` takes the endpoint URL as its argument
@@ -224,6 +143,22 @@ Set `:enable_debug_log` to `true` in the application environment to make Bypass
224143
config :bypass, enable_debug_log: true
225144
```
226145

146+
<!-- MDOC !-->
147+
148+
## Installation
149+
150+
Add bypass to your list of dependencies in mix.exs:
151+
152+
```elixir
153+
def deps do
154+
[
155+
{:bypass, "~> 1.0", only: :test}
156+
]
157+
end
158+
```
159+
160+
We do not recommended adding `:bypass` to the list of applications in your `mix.exs`.
161+
227162
## License
228163

229164
This software is licensed under [the MIT license](LICENSE).

lib/bypass.ex

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
defmodule Bypass do
2-
@moduledoc """
3-
Bypass provides a quick way to create a custom Plug that can be put in place
4-
instead of an actual HTTP server to return prebaked responses to client
5-
requests.
6-
7-
This module is the main interface to the library.
8-
"""
2+
@external_resource "README.md"
3+
@moduledoc "README.md"
4+
|> File.read!()
5+
|> String.split("<!-- MDOC !-->")
6+
|> Enum.fetch!(1)
97

108
defstruct pid: nil, port: nil
119

@@ -115,18 +113,82 @@ defmodule Bypass do
115113
def down(%Bypass{pid: pid}),
116114
do: Bypass.Instance.call(pid, :down)
117115

116+
@doc """
117+
Expects the passed function to be called at least once regardless of the route.
118+
119+
```elixir
120+
Bypass.expect(bypass, fn conn ->
121+
assert "/1.1/statuses/update.json" == conn.request_path
122+
assert "POST" == conn.method
123+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
124+
end)
125+
```
126+
"""
118127
def expect(%Bypass{pid: pid}, fun),
119128
do: Bypass.Instance.call(pid, {:expect, fun})
120129

130+
@doc """
131+
Expects the passed function to be called at least once for the specified route (method and path).
132+
133+
- `method` is one of `["GET", "POST", "HEAD", "PUT", "PATCH", "DELETE", "OPTIONS", "CONNECT"]`
134+
135+
- `path` is the endpoint.
136+
137+
```elixir
138+
Bypass.expect(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
139+
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
140+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
141+
end)
142+
```
143+
"""
121144
def expect(%Bypass{pid: pid}, methods, paths, fun),
122145
do: Bypass.Instance.call(pid, {:expect, methods, paths, fun})
123146

147+
@doc """
148+
Expects the passed function to be called exactly once regardless of the route.
149+
150+
```elixir
151+
Bypass.expect_once(bypass, fn conn ->
152+
assert "/1.1/statuses/update.json" == conn.request_path
153+
assert "POST" == conn.method
154+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
155+
end)
156+
```
157+
"""
124158
def expect_once(%Bypass{pid: pid}, fun),
125159
do: Bypass.Instance.call(pid, {:expect_once, fun})
126160

161+
@doc """
162+
Expects the passed function to be called exactly once for the specified route (method and path).
163+
164+
- `method` is one of `["GET", "POST", "HEAD", "PUT", "PATCH", "DELETE", "OPTIONS", "CONNECT"]`
165+
166+
- `path` is the endpoint.
167+
168+
```elixir
169+
Bypass.expect_once(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
170+
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
171+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
172+
end)
173+
```
174+
"""
127175
def expect_once(%Bypass{pid: pid}, methods, paths, fun),
128176
do: Bypass.Instance.call(pid, {:expect_once, methods, paths, fun})
129177

178+
@doc """
179+
Allows the function to be invoked zero or many times for the specified route (method and path).
180+
181+
- `method` is one of `["GET", "POST", "HEAD", "PUT", "PATCH", "DELETE", "OPTIONS", "CONNECT"]`
182+
183+
- `path` is the endpoint.
184+
185+
```elixir
186+
Bypass.stub(bypass, "POST", "/1.1/statuses/update.json", fn conn ->
187+
Agent.get_and_update(AgentModule, fn step_no -> {step_no, step_no + 1} end)
188+
Plug.Conn.resp(conn, 429, ~s<{"errors": [{"code": 88, "message": "Rate limit exceeded"}]}>)
189+
end)
190+
```
191+
"""
130192
def stub(%Bypass{pid: pid}, methods, paths, fun),
131193
do: Bypass.Instance.call(pid, {:stub, methods, paths, fun})
132194

0 commit comments

Comments
 (0)