Skip to content

Commit e5867a7

Browse files
committed
NSURLRequest: implement description and add tests
1 parent 3d6e760 commit e5867a7

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

Foundation/NSURLRequest.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,11 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
324324
open internal(set) var httpShouldHandleCookies: Bool = true
325325

326326
open internal(set) var httpShouldUsePipelining: Bool = true
327+
328+
open override var description: String {
329+
let url = self.url?.description ?? "(null)"
330+
return super.description + " { URL: \(url) }"
331+
}
327332
}
328333

329334
/// An `NSMutableURLRequest` object represents a mutable URL load

TestFoundation/TestNSURLRequest.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ class TestNSURLRequest : XCTestCase {
2929
("test_mutableCopy_3", test_mutableCopy_3),
3030
("test_NSCoding_1", test_NSCoding_1),
3131
("test_NSCoding_2", test_NSCoding_2),
32-
("test_NSCoding_3", test_NSCoding_3)
32+
("test_NSCoding_3", test_NSCoding_3),
33+
("test_description", test_description),
3334
]
3435
}
3536

@@ -240,4 +241,18 @@ class TestNSURLRequest : XCTestCase {
240241
XCTAssertEqual(3, requestB.httpBody!.count)
241242
XCTAssertEqual(requestB.httpBody, requestA.httpBody)
242243
}
244+
245+
func test_description() {
246+
let url = URL(string: "http://swift.org")!
247+
let request = NSMutableURLRequest(url: url)
248+
249+
if request.description.range(of: "http://swift.org") == nil {
250+
XCTFail("description should contain URL")
251+
}
252+
253+
request.url = nil
254+
if request.description.range(of: "(null)") == nil {
255+
XCTFail("description of nil URL should contain (null)")
256+
}
257+
}
243258
}

TestFoundation/TestURLRequest.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TestURLRequest : XCTestCase {
2727
("test_mutableCopy_1", test_mutableCopy_1),
2828
("test_mutableCopy_2", test_mutableCopy_2),
2929
("test_mutableCopy_3", test_mutableCopy_3),
30+
("test_description", test_description),
3031
]
3132
}
3233

@@ -194,5 +195,15 @@ class TestURLRequest : XCTestCase {
194195
XCTAssertEqual(originalRequest.url, urlA)
195196
XCTAssertNil(originalRequest.allHTTPHeaderFields)
196197
}
198+
199+
func test_description() {
200+
let url = URL(string: "http://swift.org")!
201+
202+
var request = URLRequest(url: url)
203+
XCTAssertEqual(request.description, "http://swift.org")
204+
205+
request.url = nil
206+
XCTAssertEqual(request.description, "url: nil")
207+
}
197208
}
198209

0 commit comments

Comments
 (0)