Skip to content

Commit

Permalink
Merge pull request #131 from nethraravindran/simpleClient
Browse files Browse the repository at this point in the history
Fix for flaky test ClientE2ETests.testSimpleHTTPClient
  • Loading branch information
Pushkar N Kulkarni authored Dec 29, 2018
2 parents 71bb886 + 9feb8ec commit 60d5af5
Showing 1 changed file with 33 additions and 16 deletions.
49 changes: 33 additions & 16 deletions Tests/KituraNetTests/ClientE2ETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,23 +103,25 @@ 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))")
if let contentType = response?.headers["Content-type"] {
XCTAssertEqual(contentType, ["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 +337,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 {
XCTFail("Error while writing response")
}
}
}
}

0 comments on commit 60d5af5

Please sign in to comment.