Skip to content

basic implementation for NSURLResponse init and tests #71

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
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
4 changes: 4 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
DCDBB8331C1768AC00313299 /* TestNSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCDBB8321C1768AC00313299 /* TestNSData.swift */; };
83712C8E1C1684900049AD49 /* TestNSURLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */; };
C2A9D75C1C15C08B00993803 /* TestNSUUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */; };
7A7D6FBB1C16439400957E2E /* TestNSURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */; };
E876A73E1C1180E000F279EC /* TestNSRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = E876A73D1C1180E000F279EC /* TestNSRange.swift */; };
848A30581C137B3500C83206 /* TestNSHTTPCookie.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */; };
EA66F6361BEED03E00136161 /* TargetConditionals.h in Headers */ = {isa = PBXBuildFile; fileRef = EA66F6351BEED03E00136161 /* TargetConditionals.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -534,6 +535,7 @@
DCDBB8321C1768AC00313299 /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLRequest.swift; sourceTree = "<group>"; };
C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSUUID.swift; sourceTree = "<group>"; };
7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLResponse.swift; sourceTree = "<group>"; };
E876A73D1C1180E000F279EC /* TestNSRange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSRange.swift; sourceTree = "<group>"; };
848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestNSHTTPCookie.swift; path = TestFoundation/TestNSHTTPCookie.swift; sourceTree = SOURCE_ROOT; };
EA313DFC1BE7F2E90060A403 /* CFURLComponents_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CFURLComponents_Internal.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1049,6 +1051,7 @@
84BA558D1C16F90900F48C54 /* TestNSTimeZone.swift */,
844DC3321C17584F005611F9 /* TestNSScanner.swift */,
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */,
7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */,
);
name = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1752,6 +1755,7 @@
EA66F64A1BF1619600136161 /* TestNSArray.swift in Sources */,
5BC1D8BE1BF3B09E009D3973 /* TestNSCharacterSet.swift in Sources */,
EA66F6561BF1619600136161 /* TestNSString.swift in Sources */,
7A7D6FBB1C16439400957E2E /* TestNSURLResponse.swift in Sources */,
5B40F9F21C125187000E72E3 /* TestNSXMLParser.swift in Sources */,
88D28DE71C13AE9000494606 /* TestNSGeometry.swift in Sources */,
EA66F64C1BF1619600136161 /* TestNSDictionary.swift in Sources */,
Expand Down
18 changes: 12 additions & 6 deletions Foundation/NSURLResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
about receiving the content data for a URL load.
*/
public class NSURLResponse : NSObject, NSSecureCoding, NSCopying {

static public func supportsSecureCoding() -> Bool {
return true
}
Expand All @@ -48,14 +48,20 @@ public class NSURLResponse : NSObject, NSSecureCoding, NSCopying {
@result The initialized NSURLResponse.
@discussion This is the designated initializer for NSURLResponse.
*/
public init(URL: NSURL, MIMEType: String?, expectedContentLength length: Int, textEncodingName name: String?) { NSUnimplemented() }
public init(URL: NSURL, MIMEType: String?, expectedContentLength length: Int, textEncodingName name: String?) {
self.URL = URL
self.MIMEType = MIMEType
self.expectedContentLength = Int64(length)
self.textEncodingName = name
}

/*!
@method URL
@abstract Returns the URL of the receiver.
@result The URL of the receiver.
*/
/*@NSCopying*/ public var URL: NSURL? { NSUnimplemented() }
/*@NSCopying*/ public private(set) var URL: NSURL?


/*!
@method MIMEType
Expand All @@ -68,7 +74,7 @@ public class NSURLResponse : NSObject, NSSecureCoding, NSCopying {
be made if the origin source did not report any such information.
@result The MIME type of the receiver.
*/
public var MIMEType: String? { NSUnimplemented() }
public private(set) var MIMEType: String?

/*!
@method expectedContentLength
Expand All @@ -83,7 +89,7 @@ public class NSURLResponse : NSObject, NSSecureCoding, NSCopying {
there is no expectation that can be arrived at regarding expected
content length.
*/
public var expectedContentLength: Int64 { NSUnimplemented() }
public private(set) var expectedContentLength: Int64

/*!
@method textEncodingName
Expand All @@ -96,7 +102,7 @@ public class NSURLResponse : NSObject, NSSecureCoding, NSCopying {
@result The name of the text encoding of the receiver, or nil if no
text encoding was specified.
*/
public var textEncodingName: String? { NSUnimplemented() }
public private(set) var textEncodingName: String?

/*!
@method suggestedFilename
Expand Down
63 changes: 63 additions & 0 deletions TestFoundation/TestNSURLResponse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//


#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif


class TestNSURLResponse : XCTestCase {
var allTests : [(String, () -> ())] {
return [
("test_URL", test_URL),
("test_MIMEType", test_MIMEType),
("test_ExpectedContentLength", test_ExpectedContentLength),
("test_TextEncodingName", test_TextEncodingName)
]
}

func test_URL() {
let url = NSURL(string: "a/test/path")!
let res = NSURLResponse(URL: url, MIMEType: "txt", expectedContentLength: 0, textEncodingName: nil)
XCTAssertEqual(res.URL, url, "should be the expected url")
}

func test_MIMEType() {
let mimetype1 = "text/plain"
let mimetype2 = "application/wordperfect"
let res1 = NSURLResponse(URL: NSURL(string: "test")!, MIMEType: mimetype1, expectedContentLength: 0, textEncodingName: nil)
XCTAssertEqual(res1.MIMEType, mimetype1, "should be the passed in mimetype")
let res2 = NSURLResponse(URL: NSURL(string: "test")!, MIMEType: mimetype2, expectedContentLength: 0, textEncodingName: nil)
XCTAssertEqual(res2.MIMEType, mimetype2, "should be the other mimetype")
}

func test_ExpectedContentLength() {
let zeroContentLength = 0
let positiveContentLength = 100
let url = NSURL(string: "test")!
let res1 = NSURLResponse(URL: url, MIMEType: "text/plain", expectedContentLength: zeroContentLength, textEncodingName: nil)
XCTAssertEqual(res1.expectedContentLength, Int64(zeroContentLength), "should be Int65 of the zero length")
let res2 = NSURLResponse(URL: url, MIMEType: "text/plain", expectedContentLength: positiveContentLength, textEncodingName: nil)
XCTAssertEqual(res2.expectedContentLength, Int64(positiveContentLength), "should be Int64 of the positive content length")
}

func test_TextEncodingName() {
let encoding = "utf8"
let url = NSURL(string: "test")!
let res1 = NSURLResponse(URL: url, MIMEType: nil, expectedContentLength: 0, textEncodingName: encoding)
XCTAssertEqual(res1.textEncodingName, encoding, "should be the utf8 encoding")
let res2 = NSURLResponse(URL: url, MIMEType: nil, expectedContentLength: 0, textEncodingName: nil)
XCTAssertNil(res2.textEncodingName)
}
}
1 change: 1 addition & 0 deletions TestFoundation/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ XCTMain([
TestNSHTTPCookie(),
TestNSGeometry(),
TestNSUUID(),
TestNSURLResponse(),
])