Latest 0.0.7
Homepage https://github.com/depoon/SwiftLocalhost
License MIT
Platforms ios 10.0, requires ARC
Dependencies Criollo
Authors

Simple framework that creates a mock localhost server primarily used for XCUITest.

Installation

CocoaPods

SwiftLocalhost is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'SwiftLocalhost'

Under the hood

Swiftlocalhost uses Criollo library as the in-memory web server.

How to use – Follow these 4 steps to setup

1. [Localhost] – pod install SwiftLocalhost to your XCUITest target.

There are 2 ways to create an instance of a Localhost.

Using a specific port number

LocalhostServer(portNumber: 9001)

Getting an instance with a random unused port number assigned to it. This is important if you want to execute your tests parallelly

LocalhostServer.initializeUsingRandomPortNumber()

An example of how a XCTestCase class looks like

import SwiftLocalhost

class MovieListTest: XCTestCase {

    var localhostServer: LocalhostServer!

    override func setUp() {
        continueAfterFailure = false
        self.localhostServer = LocalhostServer.initializeUsingRandomPortNumber()
        self.localhostServer.startListening()
    }

    override func tearDown() {
        self.localhostServer.stopListening()
    }
}

2. [API] – Modify App API Requests with http://localhost:xxxx (where xxxx is port number)

If you are using a specific port for your localhost, you can simply change the domain to http://localhost:xxxx.

class NetworkOperations {

    //private let baseUrl: String = "https://api.themoviedb.org/3/movie"

    private let baseUrl: String = "http://localhost:9001/3/movie"
}

If you are using random port numbers in your tests, then you will need to pass the port information into your app as launch arguments.

//In Test Runner, pass the port information using launchArguments
let app = XCUIApplication()
app.launchArguments = ["port:(self.localhostServer.portNumber)"]
app.launch()

//In Application - Read the ProcessInfo Arguments
ProcessInfo.processInfo.arguments

If you need to redirect 3rd party libraries (eg. Firebase, Google Analytics) to the localhost server, you can use NetworkInterceptor pod created by Kenneth Poon.

3. [Info.plist] – Modify App Info.plist

Since we will be using http protocol to communicate with our localhost server, we will need to add an exception domain for localhost in your Info.plist file.

picture alt

You will also need to disable SSL Pinning if needed.

4. [Mock Responses] – Setup localhost server with mock responses.

You can setup specific mock response according to your test case needs. Set the Data instance of the response file as a response to a specific path that your test cases will be covering.

//let portNumber: UInt = 9001

//Binary of Mock Response Json File
//let jsonFileData: Data! 

self.localhostServer.get("/3/movie/popular", routeBlock: { request in
    let requestURL: URL = URL(string: "http://localhost:(portNumber)/3/movie/popular")!
    let httpUrlResponse = HTTPURLResponse(url: requestURL, statusCode: 200, httpVersion: nil, headerFields: ["Content-Type":"application/json"])!
    return LocalhostServerResponse(httpUrlResponse: httpUrlResponse, data: jsonFileData)
})

Latest podspec

{
    "name": "SwiftLocalhost",
    "version": "0.0.7",
    "swift_version": "4.2",
    "summary": "Swift Localhost Server for your testing needs",
    "description": "Featuresn1. Mock Server to return stub HTTPURLResponse(s)n2. Mock Server to record and assert URLRequest(s)n3. Mock Server to record and assert Google Analytics Events",
    "homepage": "https://github.com/depoon/SwiftLocalhost",
    "license": "MIT",
    "authors": {
        "depoon": "[email protected]"
    },
    "source": {
        "git": "https://github.com/depoon/SwiftLocalhost.git",
        "tag": "0.0.7"
    },
    "platforms": {
        "ios": "10.0"
    },
    "requires_arc": true,
    "source_files": "Source/**/*",
    "dependencies": {
        "Criollo": []
    }
}

Pin It on Pinterest

Share This