fix script
100% pure Swift library to stub network requests.
100% pure Swift means,
.package(url: "https://github.com/417-72KI/StubNetworkKit.git", from: "0.2.0"),
pod 'StubNetworkKit'
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.
URLSession.shared
registerStubForSharedSession()
Otherwise, you should inject URLSessionConfiguration instance that stub is registered.
URLSessionConfiguration
Sample codes with using Alamofire, APIKit or Moya exist as test-cases in StubNetworkKitTests.swift.
Alamofire
APIKit
Moya
stub(Scheme.is("https") && Host.is("foo") && Path.is("/bar")) .responseJson(["message": "Hello world!"])
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!"]) }
stub { Scheme.is("https") Host.is("foo") Path.is("/bar") Method.isGet() }.responseJson(["message": "Hello world!"])
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("Hello world!".data(using: .utf8)!)
stub() .scheme("https") .host("foo") .path("/bar") .method(.get) .responseJson(["message": "Hello world!"])
If you are looking for more examples, look at StubNetworkKitTests.swift.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
StubNetworkKit
100% pure Swift library to stub network requests.
100% pure Swift means,
Installation
Swift Package Manager(recommended)
CocoaPods
Preparation
Pure Swift is not supporting method-swizzling, therefore you have to enable stub explicitly.
If you are using
URLSession.shared
only, you can callregisterStubForSharedSession()
to enable stubs.Otherwise, you should inject
URLSessionConfiguration
instance that stub is registered.Sample codes with using
Alamofire
,APIKit
orMoya
exist as test-cases in StubNetworkKitTests.swift.Example
Basic
Switch response with conditional branches in request.
Using Result builder
Switch response with conditional branches in request.
Function chain
More examples
If you are looking for more examples, look at StubNetworkKitTests.swift.