Skip to content

Commit

Permalink
Fix for flaky test ClientE2ETests.testSimpleHTTPClient
Browse files Browse the repository at this point in the history
  • Loading branch information
nethraravindran committed Dec 29, 2018
1 parent d3c5a9e commit edf3233
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions Tests/KituraNetTests/ClientE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,23 @@ class ClientE2ETests: KituraNetTest {
}

func testSimpleHTTPClient() {
let expectation = self.expectation(description: "HTTP client response completed")
let url = "http://httpbin.org"

_ = HTTP.get(url) { response in
XCTAssertNotNil(response, "ERROR!!! ClientRequest response object was nil")
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "HTTP Status code was \(String(describing: response?.statusCode))")
let contentType = response?.headers["Content-Type"]
XCTAssertNotNil(contentType, "No ContentType header in response")
if let contentType = contentType {
XCTAssertEqual(contentType, ["text/html; charset=utf-8"], "Content-Type header wasn't `text/html`")
}
expectation.fulfill()
}
let delegate = TestSimpleClientDelegate()
performServerTest(delegate, asyncTasks: { expectation in
self.performRequest("get", path: "/" , callback: { response in
XCTAssertEqual(response?.statusCode, HTTPStatusCode.OK, "Status code wasn't .OK was \(String(describing: response?.statusCode))")
XCTAssertEqual(response?.headers["Content-type"], ["text/plain"], "Content-Type wasn't text/plain")

waitExpectation(timeout: 10) { error in
XCTAssertNil(error)
}
do {
let result = try response?.readString()
XCTAssertNotNil(result, "The body of the response was empty")
XCTAssertEqual(result?.count, 13, "Result should have been 13 bytes, was \(String(describing: result?.count))")
XCTAssertEqual(result, "Hello, World!")
} catch {
XCTFail("Failed reading the body of the response")
}
expectation.fulfill()
})
})
}

func testPostRequests() {
Expand Down Expand Up @@ -335,4 +335,19 @@ class ClientE2ETests: KituraNetTest {
}
}
}

class TestSimpleClientDelegate: ServerDelegate {

func handle(request: ServerRequest, response: ServerResponse) {
do {
let result: String = "Hello, World!"
response.statusCode = .OK
response.headers["Content-Type"] = ["text/plain"]
response.headers["Content-Length"] = ["\(result.count)"]
try response.end(text: result)
} catch {
print("Error while writing response")
}
}
}
}

0 comments on commit edf3233

Please sign in to comment.