Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tanmaykm/mockhttpserver.jl
Convenient mock HTTP server for testing HTTP APIs in Julia
https://github.com/tanmaykm/mockhttpserver.jl
Last synced: 13 days ago
JSON representation
Convenient mock HTTP server for testing HTTP APIs in Julia
- Host: GitHub
- URL: https://github.com/tanmaykm/mockhttpserver.jl
- Owner: tanmaykm
- License: other
- Created: 2022-07-12T06:52:34.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-07-25T05:11:08.000Z (over 2 years ago)
- Last Synced: 2023-05-05T19:51:39.070Z (over 1 year ago)
- Language: Julia
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# MockHTTPServer.jl
Convenient mock HTTP server for testing HTTP APIs in Julia.
[![Build Status](https://github.com/tanmaykm/MockHTTPServer.jl/workflows/CI/badge.svg)](https://github.com/tanmaykm/MockHTTPServer.jl/actions?query=workflow%3ACI+branch%3Amain)
## Example
```julia
using MockHTTPServer, HTTP, Test
import MockHTTPServer: Ctx, with_mockhttpservermockhttp = Ctx(; host="127.0.0.1", port=8080);
server_uri = "http://127.0.0.1:8080";with_mockhttpserver(mockhttp) do
testset1 = [
(method=:get, path="/get1", handler=(req)->HTTP.Response(200, "OK")),
(method=:get, path="/post1", handler=(req)->HTTP.Response(200, "OK")),
]
with_mockhttpserver(mockhttp, testset1...) do
resp = HTTP.get(server_uri * "/get1")
@test resp.status == 200
endtestset2 = [
(method=:post, path="/post2", handler=(req)->HTTP.Response(200, "OK")),
(method=:post, path="/get2", handler=(req)->HTTP.Response(200, "OK")),
]
with_mockhttpserver(mockhttp, testset2...) do
resp = HTTP.post(server_uri * "/post2")
@test resp.status == 200
end
end
```## Details
A context encapsulates the HTTP server and its configuration to use to respond to requests.
```julia
MockHTTPServer.Ctx(hlist...; host="0.0.0.0", port=80, server_args=Dict())
```Where:
- `hlist`: A list of Handler instances to use for the server.
- `host`: The hostname to listen on.
- `port`: The port to listen on.
- `server_args`: A dictionary of arguments to pass to the underlying HTTP server.A single handler can be created with the `MockHTTPServer.Handler` method. More convenient is the `MockHTTPServer.handlers` method that can
also create a bunch of handlers:```julia
MockHTTPServer.handlers(hlist...)
```Creates an array of `MockHTTPServer.Handler` instances from the provided specification(s) in `hlist`.
Method parameters can be one of:
- `Handler` instances
- `Dict` with keys `:method`, `:path` and `:handler` which can be used to invoke the Handler constructor
- `NamedTuple` with fields `method`, `path` and `handler` which can be used to invoke the Handler constructorFinally, the `with_mockhttpserver` method allows easy to use scopes to execute testcases.
```julia
with_mockhttpserver(f, ctx::Ctx, hlist...)
```It sets up the mock HTTP server with handlers specified in `hlist` and run `f` with the server open.
Nested `with_mockhttpserver` calls are allowed, and when the execution enters a nested scope, the handlers are replaced with the ones specified for the scope. On exit, the handlers are restored to the original values. The outermost scope's exit closes the server.