Revision control

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
import UIKit
6
import GCDWebServers
7
import XCTest
8
9
/// Minimal web server tests. This class can be used as a base class for tests that need a real web server.
10
/// Simply add additional handlers your test class' setUp() method.
11
class WebServerTests: XCTestCase {
12
let webServer: GCDWebServer = GCDWebServer()
13
var webServerBase: String!
14
15
/// Setup a basic web server that binds to a random port and that has one default handler on /hello
16
fileprivate func setupWebServer() {
17
webServer.addHandler(forMethod: "GET", path: "/hello", request: GCDWebServerRequest.self) { (request) -> GCDWebServerResponse in
18
return GCDWebServerDataResponse(html: "<html><body><p>Hello World</p></body></html>")!
19
}
20
if webServer.start(withPort: 0, bonjourName: nil) == false {
21
XCTFail("Can't start the GCDWebServer")
22
}
23
webServerBase = "http://localhost:\(webServer.port)"
24
}
25
26
override func setUp() {
27
super.setUp()
28
setupWebServer()
29
}
30
31
override func tearDown() {
32
super.tearDown()
33
}
34
35
func testWebServerIsRunning() {
36
XCTAssertTrue(webServer.isRunning)
37
}
38
39
func testWebServerIsServingRequests() {
40
let response = try? String(contentsOf: URL(string: "\(webServerBase!)/hello")!, encoding: .utf8)
41
XCTAssertNotNil(response)
42
XCTAssertTrue(response == "<html><body><p>Hello World</p></body></html>")
43
}
44
}