Skip to content

Commit dfc45a5

Browse files
committed
Add HTTPBody and HTTPBodyStream, remove allowsCellularAccess
1 parent 97969a6 commit dfc45a5

File tree

2 files changed

+91
-13
lines changed

2 files changed

+91
-13
lines changed

Foundation/NSURLRequest.swift

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,10 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
179179
cachePolicy = other.cachePolicy
180180
timeoutInterval = other.timeoutInterval
181181
networkServiceType = other.networkServiceType
182-
allowsCellularAccess = other.allowsCellularAccess
183182
HTTPMethod = other.HTTPMethod
184183
allHTTPHeaderFields = other.allHTTPHeaderFields
184+
HTTPBody = other.HTTPBody
185+
HTTPBodyStream = other.HTTPBodyStream
185186
}
186187

187188
public override func mutableCopy() -> AnyObject {
@@ -255,11 +256,29 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
255256
*/
256257
/*@NSCopying */public private(set) var URL: NSURL?
257258

259+
/// The cache policy of the receiver.
258260
public private(set) var cachePolicy: NSURLRequestCachePolicy = .UseProtocolCachePolicy
261+
262+
/// The timeout interval.
263+
///
264+
/// The timeout interval specifies the limit on the idle
265+
/// interval allotted to a request in the process of loading. The *idle
266+
/// interval* is defined as the period of time that has passed since the
267+
/// last instance of load activity occurred for a request that is in the
268+
/// process of loading. Hence, when an instance of load activity occurs
269+
/// (e.g. bytes are received from the network for a request), the idle
270+
/// interval for a request is reset to 0. If the idle interval ever
271+
/// becomes greater than or equal to the timeout interval, the request
272+
/// is considered to have timed out. This timeout interval is measured
273+
/// in seconds.
259274
public private(set) var timeoutInterval: NSTimeInterval = 60
275+
276+
/// The `NSURLRequestNetworkServiceType` associated with this request.
277+
///
278+
/// This method is used to provide the network layers with a hint as to the
279+
/// purpose of the request. Most clients should not need to use this method.
260280
public private(set) var networkServiceType: NSURLRequestNetworkServiceType = .NetworkServiceTypeDefault
261-
public private(set) var allowsCellularAccess: Bool = true
262-
281+
263282
/*!
264283
@method mainDocumentURL
265284
@abstract The main document URL associated with this load.
@@ -286,6 +305,23 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
286305
*/
287306
public private(set) var allHTTPHeaderFields: [String: String]?
288307

308+
/// HTTP body data.
309+
///
310+
/// This is sent as the message body, as in an HTTP POST request.
311+
public private(set) var HTTPBody: NSData? {
312+
didSet { precondition(HTTPBody == nil || HTTPBodyStream == nil, "Must not set both HTTPBody and HTTPBodyStream.") }
313+
}
314+
315+
/// HTTP body stream.
316+
///
317+
/// - Returns: `nil` if the body stream has not been set. The returned stream is for examination only -- it is not safe to manipulate the stream in any way.
318+
///
319+
/// - Note: A request can have an HTTP body or an HTTP body stream, only one may be set for a request.
320+
/// - Note: A HTTP body stream is preserved when copying an NSURLRequest object, but is lost when a request is archived using the NSCoding protocol.
321+
public private(set) var HTTPBodyStream: NSInputStream? {
322+
didSet { precondition(HTTPBody == nil || HTTPBodyStream == nil, "Must not set both HTTPBody and HTTPBodyStream.") }
323+
}
324+
289325
/*!
290326
@method valueForHTTPHeaderField:
291327
@abstract Returns the value which corresponds to the given header
@@ -332,9 +368,6 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
332368
</ul>
333369
*/
334370
public class NSMutableURLRequest : NSURLRequest {
335-
336-
private var _HTTPMethod: String? = "GET"
337-
338371
public required init?(coder aDecoder: NSCoder) {
339372
super.init()
340373
}
@@ -378,21 +411,55 @@ public class NSMutableURLRequest : NSURLRequest {
378411
set { super.HTTPMethod = newValue }
379412
}
380413

414+
/// The cache policy of the receiver.
381415
public override var cachePolicy: NSURLRequestCachePolicy {
382416
get { return super.cachePolicy }
383417
set { super.cachePolicy = newValue }
384418
}
419+
420+
/// The timeout interval.
421+
///
422+
/// The timeout interval specifies the limit on the idle
423+
/// interval allotted to a request in the process of loading. The *idle
424+
/// interval* is defined as the period of time that has passed since the
425+
/// last instance of load activity occurred for a request that is in the
426+
/// process of loading. Hence, when an instance of load activity occurs
427+
/// (e.g. bytes are received from the network for a request), the idle
428+
/// interval for a request is reset to 0. If the idle interval ever
429+
/// becomes greater than or equal to the timeout interval, the request
430+
/// is considered to have timed out. This timeout interval is measured
431+
/// in seconds.
385432
public override var timeoutInterval: NSTimeInterval {
386433
get { return super.timeoutInterval }
387434
set { super.timeoutInterval = newValue }
388435
}
436+
437+
/// The `NSURLRequestNetworkServiceType` associated with this request.
438+
///
439+
/// This method is used to provide the network layers with a hint as to the
440+
/// purpose of the request. Most clients should not need to use this method.
389441
public override var networkServiceType: NSURLRequestNetworkServiceType {
390442
get { return super.networkServiceType }
391443
set { super.networkServiceType = newValue }
392444
}
393-
public override var allowsCellularAccess: Bool {
394-
get { return super.allowsCellularAccess }
395-
set { super.allowsCellularAccess = newValue }
445+
446+
/// HTTP body data.
447+
///
448+
/// This is sent as the message body, as in an HTTP POST request.
449+
public override var HTTPBody: NSData? {
450+
get { return super.HTTPBody }
451+
set { super.HTTPBody = newValue.map({ $0.copy() as! NSData }) }
452+
}
453+
454+
/// HTTP body stream.
455+
///
456+
/// - Returns: `nil` if the body stream has not been set. The returned stream is for examination only -- it is not safe to manipulate the stream in any way.
457+
///
458+
/// - Note: A request can have an HTTP body or an HTTP body stream, only one may be set for a request.
459+
/// - Note: A HTTP body stream is preserved when copying an NSURLRequest object, but is lost when a request is archived using the NSCoding protocol.
460+
public override var HTTPBodyStream: NSInputStream? {
461+
get { return super.HTTPBodyStream }
462+
set { super.HTTPBodyStream = newValue }
396463
}
397464

398465
/*!

TestFoundation/TestNSURLRequest.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ class TestNSURLRequest : XCTestCase {
3737
XCTAssertEqual(request.cachePolicy, NSURLRequestCachePolicy.UseProtocolCachePolicy)
3838
XCTAssertEqual(request.timeoutInterval, 60)
3939
XCTAssertEqual(request.networkServiceType, NSURLRequestNetworkServiceType.NetworkServiceTypeDefault)
40-
XCTAssertEqual(request.allowsCellularAccess, true)
4140
XCTAssertNil(request.allHTTPHeaderFields)
4241
XCTAssertNil(request.mainDocumentURL)
42+
XCTAssertNil(request.HTTPBody)
43+
XCTAssertNil(request.HTTPBodyStream)
4344
}
4445

4546
func test_mutableConstruction() {
@@ -53,9 +54,10 @@ class TestNSURLRequest : XCTestCase {
5354
XCTAssertEqual(request.cachePolicy, NSURLRequestCachePolicy.UseProtocolCachePolicy)
5455
XCTAssertEqual(request.timeoutInterval, 60)
5556
XCTAssertEqual(request.networkServiceType, NSURLRequestNetworkServiceType.NetworkServiceTypeDefault)
56-
XCTAssertEqual(request.allowsCellularAccess, true)
5757
XCTAssertNil(request.allHTTPHeaderFields)
5858
XCTAssertNil(request.mainDocumentURL)
59+
XCTAssertNil(request.HTTPBody)
60+
XCTAssertNil(request.HTTPBodyStream)
5961

6062
request.mainDocumentURL = URL
6163
XCTAssertEqual(request.mainDocumentURL, URL)
@@ -75,14 +77,23 @@ class TestNSURLRequest : XCTestCase {
7577
request.networkServiceType = newServiceType
7678
XCTAssertEqual(request.networkServiceType, newServiceType)
7779

78-
request.allowsCellularAccess = false
79-
XCTAssertEqual(request.allowsCellularAccess, false)
80+
let newData = NSMutableData()
81+
newData.appendData("A".dataUsingEncoding(NSUTF8StringEncoding)!)
82+
request.HTTPBody = newData
83+
newData.appendData("B".dataUsingEncoding(NSUTF8StringEncoding)!)
84+
if let d = request.HTTPBody {
85+
XCTAssertEqual(String(data: d, encoding: NSUTF8StringEncoding), "A")
86+
} else {
87+
XCTFail()
88+
}
8089

8190
let newURL = NSURL(string: "http://github.com")!
8291
request.URL = newURL
8392
XCTAssertEqual(request.URL, newURL)
8493
}
8594

95+
//TODO: Test HTTPBodyStream when NSInputStream is implemented.
96+
8697
func test_headerFields() {
8798
let request = NSMutableURLRequest(URL: URL)
8899

0 commit comments

Comments
 (0)