Skip to content

Commit 32bd019

Browse files
Cullen MacDonaldcullenbmacdonald
authored andcommitted
basic implementation for NSURLResponse init and tests
1 parent 998cd52 commit 32bd019

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@
203203
DCDBB8331C1768AC00313299 /* TestNSData.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCDBB8321C1768AC00313299 /* TestNSData.swift */; };
204204
83712C8E1C1684900049AD49 /* TestNSURLRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */; };
205205
C2A9D75C1C15C08B00993803 /* TestNSUUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */; };
206+
7A7D6FBB1C16439400957E2E /* TestNSURLResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */; };
206207
E876A73E1C1180E000F279EC /* TestNSRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = E876A73D1C1180E000F279EC /* TestNSRange.swift */; };
207208
848A30581C137B3500C83206 /* TestNSHTTPCookie.swift in Sources */ = {isa = PBXBuildFile; fileRef = 848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */; };
208209
EA66F6361BEED03E00136161 /* TargetConditionals.h in Headers */ = {isa = PBXBuildFile; fileRef = EA66F6351BEED03E00136161 /* TargetConditionals.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -534,6 +535,7 @@
534535
DCDBB8321C1768AC00313299 /* TestNSData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSData.swift; sourceTree = "<group>"; };
535536
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLRequest.swift; sourceTree = "<group>"; };
536537
C2A9D75B1C15C08B00993803 /* TestNSUUID.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSUUID.swift; sourceTree = "<group>"; };
538+
7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLResponse.swift; sourceTree = "<group>"; };
537539
E876A73D1C1180E000F279EC /* TestNSRange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSRange.swift; sourceTree = "<group>"; };
538540
848A30571C137B3500C83206 /* TestNSHTTPCookie.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestNSHTTPCookie.swift; path = TestFoundation/TestNSHTTPCookie.swift; sourceTree = SOURCE_ROOT; };
539541
EA313DFC1BE7F2E90060A403 /* CFURLComponents_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CFURLComponents_Internal.h; sourceTree = "<group>"; };
@@ -1049,6 +1051,7 @@
10491051
84BA558D1C16F90900F48C54 /* TestNSTimeZone.swift */,
10501052
844DC3321C17584F005611F9 /* TestNSScanner.swift */,
10511053
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */,
1054+
7A7D6FBA1C16439400957E2E /* TestNSURLResponse.swift */,
10521055
);
10531056
name = Tests;
10541057
sourceTree = "<group>";
@@ -1752,6 +1755,7 @@
17521755
EA66F64A1BF1619600136161 /* TestNSArray.swift in Sources */,
17531756
5BC1D8BE1BF3B09E009D3973 /* TestNSCharacterSet.swift in Sources */,
17541757
EA66F6561BF1619600136161 /* TestNSString.swift in Sources */,
1758+
7A7D6FBB1C16439400957E2E /* TestNSURLResponse.swift in Sources */,
17551759
5B40F9F21C125187000E72E3 /* TestNSXMLParser.swift in Sources */,
17561760
88D28DE71C13AE9000494606 /* TestNSGeometry.swift in Sources */,
17571761
EA66F64C1BF1619600136161 /* TestNSDictionary.swift in Sources */,

Foundation/NSURLResponse.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
about receiving the content data for a URL load.
2222
*/
2323
public class NSURLResponse : NSObject, NSSecureCoding, NSCopying {
24-
24+
2525
static public func supportsSecureCoding() -> Bool {
2626
return true
2727
}
@@ -48,14 +48,20 @@ public class NSURLResponse : NSObject, NSSecureCoding, NSCopying {
4848
@result The initialized NSURLResponse.
4949
@discussion This is the designated initializer for NSURLResponse.
5050
*/
51-
public init(URL: NSURL, MIMEType: String?, expectedContentLength length: Int, textEncodingName name: String?) { NSUnimplemented() }
51+
public init(URL: NSURL, MIMEType: String?, expectedContentLength length: Int, textEncodingName name: String?) {
52+
self.URL = URL
53+
self.MIMEType = MIMEType
54+
self.expectedContentLength = Int64(length)
55+
self.textEncodingName = name
56+
}
5257

5358
/*!
5459
@method URL
5560
@abstract Returns the URL of the receiver.
5661
@result The URL of the receiver.
5762
*/
58-
/*@NSCopying*/ public var URL: NSURL? { NSUnimplemented() }
63+
/*@NSCopying*/ public private(set) var URL: NSURL?
64+
5965

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

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

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

101107
/*!
102108
@method suggestedFilename
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// This source file is part of the Swift.org open source project
2+
//
3+
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
5+
//
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8+
//
9+
10+
11+
#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
12+
import Foundation
13+
import XCTest
14+
#else
15+
import SwiftFoundation
16+
import SwiftXCTest
17+
#endif
18+
19+
20+
class TestNSURLResponse : XCTestCase {
21+
var allTests : [(String, () -> ())] {
22+
return [
23+
("test_URL", test_URL),
24+
("test_MIMEType", test_MIMEType),
25+
("test_ExpectedContentLength", test_ExpectedContentLength),
26+
("test_TextEncodingName", test_TextEncodingName)
27+
]
28+
}
29+
30+
func test_URL() {
31+
let url = NSURL(string: "a/test/path")!
32+
let res = NSURLResponse(URL: url, MIMEType: "txt", expectedContentLength: 0, textEncodingName: nil)
33+
XCTAssertEqual(res.URL, url, "should be the expected url")
34+
}
35+
36+
func test_MIMEType() {
37+
let mimetype1 = "text/plain"
38+
let mimetype2 = "application/wordperfect"
39+
let res1 = NSURLResponse(URL: NSURL(string: "test")!, MIMEType: mimetype1, expectedContentLength: 0, textEncodingName: nil)
40+
XCTAssertEqual(res1.MIMEType, mimetype1, "should be the passed in mimetype")
41+
let res2 = NSURLResponse(URL: NSURL(string: "test")!, MIMEType: mimetype2, expectedContentLength: 0, textEncodingName: nil)
42+
XCTAssertEqual(res2.MIMEType, mimetype2, "should be the other mimetype")
43+
}
44+
45+
func test_ExpectedContentLength() {
46+
let zeroContentLength = 0
47+
let positiveContentLength = 100
48+
let url = NSURL(string: "test")!
49+
let res1 = NSURLResponse(URL: url, MIMEType: "text/plain", expectedContentLength: zeroContentLength, textEncodingName: nil)
50+
XCTAssertEqual(res1.expectedContentLength, Int64(zeroContentLength), "should be Int65 of the zero length")
51+
let res2 = NSURLResponse(URL: url, MIMEType: "text/plain", expectedContentLength: positiveContentLength, textEncodingName: nil)
52+
XCTAssertEqual(res2.expectedContentLength, Int64(positiveContentLength), "should be Int64 of the positive content length")
53+
}
54+
55+
func test_TextEncodingName() {
56+
let encoding = "utf8"
57+
let url = NSURL(string: "test")!
58+
let res1 = NSURLResponse(URL: url, MIMEType: nil, expectedContentLength: 0, textEncodingName: encoding)
59+
XCTAssertEqual(res1.textEncodingName, encoding, "should be the utf8 encoding")
60+
let res2 = NSURLResponse(URL: url, MIMEType: nil, expectedContentLength: 0, textEncodingName: nil)
61+
XCTAssertNil(res2.textEncodingName)
62+
}
63+
}

TestFoundation/main.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,6 @@ XCTMain([
4444
TestNSHTTPCookie(),
4545
TestNSGeometry(),
4646
TestNSUUID(),
47+
TestNSURLResponse(),
4748
])
4849

0 commit comments

Comments
 (0)