MockNetworking

0.1.0

Apply URLProtocol to XCTests for Mock Networking API's
Machx/MockNetworking

What's New

Release 0.1.0

2020-10-29T04:08:58Z

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

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.

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

  • None
Last updated: Wed Nov 06 2024 02:32:54 GMT-1000 (Hawaii-Aleutian Standard Time)