Skip to content

Commit c946737

Browse files
authored
Merge pull request swiftlang#1499 from tid-kijyun/fix_nsdata_fails_to_access_contents_of_url
2 parents fa7d92c + 481fffe commit c946737

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Foundation/NSData.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
218218
guard let data = resData else {
219219
throw resError!
220220
}
221-
_init(bytes: UnsafeMutableRawPointer(mutating: data._nsObject.bytes), length: length, copy: true)
221+
_init(bytes: UnsafeMutableRawPointer(mutating: data._nsObject.bytes), length: data.count, copy: true)
222222
}
223223
}
224224

TestFoundation/HTTPServer.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class _HTTPServer {
207207
deadlineTime = .now()
208208
}
209209

210-
DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
210+
DispatchQueue.global().asyncAfter(deadline: deadlineTime) {
211211
do {
212212
try self.socket.writeData(header: response.header, body: response.body, sendDelay: sendDelay, bodyChunks: bodyChunks)
213213
semaphore.signal()

TestFoundation/TestNSData.swift

+12-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import SwiftXCTest
1616
#endif
1717

18-
class TestNSData: XCTestCase {
18+
class TestNSData: LoopbackServerTest {
1919

2020
class AllOnesImmutableData : NSData {
2121
private var _length : Int
@@ -201,6 +201,7 @@ class TestNSData: XCTestCase {
201201
("test_openingNonExistentFile", test_openingNonExistentFile),
202202
("test_contentsOfFile", test_contentsOfFile),
203203
("test_contentsOfZeroFile", test_contentsOfZeroFile),
204+
("test_contentsOfURL", test_contentsOfURL),
204205
("test_basicReadWrite", test_basicReadWrite),
205206
("test_bufferSizeCalculation", test_bufferSizeCalculation),
206207
("test_dataHash", test_dataHash),
@@ -1473,6 +1474,16 @@ extension TestNSData {
14731474
#endif
14741475
}
14751476

1477+
func test_contentsOfURL() {
1478+
let urlString = "http://127.0.0.1:\(TestURLSession.serverPort)/country.txt"
1479+
let url = URL(string: urlString)!
1480+
let contents = NSData(contentsOf: url)
1481+
XCTAssertNotNil(contents)
1482+
if let contents = contents {
1483+
XCTAssertTrue(contents.length > 0)
1484+
}
1485+
}
1486+
14761487
func test_basicReadWrite() {
14771488
let url = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent("testfile")
14781489
let count = 1 << 24

0 commit comments

Comments
 (0)