https://github.com/wojtekmach/bandit_mock
https://github.com/wojtekmach/bandit_mock
Last synced: 3 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/wojtekmach/bandit_mock
- Owner: wojtekmach
- Created: 2023-12-17T21:36:45.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-12-26T19:59:17.000Z (almost 2 years ago)
- Last Synced: 2025-09-27T00:32:10.782Z (16 days ago)
- Language: Elixir
- Size: 7.81 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# BanditMock
```elixir
Mix.install([
{:req, "~> 0.4"},
{:bandit_mock, github: "wojtekmach/bandit_mock"}
])Application.put_env(:github, :mock, true)
defmodule GitHub do
if Application.compile_env(:github, :mock, false) do
@mock __MODULE__.Mockdef stub(fun), do: BanditMock.stub(@mock, fun)
def api_token, do: "dummy"
def base_url, do: BanditMock.base_url(@mock)
else
def api_token, do: System.fetch_env!("GITHUB_API_KEY")def base_url, do: "https://api.github.com"
enddef new(options \\ []) do
Req.new(
base_url: base_url(),
auth: {:bearer, api_token()},
headers: [
x_github_api_version: "2022-11-28"
],
http_errors: :raise
)
|> Req.update(options)
enddef request!(options) do
Req.request!(new(), options)
end
endExUnit.start()
defmodule GitHubTest do
use ExUnit.Case, async: truetest "it works" do
GitHub.stub(fn conn ->
conn
|> Plug.Conn.put_resp_content_type("application/json")
|> Plug.Conn.send_resp(200, Jason.encode!(%{login: "wojtekmach"}))
end)assert GitHub.request!(url: "/user").body["login"] == "wojtekmach"
# works from child processes (of the test process) too
Task.async(fn ->
assert GitHub.request!(url: "/user").body["login"] == "wojtekmach"
end)
|> Task.await()
end
end
```## License
Copyright 2023 Wojtek Mach
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.