Skip to content

Commit b6059f2

Browse files
author
Pushkar Kulkarni
committed
cherry-pick 413c913 and fix conflicts
1 parent 78790ea commit b6059f2

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

TestFoundation/HTTPServer.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,14 @@ struct _HTTPRequest {
191191
body = lines.last!
192192
}
193193

194+
public func getCommaSeparatedHeaders() -> String {
195+
var allHeaders = ""
196+
for header in headers {
197+
allHeaders += header + ","
198+
}
199+
return allHeaders
200+
}
201+
194202
}
195203

196204
struct _HTTPResponse {
@@ -240,18 +248,24 @@ public class TestURLSessionServer {
240248
}
241249

242250
func process(request: _HTTPRequest) -> _HTTPResponse {
243-
if request.method == .GET {
244-
return getResponse(uri: request.uri)
251+
if request.method == .GET || request.method == .POST {
252+
return getResponse(request: request)
245253
} else {
246254
fatalError("Unsupported method!")
247255
}
248256
}
249257

250-
func getResponse(uri: String) -> _HTTPResponse {
258+
func getResponse(request: _HTTPRequest) -> _HTTPResponse {
259+
let uri = request.uri
251260
if uri == "/country.txt" {
252261
let text = capitals[String(uri.characters.dropFirst())]!
253262
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.characters.count)", body: text)
254263
}
264+
265+
if uri == "/requestHeaders" {
266+
let text = request.getCommaSeparatedHeaders()
267+
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.characters.count)", body: text)
268+
}
255269
return _HTTPResponse(response: .OK, body: capitals[String(uri.characters.dropFirst())]!)
256270
}
257271

TestFoundation/TestNSURLSession.swift

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class TestURLSession : XCTestCase {
3333
("test_taskError", test_taskError),
3434
("test_taskCopy", test_taskCopy),
3535
("test_taskTimeout", test_taskTimeout),
36+
("test_verifyRequestHeaders", test_verifyRequestHeaders),
3637
]
3738
}
3839

@@ -65,22 +66,22 @@ class TestURLSession : XCTestCase {
6566
serverReady.wait()
6667
let urlString = "http://127.0.0.1:\(serverPort)/Nepal"
6768
let url = URL(string: urlString)!
68-
let d = DataTask(with: expectation(description: "data task"))
69+
let d = DataTask(with: expectation(description: "data task"))
6970
d.run(with: url)
7071
waitForExpectations(timeout: 12)
7172
if !d.error {
7273
XCTAssertEqual(d.capital, "Kathmandu", "test_dataTaskWithURLRequest returned an unexpected result")
7374
}
7475
}
75-
76+
7677
func test_dataTaskWithURLCompletionHandler() {
7778
let serverReady = ServerSemaphore()
7879
globalDispatchQueue.async {
7980
do {
8081
try self.runServer(with: serverReady)
8182
} catch {
8283
XCTAssertTrue(true)
83-
return
84+
return
8485
}
8586
}
8687
serverReady.wait()
@@ -99,7 +100,7 @@ class TestURLSession : XCTestCase {
99100
}
100101

101102
let httpResponse = response as! HTTPURLResponse?
102-
XCTAssertEqual(200, httpResponse!.statusCode, "HTTP response code is not 200")
103+
XCTAssertEqual(200, httpResponse!.statusCode, "HTTP response code is not 200")
103104
expectedResult = String(data: data!, encoding: String.Encoding.utf8)!
104105
XCTAssertEqual("Washington, D.C.", expectedResult, "Did not receive expected value")
105106
expect.fulfill()
@@ -121,7 +122,7 @@ class TestURLSession : XCTestCase {
121122
serverReady.wait()
122123
let urlString = "http://127.0.0.1:\(serverPort)/Peru"
123124
let urlRequest = URLRequest(url: URL(string: urlString)!)
124-
let d = DataTask(with: expectation(description: "data task"))
125+
let d = DataTask(with: expectation(description: "data task"))
125126
d.run(with: urlRequest)
126127
waitForExpectations(timeout: 12)
127128
if !d.error {
@@ -175,7 +176,7 @@ class TestURLSession : XCTestCase {
175176
}
176177
serverReady.wait()
177178
let urlString = "http://127.0.0.1:\(serverPort)/country.txt"
178-
let url = URL(string: urlString)!
179+
let url = URL(string: urlString)!
179180
let d = DownloadTask(with: expectation(description: "download task with delegate"))
180181
d.run(with: url)
181182
waitForExpectations(timeout: 12)
@@ -250,7 +251,7 @@ class TestURLSession : XCTestCase {
250251
task.resume()
251252
waitForExpectations(timeout: 12)
252253
}
253-
254+
254255
func test_finishTasksAndInvalidate() {
255256
let invalidateExpectation = expectation(description: "URLSession wasn't invalidated")
256257
let delegate = SessionDelegate(invalidateExpectation: invalidateExpectation)
@@ -265,7 +266,7 @@ class TestURLSession : XCTestCase {
265266
session.finishTasksAndInvalidate()
266267
waitForExpectations(timeout: 12)
267268
}
268-
269+
269270
func test_taskError() {
270271
let url = URL(string: "http://127.0.0.1:\(serverPort)/Nepal")!
271272
let session = URLSession(configuration: URLSessionConfiguration.default,
@@ -280,25 +281,54 @@ class TestURLSession : XCTestCase {
280281
}
281282
//should result in Bad URL error
282283
task.resume()
283-
284+
284285
waitForExpectations(timeout: 5) { error in
285286
XCTAssertNil(error)
286-
287+
287288
XCTAssertNotNil(task.error)
288289
XCTAssertEqual((task.error as? URLError)?.code, .badURL)
289290
}
290291
}
291-
292+
292293
func test_taskCopy() {
293294
let url = URL(string: "http://127.0.0.1:\(serverPort)/Nepal")!
294295
let session = URLSession(configuration: URLSessionConfiguration.default,
295296
delegate: nil,
296297
delegateQueue: nil)
297298
let task = session.dataTask(with: url)
298-
299+
299300
XCTAssert(task.isEqual(task.copy()))
300301
}
301302

303+
func test_verifyRequestHeaders() {
304+
let serverReady = ServerSemaphore()
305+
globalDispatchQueue.async {
306+
do {
307+
try self.runServer(with: serverReady)
308+
} catch {
309+
XCTAssertTrue(true)
310+
return
311+
}
312+
}
313+
serverReady.wait()
314+
let config = URLSessionConfiguration.default
315+
config.timeoutIntervalForRequest = 5
316+
let session = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
317+
var expect = expectation(description: "download task with handler")
318+
var req = URLRequest(url: URL(string: "http://127.0.0.1:\(serverPort)/requestHeaders")!)
319+
let headers = ["header1": "value1"]
320+
req.httpMethod = "POST"
321+
req.allHTTPHeaderFields = headers
322+
var task = session.dataTask(with: req) { (data, _, error) -> Void in
323+
defer { expect.fulfill() }
324+
let headers = String(data: data!, encoding: String.Encoding.utf8)!
325+
XCTAssertNotNil(headers.range(of: "header1: value1"))
326+
}
327+
task.resume()
328+
329+
waitForExpectations(timeout: 30)
330+
}
331+
302332
func test_taskTimeout() {
303333
let serverReady = ServerSemaphore()
304334
globalDispatchQueue.async {
@@ -320,7 +350,7 @@ class TestURLSession : XCTestCase {
320350
XCTAssertNil(error)
321351
}
322352
task.resume()
323-
353+
324354
waitForExpectations(timeout: 30)
325355
}
326356
}
@@ -343,7 +373,7 @@ class DataTask : NSObject {
343373
public var error = false
344374

345375
init(with expectation: XCTestExpectation) {
346-
dataTaskExpectation = expectation
376+
dataTaskExpectation = expectation
347377
}
348378

349379
func run(with request: URLRequest) {
@@ -353,7 +383,7 @@ class DataTask : NSObject {
353383
task = session.dataTask(with: request)
354384
task.resume()
355385
}
356-
386+
357387
func run(with url: URL) {
358388
let config = URLSessionConfiguration.default
359389
config.timeoutIntervalForRequest = 8
@@ -377,7 +407,7 @@ extension DataTask : URLSessionTaskDelegate {
377407
dataTaskExpectation.fulfill()
378408
self.error = true
379409
}
380-
}
410+
}
381411

382412
class DownloadTask : NSObject {
383413
var totalBytesWritten: Int64 = 0
@@ -407,12 +437,12 @@ class DownloadTask : NSObject {
407437
}
408438

409439
extension DownloadTask : URLSessionDownloadDelegate {
410-
440+
411441
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64,
412442
totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) -> Void {
413443
self.totalBytesWritten = totalBytesWritten
414444
}
415-
445+
416446
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
417447
do {
418448
let attr = try FileManager.default.attributesOfItem(atPath: location.path)

0 commit comments

Comments
 (0)