Skip to content

Commit e75cae8

Browse files
committed
Add HTTPBody and HTTPBodyStream, remove allowsCellularAccess
HTTPBody and HTTPBodyStream were missing, but exist in ObjC Foundation. @parkera wanted allowsCellularAccess removed.
1 parent 133782e commit e75cae8

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

Foundation/NSURLRequest.swift

Lines changed: 74 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,29 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
241241
*/
242242
/*@NSCopying */public private(set) var URL: NSURL?
243243

244+
/// The cache policy of the receiver.
244245
public private(set) var cachePolicy: NSURLRequestCachePolicy = .UseProtocolCachePolicy
246+
247+
/// The timeout interval.
248+
///
249+
/// The timeout interval specifies the limit on the idle
250+
/// interval allotted to a request in the process of loading. The *idle
251+
/// interval* is defined as the period of time that has passed since the
252+
/// last instance of load activity occurred for a request that is in the
253+
/// process of loading. Hence, when an instance of load activity occurs
254+
/// (e.g. bytes are received from the network for a request), the idle
255+
/// interval for a request is reset to 0. If the idle interval ever
256+
/// becomes greater than or equal to the timeout interval, the request
257+
/// is considered to have timed out. This timeout interval is measured
258+
/// in seconds.
245259
public private(set) var timeoutInterval: NSTimeInterval = 60
260+
261+
/// The `NSURLRequestNetworkServiceType` associated with this request.
262+
///
263+
/// This method is used to provide the network layers with a hint as to the
264+
/// purpose of the request. Most clients should not need to use this method.
246265
public private(set) var networkServiceType: NSURLRequestNetworkServiceType = .NetworkServiceTypeDefault
247-
public private(set) var allowsCellularAccess: Bool = true
248-
266+
249267
/*!
250268
@method mainDocumentURL
251269
@abstract The main document URL associated with this load.
@@ -272,6 +290,23 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
272290
*/
273291
public private(set) var allHTTPHeaderFields: [String: String]?
274292

293+
/// HTTP body data.
294+
///
295+
/// This is sent as the message body, as in an HTTP POST request.
296+
public private(set) var HTTPBody: NSData? {
297+
didSet { precondition(HTTPBody == nil || HTTPBodyStream == nil, "Must not set both HTTPBody and HTTPBodyStream.") }
298+
}
299+
300+
/// HTTP body stream.
301+
///
302+
/// - 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.
303+
///
304+
/// - Note: A request can have an HTTP body or an HTTP body stream, only one may be set for a request.
305+
/// - Note: A HTTP body stream is preserved when copying an NSURLRequest object, but is lost when a request is archived using the NSCoding protocol.
306+
public private(set) var HTTPBodyStream: NSInputStream? {
307+
didSet { precondition(HTTPBody == nil || HTTPBodyStream == nil, "Must not set both HTTPBody and HTTPBodyStream.") }
308+
}
309+
275310
/*!
276311
@method valueForHTTPHeaderField:
277312
@abstract Returns the value which corresponds to the given header
@@ -318,9 +353,6 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
318353
</ul>
319354
*/
320355
public class NSMutableURLRequest : NSURLRequest {
321-
322-
private var _HTTPMethod: String? = "GET"
323-
324356
public required init?(coder aDecoder: NSCoder) {
325357
super.init()
326358
}
@@ -364,21 +396,55 @@ public class NSMutableURLRequest : NSURLRequest {
364396
set { super.HTTPMethod = newValue }
365397
}
366398

399+
/// The cache policy of the receiver.
367400
public override var cachePolicy: NSURLRequestCachePolicy {
368401
get { return super.cachePolicy }
369402
set { super.cachePolicy = newValue }
370403
}
404+
405+
/// The timeout interval.
406+
///
407+
/// The timeout interval specifies the limit on the idle
408+
/// interval allotted to a request in the process of loading. The *idle
409+
/// interval* is defined as the period of time that has passed since the
410+
/// last instance of load activity occurred for a request that is in the
411+
/// process of loading. Hence, when an instance of load activity occurs
412+
/// (e.g. bytes are received from the network for a request), the idle
413+
/// interval for a request is reset to 0. If the idle interval ever
414+
/// becomes greater than or equal to the timeout interval, the request
415+
/// is considered to have timed out. This timeout interval is measured
416+
/// in seconds.
371417
public override var timeoutInterval: NSTimeInterval {
372418
get { return super.timeoutInterval }
373419
set { super.timeoutInterval = newValue }
374420
}
421+
422+
/// The `NSURLRequestNetworkServiceType` associated with this request.
423+
///
424+
/// This method is used to provide the network layers with a hint as to the
425+
/// purpose of the request. Most clients should not need to use this method.
375426
public override var networkServiceType: NSURLRequestNetworkServiceType {
376427
get { return super.networkServiceType }
377428
set { super.networkServiceType = newValue }
378429
}
379-
public override var allowsCellularAccess: Bool {
380-
get { return super.allowsCellularAccess }
381-
set { super.allowsCellularAccess = newValue }
430+
431+
/// HTTP body data.
432+
///
433+
/// This is sent as the message body, as in an HTTP POST request.
434+
public override var HTTPBody: NSData? {
435+
get { return super.HTTPBody }
436+
set { super.HTTPBody = newValue.map({ $0.copy() as! NSData }) }
437+
}
438+
439+
/// HTTP body stream.
440+
///
441+
/// - 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.
442+
///
443+
/// - Note: A request can have an HTTP body or an HTTP body stream, only one may be set for a request.
444+
/// - Note: A HTTP body stream is preserved when copying an NSURLRequest object, but is lost when a request is archived using the NSCoding protocol.
445+
public override var HTTPBodyStream: NSInputStream? {
446+
get { return super.HTTPBodyStream }
447+
set { super.HTTPBodyStream = newValue }
382448
}
383449

384450
/*!

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)