NetworkSpyKit
is a lightweight, thread-safe HTTP spy and stub tool for testing code that performs network requests in Swift.
It allows you to:
- Record outgoing
URLRequest
s - Return predefined or dynamic stubbed responses
- Assert request behavior without hitting the real network
- Keep your tests fast, isolated, and deterministic
- 🚫 Never touches the real network
- 🧪 Spy on requests (headers, body, URL, method)
- 🎭 Stub custom responses on a per-request basis
- 🧵 Thread-safe and safe for parallel test execution
- ☕ Built-in teapot response for fun (and HTTP 418 awareness)
TODO: generic example from the test cases
import NetworkSpyKit
let spy = NetworkSpy(sessionConfiguration: .default) { request in
return .success(statusCode: 200,
bodyData: #"{"message":"Hello"}"#.data(using: .utf8))
}
let modelController = makeModelController(sessionConfiguration: spy.sessionConfiguration)
// trigger network call
try await modelController.fetchSomething()
XCTAssertEqual(spy.recordedRequests.first?.url.path, "/something")
TODO: link to Apple docs of URLSessionConfiguration
With any URLSessionConfiguration
-based library
- Inject
spy.sessionConfiguration
into your networking stack or library. - Provide a
responseProvider
closure to determine what responses should be returned.
Stubbed responses never touch the real network. All requests are intercepted at the protocol layer using a URLProtocol subclass under the hood.
NetworkSpy
uses an internal serial queue to synchronize access.- You can safely use multiple spies in parallel or across test targets.
- Isolated by using unique headers to associate intercepted requests with the correct
NetworkSpy
instance.
let spy = NetworkSpy()
spy.responseProvider = { _ in .teaPot() }
Returns:
TODO: better layout for response
Status code: 418
Headers: [:]
Body: { "ERROR": "I'm a teapot" }
Because Hyper Text Coffee Pot Control Protocol is real. Sort of.
Swift Package Manager
Add the following to your Package.swift:
.package(url: "https://github.com/yourusername/NetworkSpyKit.git", from: "1.0.0")
Then import it where needed:
import NetworkSpyKit
CocoaPods
Add the following line to your Podfile:
pod 'NetworkSpyKit'
Then run:
pod install
MIT License. See LICENSE for details.