Skip to content

NSMutableURLRequest implementation #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 56 additions & 8 deletions Foundation/NSURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
@abstract Returns the HTTP request method of the receiver.
@result the HTTP request method of the receiver.
*/
public var HTTPMethod: String? { get { return "GET" }}
public var HTTPMethod: String? { get { return "GET" } }

/*!
@method allHTTPHeaderFields
Expand Down Expand Up @@ -308,15 +308,27 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
*/
public class NSMutableURLRequest : NSURLRequest {

private var _HTTPMethod: String? = "GET"

public required init?(coder aDecoder: NSCoder) {
NSUnimplemented()
super.init()
}
/*!

private override init() { super.init() }

/*!
@method URL
@abstract Sets the URL of the receiver.
@param URL The new URL for the receiver.
*/
/*@NSCopying */ public override var URL: NSURL? { get { NSUnimplemented() } set { NSUnimplemented() } }
/*@NSCopying */ public override var URL: NSURL? {
get {
return _URL
}
set(newURL) {
_URL = newURL
}
}

/*!
@method setMainDocumentURL:
Expand All @@ -329,14 +341,27 @@ public class NSMutableURLRequest : NSURLRequest {
"only from same domain as main document" policy, and possibly
other things in the future.
*/
/*@NSCopying*/ public override var mainDocumentURL: NSURL? { get { NSUnimplemented() } set { NSUnimplemented() } }
/*@NSCopying*/ public override var mainDocumentURL: NSURL? {
get {
return _mainDocumentURL
} set(newMainDocumentURL) {
_mainDocumentURL = newMainDocumentURL
}
}


/*!
@method HTTPMethod
@abstract Sets the HTTP request method of the receiver.
@result the HTTP request method of the receiver.
*/
public override var HTTPMethod: String? { get { NSUnimplemented() } set { NSUnimplemented() } }
public override var HTTPMethod: String? {
get {
return _HTTPMethod
} set(newHTTPMethod) {
_HTTPMethod = newHTTPMethod
}
}

/*!
@method setValue:forHTTPHeaderField:
Expand All @@ -348,7 +373,18 @@ public class NSMutableURLRequest : NSURLRequest {
@param value the header field value.
@param field the header field name (case-insensitive).
*/
public func setValue(value: String?, forHTTPHeaderField field: String) { NSUnimplemented() }
public func setValue(value: String?, forHTTPHeaderField field: String) {
if _httpHeaderFields == nil {
_httpHeaderFields = [:]
}
if let existingHeader = _httpHeaderFields?.filter({ (existingField, _) -> Bool in
return existingField.lowercaseString == field.lowercaseString
}).first {
let (existingField, _) = existingHeader
_httpHeaderFields?.removeValueForKey(existingField)
}
_httpHeaderFields?[field] = value
}

/*!
@method addValue:forHTTPHeaderField:
Expand All @@ -364,7 +400,19 @@ public class NSMutableURLRequest : NSURLRequest {
@param value the header field value.
@param field the header field name (case-insensitive).
*/
public func addValue(value: String, forHTTPHeaderField field: String) { NSUnimplemented() }
public func addValue(value: String, forHTTPHeaderField field: String) {
if _httpHeaderFields == nil {
_httpHeaderFields = [:]
}
if let existingHeader = _httpHeaderFields?.filter({ (existingField, _) -> Bool in
return existingField.lowercaseString == field.lowercaseString
}).first {
let (existingField, existingValue) = existingHeader
_httpHeaderFields?[existingField] = "\(existingValue),\(value)"
} else {
_httpHeaderFields?[field] = value
}
}
}


44 changes: 43 additions & 1 deletion TestFoundation/TestNSURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ class TestNSURLRequest : XCTestCase {
var allTests : [(String, () -> ())] {
return [
("test_construction", test_construction),
("test_mutableConstruction", test_mutableConstruction),
("test_headerFields", test_headerFields)
]
}

let URL = NSURL(string: "http://swift.org")!

func test_construction() {
let URL = NSURL(string: "http://swift.org")!
let request = NSURLRequest(URL: URL)
// Match OS X Foundation responses
XCTAssertNotNil(request)
Expand All @@ -34,4 +37,43 @@ class TestNSURLRequest : XCTestCase {
XCTAssertNil(request.allHTTPHeaderFields)
XCTAssertNil(request.mainDocumentURL)
}

func test_mutableConstruction() {
let URL = NSURL(string: "http://swift.org")!
let request = NSMutableURLRequest(URL: URL)

//Confirm initial state matches NSURLRequest responses
XCTAssertNotNil(request)
XCTAssertEqual(request.URL, URL)
XCTAssertEqual(request.HTTPMethod, "GET")
XCTAssertNil(request.allHTTPHeaderFields)
XCTAssertNil(request.mainDocumentURL)

request.mainDocumentURL = URL
XCTAssertEqual(request.mainDocumentURL, URL)

request.HTTPMethod = "POST"
XCTAssertEqual(request.HTTPMethod, "POST")

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

func test_headerFields() {
let request = NSMutableURLRequest(URL: URL)

request.setValue("application/json", forHTTPHeaderField: "Accept")
XCTAssertNotNil(request.allHTTPHeaderFields)
XCTAssertEqual(request.allHTTPHeaderFields?["Accept"], "application/json")

// Setting "accept" should remove "Accept"
request.setValue("application/xml", forHTTPHeaderField: "accept")
XCTAssertNil(request.allHTTPHeaderFields?["Accept"])
XCTAssertEqual(request.allHTTPHeaderFields?["accept"], "application/xml")

// Adding to "Accept" should add to "accept"
request.addValue("text/html", forHTTPHeaderField: "Accept")
XCTAssertEqual(request.allHTTPHeaderFields?["accept"], "application/xml,text/html")
}
}