
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.
What if WebMock and NSURLProtocol had a baby?
Update your Gemfile:
gem "webstub"
Bundle:
$ bundle install
Add the following line to the top-most describe
block in your spec:
extend WebStub::SpecHelpers
Use the following methods to control the use of request stubbing:
disable_network_access!
enable_network_access!
stub_request
reset_stubs
describe "Example" do
extend WebStub::SpecHelpers
describe "Stubbing a GET request to return a simple response after a delay" do
it "retrieves the front page" do
stub_request(:get, "http://example.com/").
to_return(body: "Hello!", content_type: "text/plain", delay: 0.3)
@body = nil
@api.get_index do |body, error|
@body = body
resume
end
wait_max 1.0 do
@body.should.be == "Hello!"
end
end
end
describe "Stubbing a GET request to return JSON" do
it "retrieves suggestions" do
stub_request(:get, "https://example.com/suggestions?q=mu").
to_return(json: { suggestions: ["muse"] })
@suggestions = nil
@api.get_suggestions("mu") do |results, error|
@suggestions = results
resume
end
wait_max 1.0 do
@suggestions.should.not.be.empty
end
end
end
describe "Stubbing a POST request to return JSON" do
it "handles a POST request" do
stub_request(:post, "https://example.com/action").
with(body: { q: "unsustainable" }).
to_return(json: [ { album: "The 2nd Law", release_date: "2012-10-01", artist: "Muse" } ])
@results = nil
@api.get_album_info_for_track("unsustainable") do |results, error|
@results = results
resume
end
wait_max 1.0 do
@results.should.not.be.empty
end
end
end
describe "Stubbing a GET request to fail" do
it "returns an NSError with the NSURLError domain" do
stub_request(:get, "https://example.com/action").
to_fail(code: NSURLErrorNotConnectedToInternet)
@error = nil
@api.get_albums do |results, error|
@error = error
resume
end
wait_max 1.0 do
@error.code.should == NSURLErrorNotConnectedToInternet
end
end
end
end
with
method's body
option accepts either a Hash or a String:
application/x-www-form-urlencoded
content type)to_return
method accepts a few options:
json
: accepts either a Hash, Array or a String. If a Hash or Array is provided, it will be converted to JSON. Strings are returned as is, with the Content-Type set to application/json
.body
: accepts a String, and returns it as-iscontent_type
: sets the Content-Type when using the body
parameterstatus_code
: sets the integer Status Code of the response. Defaults to 200
.to_redirect
method accepts:
url
: String of the URL to redirect to (required)to_return
except for status_code
to_fail
method accepts one of the following options:
code
: NSURLErrorDomain
error codeerror
: NSError
to fail the request withSometimes, you may just want to check that the request has been made to a given URL. In this case, you can use the requested?
method of the stub returned by stub_request
:
describe Elevate::HTTP do
extend WebStub::SpecHelpers
describe ".get" do
it "synchronously issues a HTTP GET request" do
stub = stub_request(:get, "http://www.example.com/")
Elevate::HTTP.get("http://www.example.com/")
stub.should.be.requested
end
end
end
Sometimes, you may want to inspect the request made for a given URL. In this case, you can use the with_callback
method of the stub returned by stub_request
to add a callback hook:
describe Elevate:HTTP do
extend WebStub::SpecHelpers
describe ".post" do
it "synchronously issues an HTTP POST request" do
stub = stub_request(:post, "http://www.example.com/")
stub.with_callback do |headers, body|
headers.kind_of?(Hash).should == true
body.kind_of?(Hash).should == true
body.should == {"key" => "value"}
end
Elevate::HTTP.post("http://www.example.com/", json: {key: "value"})
end
end
end
While WebStub supports NSURLSession
, it does not support background sessions, as they don't allow the use of custom NSURLProtocol
classes.
FAQs
Unknown package
We found that webstub 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.