Skip to content

Commit 96b9ae9

Browse files
authored
Merge branch 'master' into jv-latest-cowboy
2 parents 6606fa7 + 350b87e commit 96b9ae9

File tree

5 files changed

+186
-134
lines changed

5 files changed

+186
-134
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## v2.0.0 - TBA
3+
## v2.0.0 - 19 Aug 2020
44

55
* Allow the redefinition of routes.
66
* Make listen interface configurable.

README.md

Lines changed: 59 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2,119 +2,43 @@
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
6-
HTTP server to return prebaked responses to client requests. This is most useful in tests, when you
7-
want to create a mock HTTP server and test how your HTTP client handles different types of
8-
responses from the server.
5+
[Online Documentation](https://hexdocs.pm/bypass).
96

10-
## Installation
7+
<!-- MDOC !-->
118

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.
9+
`Bypass` provides a quick way to create a custom plug that can be put in place
10+
instead of an actual HTTP server to return prebaked responses to client
11+
requests. This is most useful in tests, when you want to create a mock HTTP
12+
server and test how your HTTP client handles different types of responses from
13+
the server.
2414

2515
Bypass supports Elixir 1.6 and OTP 20 and up. It works with Cowboy 1 and 2.
2616

27-
2817
## Usage
2918

30-
To use Bypass in a test case, open a connection and use its port to connect your client to it.
19+
To use Bypass in a test case, open a connection and use its port to connect your
20+
client to it.
3121

32-
If you want to test what happens when the HTTP server goes down, use `Bypass.down/1` to close the
33-
TCP socket and `Bypass.up/1` to start listening on the same port again. Both functions block until
34-
the socket updates its state.
22+
If you want to test what happens when the HTTP server goes down, use
23+
`Bypass.down/1` to close the TCP socket and `Bypass.up/1` to start listening on
24+
the same port again. Both functions block until the socket updates its state.
3525

3626
### Expect Functions
3727

3828
You can take any of the following approaches:
39-
* `expect/2` or `expect_once/2` to install a generic function that all calls to bypass will use
29+
30+
* `expect/2` or `expect_once/2` to install a generic function that all calls to
31+
bypass will use
4032
* `expect/4` and/or `expect_once/4` to install specific routes (method and path)
4133
* `stub/4` to install specific routes without expectations
42-
* a combination of the above, where the routes will be used first, and then the generic version
43-
will be used as default
44-
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-
```
34+
* a combination of the above, where the routes will be used first, and then the
35+
generic version will be used as default
11336

11437
### Example
11538

116-
In the following example `TwitterClient.start_link()` takes the endpoint URL as its argument
117-
allowing us to make sure it will connect to the running instance of Bypass.
39+
In the following example `TwitterClient.start_link()` takes the endpoint URL as
40+
its argument allowing us to make sure it will connect to the running instance of
41+
Bypass.
11842

11943
```elixir
12044
defmodule TwitterClientTest do
@@ -162,33 +86,31 @@ defmodule TwitterClientTest do
16286
end
16387
```
16488

165-
That's all you need to do. Bypass automatically sets up an `on_exit` hook to close its socket when
166-
the test finishes running.
167-
168-
Multiple concurrent Bypass instances are supported, all will have a different unique port. Concurrent
169-
requests are also supported on the same instance.
170-
171-
In case you need to assign a specific port to a Bypass instance to listen on, you can pass the
172-
`port` option to `Bypass.open()`:
89+
That's all you need to do. Bypass automatically sets up an `on_exit` hook to
90+
close its socket when the test finishes running.
17391

174-
```elixir
175-
bypass = Bypass.open(port: 1234)
176-
```
92+
Multiple concurrent Bypass instances are supported, all will have a different
93+
unique port. Concurrent requests are also supported on the same instance.
17794

178-
> Note: `Bypass.open/0` **must not** be called in a `setup_all` blocks due to the way Bypass verifies the expectations at the end of each
179-
> test.
95+
> Note: `Bypass.open/0` **must not** be called in a `setup_all` blocks due to
96+
> the way Bypass verifies the expectations at the end of each test.
18097
18198
## How to use with ESpec
18299

183-
While Bypass primarily targets ExUnit, the official Elixir builtin test framework, it can also be used with [ESpec](https://hex.pm/packages/espec). The test configuration is basically the same, there are only two differences:
100+
While Bypass primarily targets ExUnit, the official Elixir builtin test
101+
framework, it can also be used with [ESpec](https://hex.pm/packages/espec). The
102+
test configuration is basically the same, there are only two differences:
184103

185-
1. In your Mix config file, you must declare which test framework Bypass is being used with (defaults to `:ex_unit`). This simply disables the automatic integration with some hooks provided by `ExUnit`.
104+
1. In your Mix config file, you must declare which test framework Bypass is
105+
being used with (defaults to `:ex_unit`). This simply disables the automatic
106+
integration with some hooks provided by `ExUnit`.
186107

187108
```elixir
188109
config :bypass, test_framework: :espec
189110
```
190111

191-
2. In your specs, you must explicitly verify the declared expectations. You can do it in the `finally` block.
112+
2. In your specs, you must explicitly verify the declared expectations. You can
113+
do it in the `finally` block.
192114

193115
```elixir
194116
defmodule TwitterClientSpec do
@@ -218,12 +140,30 @@ end
218140

219141
## Configuration options
220142

221-
Set `:enable_debug_log` to `true` in the application environment to make Bypass log what it's doing:
143+
Set `:enable_debug_log` to `true` in the application environment to make Bypass
144+
log what it's doing:
222145

223146
```elixir
224147
config :bypass, enable_debug_log: true
225148
```
226149

150+
<!-- MDOC !-->
151+
152+
## Installation
153+
154+
Add bypass to your list of dependencies in mix.exs:
155+
156+
```elixir
157+
def deps do
158+
[
159+
{:bypass, "~> 2.0", only: :test}
160+
]
161+
end
162+
```
163+
164+
We do not recommended adding `:bypass` to the list of applications in your
165+
`mix.exs`.
166+
227167
## License
228168

229169
This software is licensed under [the MIT license](LICENSE).
@@ -236,8 +176,10 @@ This software is licensed under [the MIT license](LICENSE).
236176

237177
This project is maintained and funded by [PSPDFKit](https://pspdfkit.com/).
238178

239-
Please ensure
240-
[you signed our CLA](https://pspdfkit.com/guides/web/current/miscellaneous/contributing/) so we can
241-
accept your contributions.
179+
Please ensure [you signed our
180+
CLA](https://pspdfkit.com/guides/web/current/miscellaneous/contributing/) so we
181+
can accept your contributions.
242182

243-
See [our other open source projects](https://github.com/PSPDFKit-labs), read [our blog](https://pspdfkit.com/blog/) or say hello on Twitter ([@PSPDFKit](https://twitter.com/pspdfkit)).
183+
See [our other open source projects](https://github.com/PSPDFKit-labs), read
184+
[our blog](https://pspdfkit.com/blog/) or say hello on Twitter
185+
([@PSPDFKit](https://twitter.com/pspdfkit)).

0 commit comments

Comments
 (0)