417-72KI/StubNetworkKit
Stub your network requests in Swift
Installation
Swift Package Manager(recommended)
.package(url: "https://github.com/417-72KI/StubNetworkKit.git", from: "0.5.0"),CocoaPods
[!WARNING]
watchOSsupport is unavailable in CocoaPods due to dependency. If you want to use includingwatchOS, consider migrating to Swift Package Manager.
pod 'StubNetworkKit'Preparation
Pure Swift is not supporting method-swizzling, therefore you have to enable stub explicitly.
If you are using URLSession.shared only, you can call registerStubForSharedSession() to enable stubs.
Otherwise, you should inject URLSessionConfiguration instance that stub is registered.
Sample codes with using Alamofire or APIKit exist in Sample.
Example
Basic
stub(Scheme.is("https") && Host.is("foo") && Path.is("/bar"))
.responseJson(["message": "Hello world!"])Switch response with conditional branches in request.
stub(Scheme.is("https") && Host.is("foo") && Path.is("/bar")) { request in
guard request.url?.query == "q=1" else {
return .error(.unexpectedRequest($0))
}
return .json(["message": "Hello world!"])
}Using Result builder
stub {
Scheme.is("https")
Host.is("foo")
Path.is("/bar")
Method.isGet()
}.responseJson(["message": "Hello world!"])Switch response with conditional branches in request.
stub {
Scheme.is("https")
Host.is("foo")
Path.is("/bar")
Method.isGet()
} withResponse: { request in
guard request.url?.query == "q=1" else {
return .error(.unexpectedRequest($0))
}
return .json(["message": "Hello world!"])
}stub(url: "foo://bar/baz", method: .get)
.responseData(Data("Hello world!".utf8))Function chain
stub()
.scheme("https")
.host("foo")
.path("/bar")
.method(.get)
.responseJson(["message": "Hello world!"])More examples
If you are looking for more examples, look at StubNetworkKitTests.
Package Metadata
Repository: 417-72KI/StubNetworkKit
Stars: 2
Forks: 0
Open issues: 10
Default branch: main
Primary language: swift
License: MIT
README: README.md