Skip to content

Commit 259302b

Browse files
committed
Add 4 missing attributes to NSURLRequest
cachePolicy, timeoutInterval, networkServiceType, and allowsCellularAccess Rework how attributes are stored. Refactor header field code into existingHeaderField().
1 parent 14ed881 commit 259302b

File tree

2 files changed

+80
-47
lines changed

2 files changed

+80
-47
lines changed

Foundation/NSURLRequest.swift

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,6 @@ public enum NSURLRequestNetworkServiceType : UInt {
164164
*/
165165
public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying {
166166

167-
private var _URL : NSURL?
168-
private var _mainDocumentURL: NSURL?
169-
private var _httpHeaderFields: [String: String]?
170-
171167
public override func copy() -> AnyObject {
172168
return copyWithZone(nil)
173169
}
@@ -235,16 +231,21 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
235231
*/
236232
public convenience init(URL: NSURL) {
237233
self.init()
238-
_URL = URL
234+
self.URL = URL
239235
}
240236

241237
/*!
242238
@method URL
243239
@abstract Returns the URL of the receiver.
244240
@result The URL of the receiver.
245241
*/
246-
/*@NSCopying */public var URL: NSURL? { return _URL }
242+
/*@NSCopying */public private(set) var URL: NSURL?
247243

244+
public private(set) var cachePolicy: NSURLRequestCachePolicy = .UseProtocolCachePolicy
245+
public private(set) var timeoutInterval: NSTimeInterval = 60
246+
public private(set) var networkServiceType: NSURLRequestNetworkServiceType = .NetworkServiceTypeDefault
247+
public private(set) var allowsCellularAccess: Bool = true
248+
248249
/*!
249250
@method mainDocumentURL
250251
@abstract The main document URL associated with this load.
@@ -253,14 +254,14 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
253254
See setMainDocumentURL:
254255
@result The main document URL.
255256
*/
256-
/*@NSCopying*/ public var mainDocumentURL: NSURL? { return _mainDocumentURL }
257+
/*@NSCopying*/ public private(set) var mainDocumentURL: NSURL?
257258

258259
/*!
259260
@method HTTPMethod
260261
@abstract Returns the HTTP request method of the receiver.
261262
@result the HTTP request method of the receiver.
262263
*/
263-
public var HTTPMethod: String? { return "GET" }
264+
public private(set) var HTTPMethod: String? = "GET"
264265

265266
/*!
266267
@method allHTTPHeaderFields
@@ -269,7 +270,7 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
269270
@result a dictionary containing all the HTTP header fields of the
270271
receiver.
271272
*/
272-
public var allHTTPHeaderFields: [String : String]? { return _httpHeaderFields }
273+
public private(set) var allHTTPHeaderFields: [String: String]?
273274

274275
/*!
275276
@method valueForHTTPHeaderField:
@@ -281,8 +282,10 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
281282
@result the value associated with the given header field, or nil if
282283
there is no value associated with the given header field.
283284
*/
284-
public func valueForHTTPHeaderField(field: String) -> String? { return _httpHeaderFields?[field.lowercased()] }
285-
285+
public func valueForHTTPHeaderField(field: String) -> String? {
286+
guard let f = allHTTPHeaderFields else { return nil }
287+
return existingHeaderField(field, inHeaderFields: f)?.1
288+
}
286289
}
287290

288291
/*!
@@ -330,14 +333,10 @@ public class NSMutableURLRequest : NSURLRequest {
330333
@param URL The new URL for the receiver.
331334
*/
332335
/*@NSCopying */ public override var URL: NSURL? {
333-
get {
334-
return _URL
335-
}
336-
set(newURL) {
337-
_URL = newURL
338-
}
336+
get { return super.URL }
337+
set { super.URL = newValue }
339338
}
340-
339+
341340
/*!
342341
@method setMainDocumentURL:
343342
@abstract Sets the main document URL
@@ -350,11 +349,8 @@ public class NSMutableURLRequest : NSURLRequest {
350349
other things in the future.
351350
*/
352351
/*@NSCopying*/ public override var mainDocumentURL: NSURL? {
353-
get {
354-
return _mainDocumentURL
355-
} set(newMainDocumentURL) {
356-
_mainDocumentURL = newMainDocumentURL
357-
}
352+
get { return super.mainDocumentURL }
353+
set { super.mainDocumentURL = newValue }
358354
}
359355

360356

@@ -364,11 +360,25 @@ public class NSMutableURLRequest : NSURLRequest {
364360
@result the HTTP request method of the receiver.
365361
*/
366362
public override var HTTPMethod: String? {
367-
get {
368-
return _HTTPMethod
369-
} set(newHTTPMethod) {
370-
_HTTPMethod = newHTTPMethod
371-
}
363+
get { return super.HTTPMethod }
364+
set { super.HTTPMethod = newValue }
365+
}
366+
367+
public override var cachePolicy: NSURLRequestCachePolicy {
368+
get { return super.cachePolicy }
369+
set { super.cachePolicy = newValue }
370+
}
371+
public override var timeoutInterval: NSTimeInterval {
372+
get { return super.timeoutInterval }
373+
set { super.timeoutInterval = newValue }
374+
}
375+
public override var networkServiceType: NSURLRequestNetworkServiceType {
376+
get { return super.networkServiceType }
377+
set { super.networkServiceType = newValue }
378+
}
379+
public override var allowsCellularAccess: Bool {
380+
get { return super.allowsCellularAccess }
381+
set { super.allowsCellularAccess = newValue }
372382
}
373383

374384
/*!
@@ -382,16 +392,12 @@ public class NSMutableURLRequest : NSURLRequest {
382392
@param field the header field name (case-insensitive).
383393
*/
384394
public func setValue(value: String?, forHTTPHeaderField field: String) {
385-
if _httpHeaderFields == nil {
386-
_httpHeaderFields = [:]
387-
}
388-
if let existingHeader = _httpHeaderFields?.filter({ (existingField, _) -> Bool in
389-
return existingField.lowercased() == field.lowercased()
390-
}).first {
391-
let (existingField, _) = existingHeader
392-
_httpHeaderFields?.removeValue(forKey: existingField)
395+
var f: [String: String] = allHTTPHeaderFields ?? [:]
396+
if let old = existingHeaderField(field, inHeaderFields: f) {
397+
f.removeValue(forKey: old.0)
393398
}
394-
_httpHeaderFields?[field] = value
399+
f[field] = value
400+
allHTTPHeaderFields = f
395401
}
396402

397403
/*!
@@ -409,18 +415,22 @@ public class NSMutableURLRequest : NSURLRequest {
409415
@param field the header field name (case-insensitive).
410416
*/
411417
public func addValue(value: String, forHTTPHeaderField field: String) {
412-
if _httpHeaderFields == nil {
413-
_httpHeaderFields = [:]
414-
}
415-
if let existingHeader = _httpHeaderFields?.filter({ (existingField, _) -> Bool in
416-
return existingField.lowercased() == field.lowercased()
417-
}).first {
418-
let (existingField, existingValue) = existingHeader
419-
_httpHeaderFields?[existingField] = "\(existingValue),\(value)"
418+
var f: [String: String] = allHTTPHeaderFields ?? [:]
419+
if let old = existingHeaderField(field, inHeaderFields: f) {
420+
f[old.0] = old.1 + "," + value
420421
} else {
421-
_httpHeaderFields?[field] = value
422+
f[field] = value
422423
}
424+
allHTTPHeaderFields = f
423425
}
424426
}
425427

426-
428+
/// Returns an existing key-value pair inside the header fields if it exists.
429+
private func existingHeaderField(key: String, inHeaderFields fields: [String: String]) -> (String, String)? {
430+
for (k, v) in fields {
431+
if k.lowercased() == key.lowercased() {
432+
return (k, v)
433+
}
434+
}
435+
return nil
436+
}

TestFoundation/TestNSURLRequest.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class TestNSURLRequest : XCTestCase {
3434
XCTAssertNotNil(request)
3535
XCTAssertEqual(request.URL, URL)
3636
XCTAssertEqual(request.HTTPMethod, "GET")
37+
XCTAssertEqual(request.cachePolicy, NSURLRequestCachePolicy.UseProtocolCachePolicy)
38+
XCTAssertEqual(request.timeoutInterval, 60)
39+
XCTAssertEqual(request.networkServiceType, NSURLRequestNetworkServiceType.NetworkServiceTypeDefault)
40+
XCTAssertEqual(request.allowsCellularAccess, true)
3741
XCTAssertNil(request.allHTTPHeaderFields)
3842
XCTAssertNil(request.mainDocumentURL)
3943
}
@@ -46,6 +50,10 @@ class TestNSURLRequest : XCTestCase {
4650
XCTAssertNotNil(request)
4751
XCTAssertEqual(request.URL, URL)
4852
XCTAssertEqual(request.HTTPMethod, "GET")
53+
XCTAssertEqual(request.cachePolicy, NSURLRequestCachePolicy.UseProtocolCachePolicy)
54+
XCTAssertEqual(request.timeoutInterval, 60)
55+
XCTAssertEqual(request.networkServiceType, NSURLRequestNetworkServiceType.NetworkServiceTypeDefault)
56+
XCTAssertEqual(request.allowsCellularAccess, true)
4957
XCTAssertNil(request.allHTTPHeaderFields)
5058
XCTAssertNil(request.mainDocumentURL)
5159

@@ -55,6 +63,21 @@ class TestNSURLRequest : XCTestCase {
5563
request.HTTPMethod = "POST"
5664
XCTAssertEqual(request.HTTPMethod, "POST")
5765

66+
let newPolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
67+
request.cachePolicy = newPolicy
68+
XCTAssertEqual(request.cachePolicy, newPolicy)
69+
70+
let newTimeout: NSTimeInterval = 42
71+
request.timeoutInterval = newTimeout
72+
XCTAssertEqual(request.timeoutInterval, newTimeout)
73+
74+
let newServiceType = NSURLRequestNetworkServiceType.NetworkServiceTypeVoice
75+
request.networkServiceType = newServiceType
76+
XCTAssertEqual(request.networkServiceType, newServiceType)
77+
78+
request.allowsCellularAccess = false
79+
XCTAssertEqual(request.allowsCellularAccess, false)
80+
5881
let newURL = NSURL(string: "http://github.com")!
5982
request.URL = newURL
6083
XCTAssertEqual(request.URL, newURL)

0 commit comments

Comments
 (0)