
Security News
New Website “Is It Really FOSS?” Tracks Transparency in Open Source Distribution Models
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Mock5 allows to mock external APIs with simple Sinatra Rack apps.
This gem could be useful for testing, and maybe development purposes. Add it to the relevant groups in your Gemfile.
gem "mock5", groups: [:test, :development]
and run bundle
.
Use this method to describe API you're trying to mock.
weather_api = Mock5.mock("http://weather-api.com") do
get "/weather.json" do
MultiJson.dump(
location: "Philadelphia, PA",
temperature: "60F",
description: "Sunny"
)
end
end
Use this method to enable API mocks you've defined previously.
Mock5.mount weather_api, some_other_api
Net::HTTP.get("weather-api.com", "/weather.json") # => "{\"location\":...
Unmounts passed APIs if thery were previously mounted
Mock5.unmount some_other_api # [, and_another_api... ]
This method returns a Set of all currently mounted APIs
Mock5.mounted_apis # => { weather_api }
Mock5.mount another_api
Mock5.mounted_apis # => { weather_api, another_api }
Executes the block with all given APIs mounted, and then unmounts them.
Mock5.mounted_apis # => { other_api }
Mock5.with_mounted weather_api, other_api do
Mock5.mounted_apis # => { other_api, weather_api }
run_weather_api_test_suite!
end
Mock5.mounted_apis # => { other_api }
Say you're writing a nice wrapper around remote user management REST API. You want your library to handle any unexpected situation aproppriately and show a relevant error message, or schedule a retry some time later.
Obviously, you can't rely on a production API to test all these codepaths. You probably want a way to emulate all these situations locally. Enter Mock5:
# user registers successfully
SuccessfulRegistration = Mock5.mock("http://example.com") do
post "/users" do
MultiJson.dump(
first_name: "Zapp",
last_name: "Brannigan",
email: "zapp@planetexpress.com"
)
end
end
# registration returns validation error
UnsuccessfulRegistration = Mock5.mock("http://example.com") do
post "/users" do
halt 406, MultiJson.dump(
first_name: ["is too lame"],
email: ["is not unique"]
)
end
end
# remote api is down for some reason
RegistrationUnavailable = Mock5.mock("http://example.com") do
post "/users" do
halt 503, "Service Unavailable"
end
end
# remote api times takes long time to respond
RegistrationTimeout = Mock5.mock("http://example.com") do
post "/users" do
sleep 15
end
end
describe MyApiWrapper do
describe "successfull" do
around do |example|
Mock5.with_mounted(SuccessfulRegistration, &example)
end
it "allows user registration" do
expect{ MyApiWrapper.register_user }.not_to raise_error
end
end
describe "validation errors" do
around do |example|
Mock5.with_mounted(UnsuccessfulRegistration, &example)
end
it "raises a valiation error" do
expect{ MyApiWrapper.register_user }.to raise_error(MyApiWrapper::ValidationError)
end
end
describe "service is unavailable" do
around do |example|
Mock5.with_mounted(RegistrationUnavailable, &example)
end
it "raises a ServiceUnavailable error" do
expect{ MyApiWrapper.register_user }.to raise_error(MyApiWrapper::ServiceUnavailable)
end
end
describe "timeout" do
around do |example|
Mock5.with_mounted(RegistrationTimeout, &example)
end
it "raises timeout error" do
expect{ MyApiWrapper.register_user }.to raise_error(Timeout::Error)
end
end
end
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that mock5 demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.
Security News
The Latio podcast explores how static and runtime reachability help teams prioritize exploitable vulnerabilities and streamline AppSec workflows.