Merge pull request #1012 from pushkarnk/duplicate-rules

diff --git a/Foundation.xcodeproj/project.pbxproj b/Foundation.xcodeproj/project.pbxproj
index 99f3978..2e49756 100644
--- a/Foundation.xcodeproj/project.pbxproj
+++ b/Foundation.xcodeproj/project.pbxproj
@@ -765,6 +765,7 @@
 		90E645DE1E4C89A400D0D47C /* TestNSCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSCache.swift; sourceTree = "<group>"; };
 		A5A34B551C18C85D00FD972B /* TestNSByteCountFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSByteCountFormatter.swift; sourceTree = "<group>"; };
 		AE35A1851CBAC85E0042DB84 /* SwiftFoundation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SwiftFoundation.h; sourceTree = "<group>"; };
+		B167A6641ED7303F0040B09A /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
 		BD8042151E09857800487EB8 /* TestNSLengthFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSLengthFormatter.swift; sourceTree = "<group>"; };
 		BDBB658F1E256BFA001A7286 /* TestNSEnergyFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSEnergyFormatter.swift; sourceTree = "<group>"; };
 		BDFDF0A61DFF5B3E00C04CC5 /* TestNSPersonNameComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSPersonNameComponents.swift; sourceTree = "<group>"; };
@@ -956,6 +957,7 @@
 		5B5D88531BBC938800234F36 = {
 			isa = PBXGroup;
 			children = (
+				B167A6641ED7303F0040B09A /* README.md */,
 				5BDC3F2C1BCC5DB500ED97BB /* Foundation */,
 				EAB57B681BD1A255004AC5C5 /* CoreFoundation */,
 				EA66F65B1BF2DF3200136161 /* Bootstrap */,
diff --git a/TestFoundation/TestNSURLSession.swift b/TestFoundation/TestNSURLSession.swift
index 493f0da..d06e88d 100644
--- a/TestFoundation/TestNSURLSession.swift
+++ b/TestFoundation/TestNSURLSession.swift
@@ -24,19 +24,19 @@
 //Disabling to avoid https://bugs.swift.org/browse/SR-4677 and a timeout failure
 //            ("test_dataTaskWithURL", test_dataTaskWithURL),
 //            ("test_dataTaskWithURLRequest", test_dataTaskWithURLRequest),
-//            ("test_dataTaskWithURLCompletionHandler", test_dataTaskWithURLCompletionHandler),
-//            ("test_dataTaskWithURLRequestCompletionHandler", test_dataTaskWithURLRequestCompletionHandler),
+            ("test_dataTaskWithURLCompletionHandler", test_dataTaskWithURLCompletionHandler),
+            ("test_dataTaskWithURLRequestCompletionHandler", test_dataTaskWithURLRequestCompletionHandler),
 //            ("test_downloadTaskWithURL", test_downloadTaskWithURL),
 //            ("test_downloadTaskWithURLRequest", test_downloadTaskWithURLRequest),
-//            ("test_downloadTaskWithRequestAndHandler", test_downloadTaskWithRequestAndHandler),
-//            ("test_downloadTaskWithURLAndHandler", test_downloadTaskWithURLAndHandler),
+            ("test_downloadTaskWithRequestAndHandler", test_downloadTaskWithRequestAndHandler),
+            ("test_downloadTaskWithURLAndHandler", test_downloadTaskWithURLAndHandler),
 //            ("test_finishTaskAndInvalidate", test_finishTasksAndInvalidate),
 //            ("test_taskError", test_taskError),
-//            ("test_taskCopy", test_taskCopy),
+            ("test_taskCopy", test_taskCopy),
 //            ("test_cancelTask", test_cancelTask),
 //            ("test_taskTimeout", test_taskTimeout),
-//            ("test_verifyRequestHeaders", test_verifyRequestHeaders),
-//            ("test_verifyHttpAdditionalHeaders", test_verifyHttpAdditionalHeaders),
+            ("test_verifyRequestHeaders", test_verifyRequestHeaders),
+            ("test_verifyHttpAdditionalHeaders", test_verifyHttpAdditionalHeaders),
             ("test_timeoutInterval", test_timeoutInterval),
         ]
     }
@@ -97,17 +97,14 @@
         let expect = expectation(description: "URL test with completion handler")
         var expectedResult = "unknown"
         let task = session.dataTask(with: url) { data, response, error in
-            if let e = error as? URLError {
-                XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
-                expect.fulfill()
-                return
-            }
-
-            let httpResponse = response as! HTTPURLResponse?
-            XCTAssertEqual(200, httpResponse!.statusCode, "HTTP response code is not 200")
-            expectedResult = String(data: data!, encoding: String.Encoding.utf8)!
+            defer { expect.fulfill() }
+            XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
+            XCTAssertNotNil(response)
+            XCTAssertNotNil(data)
+            guard let httpResponse = response as? HTTPURLResponse, let data = data else { return }
+            XCTAssertEqual(200, httpResponse.statusCode, "HTTP response code is not 200")
+            expectedResult = String(data: data, encoding: String.Encoding.utf8) ?? ""
             XCTAssertEqual("Washington, D.C.", expectedResult, "Did not receive expected value")
-            expect.fulfill()
         }
         task.resume()
         waitForExpectations(timeout: 12)
@@ -153,16 +150,14 @@
         let expect = expectation(description: "URL test with completion handler")
         var expectedResult = "unknown"
         let task = session.dataTask(with: urlRequest) { data, response, error in
-            if let e = error as? URLError {
-                XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
-                expect.fulfill()
-                return
-            }
-            let httpResponse = response as! HTTPURLResponse?
-            XCTAssertEqual(200, httpResponse!.statusCode, "HTTP response code is not 200")
-            expectedResult = String(data: data!, encoding: String.Encoding.utf8)!
+            defer { expect.fulfill() }
+            XCTAssertNotNil(data)
+            XCTAssertNotNil(response)
+            XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
+            guard let httpResponse = response as? HTTPURLResponse, let data = data else { return }
+            XCTAssertEqual(200, httpResponse.statusCode, "HTTP response code is not 200")
+            expectedResult = String(data: data, encoding: String.Encoding.utf8) ?? ""
             XCTAssertEqual("Rome", expectedResult, "Did not receive expected value")
-            expect.fulfill()
         }
         task.resume()
         waitForExpectations(timeout: 12)
@@ -221,9 +216,7 @@
         let expect = expectation(description: "download task with handler")
         let req = URLRequest(url: URL(string: "http://127.0.0.1:\(serverPort)/country.txt")!)
         let task = session.downloadTask(with: req) { (_, _, error) -> Void in
-            if let e = error as? URLError {
-                XCTAssertEqual(e.code, .timedOut, "Unexpected error code")
-            }
+            XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
             expect.fulfill()
         }
         task.resume()
@@ -344,7 +337,10 @@
         req.allHTTPHeaderFields = headers
         var task = session.dataTask(with: req) { (data, _, error) -> Void in
             defer { expect.fulfill() }
-            let headers = String(data: data!, encoding: String.Encoding.utf8)!
+            XCTAssertNotNil(data)
+            XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
+            guard let data = data else { return }
+            let headers = String(data: data, encoding: String.Encoding.utf8) ?? ""
             XCTAssertNotNil(headers.range(of: "header1: value1"))
         }
         task.resume()
@@ -377,7 +373,9 @@
         req.allHTTPHeaderFields = headers
         var task = session.dataTask(with: req) { (data, _, error) -> Void in
             defer { expect.fulfill() }
-            let headers = String(data: data!, encoding: String.Encoding.utf8)!
+            XCTAssertNotNil(data)
+            XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
+            let headers = String(data: data!, encoding: String.Encoding.utf8) ?? ""
             XCTAssertNotNil(headers.range(of: "header1: rvalue1"))
             XCTAssertNotNil(headers.range(of: "header2: rvalue2"))
             XCTAssertNotNil(headers.range(of: "header3: svalue3"))
@@ -405,7 +403,7 @@
         let req = URLRequest(url: URL(string: "http://127.0.0.1:\(serverPort)/Peru")!)
         var task = session.dataTask(with: req) { (data, _, error) -> Void in
             defer { expect.fulfill() }
-            XCTAssertNil(error)
+            XCTAssertNil(error as? URLError, "error = \(error as! URLError)")
         }
         task.resume()