Skip to content

Commit c8c19ec

Browse files
committed
Merge pull request #104 from ChrisChares/NSMutableURLRequest
NSMutableURLRequest implementation
2 parents f4bf7a4 + e1fd867 commit c8c19ec

File tree

2 files changed

+99
-9
lines changed

2 files changed

+99
-9
lines changed

Foundation/NSURLRequest.swift

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
252252
@abstract Returns the HTTP request method of the receiver.
253253
@result the HTTP request method of the receiver.
254254
*/
255-
public var HTTPMethod: String? { get { return "GET" }}
255+
public var HTTPMethod: String? { get { return "GET" } }
256256

257257
/*!
258258
@method allHTTPHeaderFields
@@ -308,15 +308,27 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
308308
*/
309309
public class NSMutableURLRequest : NSURLRequest {
310310

311+
private var _HTTPMethod: String? = "GET"
312+
311313
public required init?(coder aDecoder: NSCoder) {
312-
NSUnimplemented()
314+
super.init()
313315
}
314-
/*!
316+
317+
private override init() { super.init() }
318+
319+
/*!
315320
@method URL
316321
@abstract Sets the URL of the receiver.
317322
@param URL The new URL for the receiver.
318323
*/
319-
/*@NSCopying */ public override var URL: NSURL? { get { NSUnimplemented() } set { NSUnimplemented() } }
324+
/*@NSCopying */ public override var URL: NSURL? {
325+
get {
326+
return _URL
327+
}
328+
set(newURL) {
329+
_URL = newURL
330+
}
331+
}
320332

321333
/*!
322334
@method setMainDocumentURL:
@@ -329,14 +341,27 @@ public class NSMutableURLRequest : NSURLRequest {
329341
"only from same domain as main document" policy, and possibly
330342
other things in the future.
331343
*/
332-
/*@NSCopying*/ public override var mainDocumentURL: NSURL? { get { NSUnimplemented() } set { NSUnimplemented() } }
344+
/*@NSCopying*/ public override var mainDocumentURL: NSURL? {
345+
get {
346+
return _mainDocumentURL
347+
} set(newMainDocumentURL) {
348+
_mainDocumentURL = newMainDocumentURL
349+
}
350+
}
351+
333352

334353
/*!
335354
@method HTTPMethod
336355
@abstract Sets the HTTP request method of the receiver.
337356
@result the HTTP request method of the receiver.
338357
*/
339-
public override var HTTPMethod: String? { get { NSUnimplemented() } set { NSUnimplemented() } }
358+
public override var HTTPMethod: String? {
359+
get {
360+
return _HTTPMethod
361+
} set(newHTTPMethod) {
362+
_HTTPMethod = newHTTPMethod
363+
}
364+
}
340365

341366
/*!
342367
@method setValue:forHTTPHeaderField:
@@ -348,7 +373,18 @@ public class NSMutableURLRequest : NSURLRequest {
348373
@param value the header field value.
349374
@param field the header field name (case-insensitive).
350375
*/
351-
public func setValue(value: String?, forHTTPHeaderField field: String) { NSUnimplemented() }
376+
public func setValue(value: String?, forHTTPHeaderField field: String) {
377+
if _httpHeaderFields == nil {
378+
_httpHeaderFields = [:]
379+
}
380+
if let existingHeader = _httpHeaderFields?.filter({ (existingField, _) -> Bool in
381+
return existingField.lowercaseString == field.lowercaseString
382+
}).first {
383+
let (existingField, _) = existingHeader
384+
_httpHeaderFields?.removeValueForKey(existingField)
385+
}
386+
_httpHeaderFields?[field] = value
387+
}
352388

353389
/*!
354390
@method addValue:forHTTPHeaderField:
@@ -364,7 +400,19 @@ public class NSMutableURLRequest : NSURLRequest {
364400
@param value the header field value.
365401
@param field the header field name (case-insensitive).
366402
*/
367-
public func addValue(value: String, forHTTPHeaderField field: String) { NSUnimplemented() }
403+
public func addValue(value: String, forHTTPHeaderField field: String) {
404+
if _httpHeaderFields == nil {
405+
_httpHeaderFields = [:]
406+
}
407+
if let existingHeader = _httpHeaderFields?.filter({ (existingField, _) -> Bool in
408+
return existingField.lowercaseString == field.lowercaseString
409+
}).first {
410+
let (existingField, existingValue) = existingHeader
411+
_httpHeaderFields?[existingField] = "\(existingValue),\(value)"
412+
} else {
413+
_httpHeaderFields?[field] = value
414+
}
415+
}
368416
}
369417

370418

TestFoundation/TestNSURLRequest.swift

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ class TestNSURLRequest : XCTestCase {
2121
var allTests : [(String, () -> ())] {
2222
return [
2323
("test_construction", test_construction),
24+
("test_mutableConstruction", test_mutableConstruction),
25+
("test_headerFields", test_headerFields)
2426
]
2527
}
2628

29+
let URL = NSURL(string: "http://swift.org")!
30+
2731
func test_construction() {
28-
let URL = NSURL(string: "http://swift.org")!
2932
let request = NSURLRequest(URL: URL)
3033
// Match OS X Foundation responses
3134
XCTAssertNotNil(request)
@@ -34,4 +37,43 @@ class TestNSURLRequest : XCTestCase {
3437
XCTAssertNil(request.allHTTPHeaderFields)
3538
XCTAssertNil(request.mainDocumentURL)
3639
}
40+
41+
func test_mutableConstruction() {
42+
let URL = NSURL(string: "http://swift.org")!
43+
let request = NSMutableURLRequest(URL: URL)
44+
45+
//Confirm initial state matches NSURLRequest responses
46+
XCTAssertNotNil(request)
47+
XCTAssertEqual(request.URL, URL)
48+
XCTAssertEqual(request.HTTPMethod, "GET")
49+
XCTAssertNil(request.allHTTPHeaderFields)
50+
XCTAssertNil(request.mainDocumentURL)
51+
52+
request.mainDocumentURL = URL
53+
XCTAssertEqual(request.mainDocumentURL, URL)
54+
55+
request.HTTPMethod = "POST"
56+
XCTAssertEqual(request.HTTPMethod, "POST")
57+
58+
let newURL = NSURL(string: "http://github.com")!
59+
request.URL = newURL
60+
XCTAssertEqual(request.URL, newURL)
61+
}
62+
63+
func test_headerFields() {
64+
let request = NSMutableURLRequest(URL: URL)
65+
66+
request.setValue("application/json", forHTTPHeaderField: "Accept")
67+
XCTAssertNotNil(request.allHTTPHeaderFields)
68+
XCTAssertEqual(request.allHTTPHeaderFields?["Accept"], "application/json")
69+
70+
// Setting "accept" should remove "Accept"
71+
request.setValue("application/xml", forHTTPHeaderField: "accept")
72+
XCTAssertNil(request.allHTTPHeaderFields?["Accept"])
73+
XCTAssertEqual(request.allHTTPHeaderFields?["accept"], "application/xml")
74+
75+
// Adding to "Accept" should add to "accept"
76+
request.addValue("text/html", forHTTPHeaderField: "Accept")
77+
XCTAssertEqual(request.allHTTPHeaderFields?["accept"], "application/xml,text/html")
78+
}
3779
}

0 commit comments

Comments
 (0)