https://github.com/417-72ki/stubnetworkkit
Stub your network requests in Swift
https://github.com/417-72ki/stubnetworkkit
Last synced: 27 days ago
JSON representation
Stub your network requests in Swift
- Host: GitHub
- URL: https://github.com/417-72ki/stubnetworkkit
- Owner: 417-72KI
- License: mit
- Created: 2022-04-27T15:44:25.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-15T19:46:51.000Z (about 2 months ago)
- Last Synced: 2025-04-27T11:05:27.878Z (about 1 month ago)
- Language: Swift
- Homepage:
- Size: 230 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# StubNetworkKit
[](https://github.com/417-72KI/StubNetworkKit/actions/workflows/ci.yml)
[](https://github.com/417-72KI/StubNetworkKit/releases)
[](https://swiftpackageindex.com/417-72KI/StubNetworkKit)
[](https://swiftpackageindex.com/417-72KI/StubNetworkKit)
[](http://cocoapods.org/pods/StubNetworkKit)
[](http://cocoapods.org/pods/StubNetworkKit)
[](https://github.com/417-72KI/StubNetworkKit/blob/main/LICENSE)**100% pure Swift** library to stub network requests.
**100% pure Swift** means,
- No more Objective-C API
- Testable also in other than Apple platform (e.g. Linux)## Installation
### Swift Package Manager(recommended)```swift:Package.swift
.package(url: "https://github.com/417-72KI/StubNetworkKit.git", from: "0.5.0"),
```### CocoaPods
> [!WARNING]
>
> `watchOS` support is unavailable in CocoaPods due to dependency.
> If you want to use including `watchOS`, consider migrating to Swift Package Manager.```ruby:Podfile
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](https://github.com/417-72KI/StubNetworkKit/tree/main/Tests/StubNetworkKitTests/Sample).
## Example
### Basic```swift
stub(Scheme.is("https") && Host.is("foo") && Path.is("/bar"))
.responseJson(["message": "Hello world!"])
```#### Switch response with conditional branches in request.
```swift
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
```swift
stub {
Scheme.is("https")
Host.is("foo")
Path.is("/bar")
Method.isGet()
}.responseJson(["message": "Hello world!"])
```#### Switch response with conditional branches in request.
```swift
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!"])
}
```###
```swift
stub(url: "foo://bar/baz", method: .get)
.responseData(Data("Hello world!".utf8))
```### Function chain
```swift
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](https://github.com/417-72KI/StubNetworkKit/blob/main/Tests/StubNetworkKitTests).