Swiftpack.co - Machx/MockNetworking as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Machx.
Machx/MockNetworking 0.2.1
Apply URLProtocol to XCTests for Mock Networking API's
⭐️ 0
🕓 1 year ago
iOS macOS
.package(url: "https://github.com/Machx/MockNetworking.git", from: "0.2.1")

MockNetworking

MockNetworking is a Swift package allowing the replaying of mock responses to network requests through Apple Networking API's by tying into the URLProtocol system.

Installation

MockNetworking is available for integration into Xcode through the Swift Package Manager. Point to https://github.com/Machx/MockNetworking.git in Xcode when adding the new package.

Basic Usage

To setup a mock response you simply need to register the response and then unregister the class when appropraite. In the example below we'll setup a response for 1 test.

let url = try XCTUnwrap(URL(string: "https://wwww.apple.com"))
let response = try XCTUnwrap(HTTPURLResponse(url: url,
					statusCode: 200,
					httpVersion: HTTPURLResponse.HTTP_1_1,
					headerFields: nil))

MockURLProtocol.register(response: response, for: url)
defer {
	MockURLProtocol.unregister()
}

This uses HTTPURLResponse, there is also a custom api available.

let url = try XCTUnwrap(URL(string: "https://wwww.apple.com"))
let mockResponse = MockPropertyResponse(url: url,
					status: 200,
					headerFields: [:])

MockURLProtocol.regigsterMock(response: mockResponse, for: url)
defer {
	MockURLProtocol.unregister()
}

The register api both registers MockURLProtocol with the URLProtocol system if it isn't already registered and stores the prepared response for the given url.

License

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

GitHub

link
Stars: 0
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Release Notes

Release 0.1.0
1 year ago

This is the first release of MockNetworking Swift Package. MockNetworking is primarily intended to be used in Unit Testing Network Related code.

  • MockURLProtocol for setting up Mock Network Responses
  • Support for HTTP Headers
  • Support for returning Body Data
  • Support for returning Errors
  • Support for HTTP Status Codes

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics