Git Product home page Git Product logo

mocknetworking's Introduction

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.

mocknetworking's People

Contributors

machx avatar

Watchers

 avatar  avatar  avatar

mocknetworking's Issues

Review why 2 responses coming back in http response test weren't equal

In the test

func testHTTPResponse() throws {
		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: ["thing180":"thing2"]))
		
		MockURLProtocol.register(response: response, for: url)
		defer {
			MockURLProtocol.unregister()
		}
		
		var receivedResponse: URLResponse?
		let expectation = XCTestExpectation()
		URLSession.sessionWith(.ephemeral).downloadTask(with: url) { (url, response, error) in
			receivedResponse = response
			expectation.fulfill()
		}.resume()
		
		wait(for: [expectation], timeout: 5.0)
		
		XCTAssertNotNil(receivedResponse)
		guard let receivedHTTPResponse = receivedResponse as? HTTPURLResponse else {
			XCTFail("Could not convert Response to HTTPURLResponse type")
			return
		}
		XCTAssertEqual(response.url, receivedHTTPResponse.url)
		XCTAssertEqual(response.statusCode, receivedHTTPResponse.statusCode)
		XCTAssertEqual(response.allStringHTTPHeaders(), receivedHTTPResponse.allStringHTTPHeaders())
	}

if you straight ask for the 2 http responses being equal, you will get a negative response back. I suspect that the 2nd response (having gone through URLProtocol) has more fields in it than the one we supplied it to begin with. Should quickly evaluate this and see if a better equality method can be made for the unit test.

Implement Cancel request

Currently there is an API

public override func stopLoading() {
	//
}

this should probably operate like NSOperation's cancel where you make a private variable and then just check it during the course of the load logic

Change URLPropertyStore to something else

URLPropertyStore sounds too official and doesn't sound like something from this package. I'd like to find a name that sounds more like it fits in with the expected naming style from this project.

URLResponse custom isEqual func for or custom XCTAssert for unit tests

URLResponse considers fields that MockNetworking doesn't care about such as mime type in the equality comparison. For unit test purposes only a better equality method/assertion should be made using 1 of 2 methods

  1. Make a XCTAsser func that works better for URLResponse
  2. Make a better (for unit tests) equal func for comparison of 2 responses

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.