我正在尝试使用OHHTTPStubs
测试我的HTTP请求是否包含cookie。不幸的是,我发现我的存根无法到达Cookie标头。
我的测试非常简单:
func test001_GET_WithSetCookie() {
let stubResponse = self.expectation(description: "Server stub response successful")
OHHTTPStubs.removeAllStubs()
OHHTTPStubs.stubRequests(passingTest: {
request in
let requestHeaders = request.allHTTPHeaderFields
NSLog("REQUEST ====> all header fields are: \(requestHeaders)") // I am expecting cookie header here, while performing second request;
NSLog("REQUEST ====> Shared cookies at start: \(self.cookieStorage.cookies?.count)")
return true
}) { (URLRequest) -> OHHTTPStubsResponse in
let localResponseHeaders = [
"Content-Type" : "text/html;charset=ISO-8859-1",
"Connection" : "keep-alive",
"Expires" : "Tue, 1 Jan 2036 18:43:49 GMT",
"Cache-Control" : "max-age=0",
"Location" : "http://my-stub.com",
"Server" : "openresty/1.9.3.1",
"Set-Cookie" : "testCookie1=Oreo; expires=Wed, 23 Oct 2030 11:32:59 GMT; path=/; domain=my-stub.com"
]
stubResponse.fulfill()
return OHHTTPStubsResponse(data: Data(), statusCode: TestHttpResponse.ok.httpCode, headers: localResponseHeaders)
}
self.deleteAllStoredCookies()
let url_A = URL(string: "http://my-stub.com/hello.html")!
let url_B = URL(string: "http://my-stub.com/bye.html")!
var request_A = URLRequest(url: url_A)
request_A.httpMethod = "GET"
request_A.cachePolicy = .reloadIgnoringLocalCacheData
var request_B = URLRequest(url: url_B)
request_B.httpMethod = "GET"
request_B.cachePolicy = .reloadIgnoringLocalCacheData
let getRequestExpectationA = self.expectation(description: "Server GET request successful")
let getDataTaskA = URLSession.shared.dataTask(with: request_A, completionHandler: {
(data, response, error) in
XCTAssertNil(error, "Error occured while completing the request")
XCTAssertTrue((response as! HTTPURLResponse).statusCode == TestHttpResponse.ok.httpCode, "Request didn't complete succesfully.")
getRequestExpectationA.fulfill()
})
getDataTaskA.resume()
self.asyncWaitForExpectations(timeout: 30, handler: {
error in
if (error != nil) {
NSLog("Timeout error: \(error)")
}
})
/*
MARK: First request complete
*/
let getRequestExpectationB = self.expectation(description: "Server GET request successful")
let getDataTaskB = URLSession.shared.dataTask(with: request_B, completionHandler: {
(data, response, error) in
XCTAssertNil(error, "Error occured while completing the request")
XCTAssertTrue((response as! HTTPURLResponse).statusCode == TestHttpResponse.ok.httpCode, "Request didn't complete succesfully.")
getRequestExpectationB.fulfill()
})
getDataTaskB.resume()
self.asyncWaitForExpectations(timeout: 30, handler: {
error in
if (error != nil) {
NSLog("Timeout error: \(error)")
}
})
}
func deleteAllStoredCookies() {
let cookies = cookieStorage.cookies;
cookies?.forEach({
cookie in
NSLog("Deleting Cookie: \(cookie.name), domain=\(cookie.domain), path=\(cookie.path), expires=\(cookie.expiresDate)")
cookieStorage.deleteCookie(cookie)
})
}
在我看到的日志中,存根内部没有检索到标头:
2020-06-07 17:48:46.418544+0200 MapSdkTestHostApp[11372:244231] REQUEST ====> all header fields are: Optional([:])
2020-06-07 17:48:46.418719+0200 MapSdkTestHostApp[11372:244231] REQUEST ====> Shared cookies at start: Optional(0)
2020-06-07 17:48:46.421933+0200 MapSdkTestHostApp[11372:244354] REQUEST ====> all header fields are: Optional([:])
2020-06-07 17:48:46.422097+0200 MapSdkTestHostApp[11372:244354] REQUEST ====> Shared cookies at start: Optional(0)
2020-06-07 17:48:46.425614+0200 MapSdkTestHostApp[11372:244239] REQUEST ====> all header fields are: Optional([:])
2020-06-07 17:48:46.425783+0200 MapSdkTestHostApp[11372:244239] REQUEST ====> Shared cookies at start: Optional(1)
2020-06-07 17:48:46.426121+0200 MapSdkTestHostApp[11372:244354] REQUEST ====> all header fields are: Optional([:])
2020-06-07 17:48:46.426241+0200 MapSdkTestHostApp[11372:244354] REQUEST ====> Shared cookies at start: Optional(1)
我试图执行相同的测试,涉及本地主机服务器而不是OHHTTP存根,并且可以在执行第二个请求时在服务器端检索cookie。