You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+43-29Lines changed: 43 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,34 +6,39 @@
6
6
7
7
<!-- MDOC !-->
8
8
9
-
`Bypass` provides a quick way to create a custom plug that can be put in place instead of an actual
10
-
HTTP server to return prebaked responses to client requests. This is most useful in tests, when you
11
-
want to create a mock HTTP server and test how your HTTP client handles different types of
12
-
responses from the server.
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.
13
14
14
15
Bypass supports Elixir 1.6 and OTP 20 and up. It works with Cowboy 1 and 2.
15
16
16
17
## Usage
17
18
18
-
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.
19
21
20
-
If you want to test what happens when the HTTP server goes down, use`Bypass.down/1` to close the
21
-
TCP socket and `Bypass.up/1` to start listening on the same port again. Both functions block until
22
-
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.
23
25
24
26
### Expect Functions
25
27
26
28
You can take any of the following approaches:
27
-
*`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
28
32
*`expect/4` and/or `expect_once/4` to install specific routes (method and path)
29
33
*`stub/4` to install specific routes without expectations
30
-
* a combination of the above, where the routes will be used first, and then the generic version
31
-
will be used as default
34
+
* a combination of the above, where the routes will be used first, and then the
35
+
generic version will be used as default
32
36
33
37
### Example
34
38
35
-
In the following example `TwitterClient.start_link()` takes the endpoint URL as its argument
36
-
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.
37
42
38
43
```elixir
39
44
defmoduleTwitterClientTestdo
@@ -81,26 +86,31 @@ defmodule TwitterClientTest do
81
86
end
82
87
```
83
88
84
-
That's all you need to do. Bypass automatically sets up an `on_exit` hook to close its socket when
85
-
the test finishes running.
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.
86
91
87
-
Multiple concurrent Bypass instances are supported, all will have a different unique port. Concurrent
88
-
requests are also supported on the same instance.
92
+
Multiple concurrent Bypass instances are supported, all will have a different
93
+
unique port. Concurrent requests are also supported on the same instance.
89
94
90
-
> 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
91
-
> 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.
92
97
93
98
## How to use with ESpec
94
99
95
-
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:
96
103
97
-
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`.
98
107
99
108
```elixir
100
109
config :bypass, test_framework::espec
101
110
```
102
111
103
-
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.
104
114
105
115
```elixir
106
116
defmoduleTwitterClientSpecdo
@@ -130,7 +140,8 @@ end
130
140
131
141
## Configuration options
132
142
133
-
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:
134
145
135
146
```elixir
136
147
config :bypass, enable_debug_log:true
@@ -145,12 +156,13 @@ Add bypass to your list of dependencies in mix.exs:
145
156
```elixir
146
157
defdepsdo
147
158
[
148
-
{:bypass, "~> 1.0", only::test}
159
+
{:bypass, "~> 2.0", only::test}
149
160
]
150
161
end
151
162
```
152
163
153
-
We do not recommended adding `:bypass` to the list of applications in your `mix.exs`.
164
+
We do not recommended adding `:bypass` to the list of applications in your
165
+
`mix.exs`.
154
166
155
167
## License
156
168
@@ -164,8 +176,10 @@ This software is licensed under [the MIT license](LICENSE).
164
176
165
177
This project is maintained and funded by [PSPDFKit](https://pspdfkit.com/).
166
178
167
-
Please ensure
168
-
[you signed our CLA](https://pspdfkit.com/guides/web/current/miscellaneous/contributing/) so we can
169
-
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.
170
182
171
-
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
0 commit comments