https://github.com/bdbergeron/stubby
A little URLSession response stubbing library.
https://github.com/bdbergeron/stubby
ios macos swift urlsession
Last synced: about 2 months ago
JSON representation
A little URLSession response stubbing library.
- Host: GitHub
- URL: https://github.com/bdbergeron/stubby
- Owner: bdbergeron
- License: mit
- Created: 2023-09-17T22:57:16.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-12T22:03:53.000Z (over 2 years ago)
- Last Synced: 2024-03-15T07:48:12.317Z (over 2 years ago)
- Topics: ios, macos, swift, urlsession
- Language: Swift
- Homepage: https://stubby.brad.app/documentation/stubby/
- Size: 329 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 🥸 Stubby
A little URLSession response stubbing library.

[](https://codecov.io/gh/bdbergeron/Stubby)
[](https://www.codefactor.io/repository/github/bdbergeron/stubby)
## Getting Started
Add Stubby to your project via Swift Package Manager:
```swift
.package(url: "https://github.com/bdbergeron/Stubby", from: "1.0.0"),
```
In your Tests folder, create an object that conforms to `StubbyResponseProvider`:
```swift
import Foundation
import Stubby
import XCTest
extension URL {
static let repoURL = URL(string: "https://bdbergeron.github.io")!
static let githubURL = URL(string: "https://github.com")!
}
struct SomeResponseProvider: StubbyResponseProvider {
static func respondsTo(request: URLRequest) -> Bool {
true
}
static func response(for request: URLRequest) throws -> Result {
switch try XCTUnwrap(request.url) {
case .repoURL:
return try .success(.init(data: try XCTUnwrap("Hello, world!".data(using: .utf8)), for: .repoURL))
default:
return .failure(URLError(.unsupportedURL))
}
}
}
```
And now you can use your response provider in your tests:
```swift
import Stubby
import XCTest
final class StubbyTests: XCTestCase {
func test_stubbedResponse_succeeds() async {
let urlSession = URLSession.stubbed(responseProvider: SomeResponseProvider.self)
let request = URLRequest(url: .repoURL)
do {
let (data, _) = try await urlSession.data(for: request)
let string = String(data: data, encoding: .utf8)
XCTAssertEqual(string, "Hello, world!")
} catch {
XCTFail("Unexpected error: \(error)")
}
}
func test_stubbyResponse_failsWithUnsupportedURLError() async {
let urlSession = URLSession.stubbed(responseProvider: SomeResponseProvider.self)
let request = URLRequest(url: .githubURL)
do {
_ = try await urlSession.data(for: request)
XCTFail("Should fail.")
} catch let error as NSError {
XCTAssertEqual(error.domain, URLError.errorDomain)
let expectedError = URLError(.unsupportedURL)
XCTAssertEqual(error.code, expectedError.errorCode)
XCTAssertEqual(error.localizedDescription, expectedError.localizedDescription)
} catch {
XCTFail("Unexpected error: \(error)")
}
}
}
```