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 2 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
65 changes: 58 additions & 7 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,39 @@ public class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopyin
*/
public class NSMutableURLRequest : NSURLRequest {

private var _HTTPMethod: String? = "GET"

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

/*!
@method initWithURL:
@abstract Initializes an NSMutableURLRequest with the given URL.
@discussion Default values are used for cache policy
(NSURLRequestUseProtocolCachePolicy) and timeout interval (60
seconds).
@param URL The URL for the request.
@result An initialized NSMutableURLRequest.
*/
public init(URL: NSURL) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the superclass handle this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL: yea interesting - it does not seem to have the funnel behavior i would have guessed either. In short this seems fine to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey sorry I think I deleted that comment too quick, didn't know you'd get to it that fast. NSMutableURLRequest wasn't overriding NSURLRequest's private blank initializer, so the convenience initializers weren't being passed down

super.init()
_URL = URL
}

/*!
@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 +353,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 +385,12 @@ 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 = [:]
}
_httpHeaderFields?[field.lowercaseString] = value

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Obj-C Foundation framework preserves the capitalization as entered (as seen in what is returned from allHTTPHeaderFields), though is otherwise indeed case insensitive. Keeping the capitalization as is helps with readability during debugging network calls.

}

/*!
@method addValue:forHTTPHeaderField:
Expand All @@ -364,7 +406,16 @@ 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 oldValue = _httpHeaderFields?[field.lowercaseString] {
_httpHeaderFields?[field.lowercaseString] = "\(oldValue),\(value)"
} else {
_httpHeaderFields?[field.lowercaseString] = value
}
}
}


36 changes: 35 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,35 @@ 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")
//HTTP Header fields should be case insensitive
request.addValue("text/html", forHTTPHeaderField: "Accept")
XCTAssertEqual(request.allHTTPHeaderFields?["accept"], "application/json,text/html")
}
}