7
7
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8
8
//
9
9
10
+ #if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
11
+ #if canImport(SwiftFoundationNetworking) && !DEPLOYMENT_RUNTIME_OBJC
12
+ @testable import SwiftFoundationNetworking
13
+ #else
14
+ #if canImport(FoundationNetworking)
15
+ @testable import FoundationNetworking
16
+ #endif
17
+ #endif
18
+ #endif
19
+
10
20
class TestURLProtectionSpace : XCTestCase {
11
21
12
22
static var allTests : [ ( String , ( TestURLProtectionSpace ) -> ( ) throws -> Void ) ] {
13
- return [
23
+ var tests : [ ( String , ( TestURLProtectionSpace ) -> ( ) throws -> ( ) ) ] = [
14
24
( " test_description " , test_description) ,
15
25
]
26
+
27
+ #if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
28
+ tests. append ( contentsOf: [
29
+ ( " test_createWithHTTPURLresponse " , test_createWithHTTPURLresponse) ,
30
+ ] )
31
+ #endif
32
+
33
+ return tests
16
34
}
17
35
18
36
func test_description( ) {
@@ -36,4 +54,100 @@ class TestURLProtectionSpace : XCTestCase {
36
54
XCTAssert ( space. description. hasPrefix ( " < \( type ( of: space) ) " ) )
37
55
XCTAssert ( space. description. hasSuffix ( " : Host:apple.com, Server:http, Auth-Scheme:NSURLAuthenticationMethodHTMLForm, Realm:(null), Port:80, Proxy:NO, Proxy-Type:(null) " ) )
38
56
}
57
+
58
+ #if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
59
+ func test_createWithHTTPURLresponse( ) throws {
60
+ // Real responce from outlook.office365.com
61
+ let headerFields1 = [
62
+ " Server " : " Microsoft-IIS/10.0 " ,
63
+ " request-id " : " c71c2202-4013-4d64-9319-d40aba6bbe5c " ,
64
+ " WWW-Authenticate " : " Basic Realm= \" \" " ,
65
+ " X-Powered-By " : " ASP.NET " ,
66
+ " X-FEServer " : " AM6PR0502CA0062 " ,
67
+ " Date " : " Sat, 04 Apr 2020 16:19:39 GMT " ,
68
+ " Content-Length " : " 0 " ,
69
+ ]
70
+ let response1 = try XCTUnwrap ( HTTPURLResponse ( url: URL ( string: " https://outlook.office365.com/Microsoft-Server-ActiveSync " ) !,
71
+ statusCode: 401 ,
72
+ httpVersion: " HTTP/1.1 " ,
73
+ headerFields: headerFields1) )
74
+ let space1 = try XCTUnwrap ( URLProtectionSpace . create ( with: response1) , " Failed to create protection space from valid response " )
75
+
76
+ XCTAssertEqual ( space1. authenticationMethod, NSURLAuthenticationMethodHTTPBasic)
77
+ XCTAssertEqual ( space1. protocol, " https " )
78
+ XCTAssertEqual ( space1. host, " outlook.office365.com " )
79
+ XCTAssertEqual ( space1. port, 443 )
80
+ XCTAssertEqual ( space1. realm, " " )
81
+
82
+ // Real response from jigsaw.w3.org
83
+ let headerFields2 = [
84
+ " date " : " Sat, 04 Apr 2020 17:24:23 GMT " ,
85
+ " content-length " : " 261 " ,
86
+ " content-type " : " text/html;charset=ISO-8859-1 " ,
87
+ " server " : " Jigsaw/2.3.0-beta3 " ,
88
+ " www-authenticate " : " Basic realm= \" test \" " ,
89
+ " strict-transport-security " : " max-age=15552015; includeSubDomains; preload " ,
90
+ " public-key-pins " : " pin-sha256= \" cN0QSpPIkuwpT6iP2YjEo1bEwGpH/yiUn6yhdy+HNto= \" ; pin-sha256= \" WGJkyYjx1QMdMe0UqlyOKXtydPDVrk7sl2fV+nNm1r4= \" ; pin-sha256= \" LrKdTxZLRTvyHM4/atX2nquX9BeHRZMCxg3cf4rhc2I= \" ; max-age=864000 " ,
91
+ " x-frame-options " : " deny " ,
92
+ " x-xss-protection " : " 1; mode=block " ,
93
+ ]
94
+ let response2 = try XCTUnwrap ( HTTPURLResponse ( url: URL ( string: " https://jigsaw.w3.org/HTTP/Basic/ " ) !,
95
+ statusCode: 401 ,
96
+ httpVersion: " HTTP/2 " ,
97
+ headerFields: headerFields2) )
98
+ let space2 = try XCTUnwrap ( URLProtectionSpace . create ( with: response2) , " Failed to create protection space from valid response " )
99
+
100
+ XCTAssertEqual ( space2. authenticationMethod, NSURLAuthenticationMethodHTTPBasic)
101
+ XCTAssertEqual ( space2. protocol, " https " )
102
+ XCTAssertEqual ( space2. host, " jigsaw.w3.org " )
103
+ XCTAssertEqual ( space2. port, 443 )
104
+ XCTAssertEqual ( space2. realm, " test " )
105
+
106
+ // More cases with partial response
107
+ let authenticate3 = " Digest realm= \" Test \\ \" quoted \\ \" \" , domain= \" /HTTP/Digest \" , nonce= \" be2e96ad8ab8acb7ccfb49bc7e162914 \" "
108
+ let response3 = try XCTUnwrap ( HTTPURLResponse ( url: URL ( string: " http://jigsaw.w3.org/HTTP/Basic/ " ) !,
109
+ statusCode: 401 ,
110
+ httpVersion: " HTTP/1.1 " ,
111
+ headerFields: [ " www-authenticate " : authenticate3] ) )
112
+ let space3 = try XCTUnwrap ( URLProtectionSpace . create ( with: response3) , " Failed to create protection space from valid response " )
113
+
114
+ XCTAssertEqual ( space3. authenticationMethod, NSURLAuthenticationMethodHTTPDigest)
115
+ XCTAssertEqual ( space3. protocol, " http " )
116
+ XCTAssertEqual ( space3. host, " jigsaw.w3.org " )
117
+ XCTAssertEqual ( space3. port, 80 )
118
+ XCTAssertEqual ( space3. realm, " Test \" quoted \" " )
119
+
120
+ let response4 = try XCTUnwrap ( HTTPURLResponse ( url: URL ( string: " http://apple.com:333 " ) !,
121
+ statusCode: 401 ,
122
+ httpVersion: " HTTP/1.1 " ,
123
+ headerFields: [ " www-authenTicate " : " NTLM realm= \" \\ \" \" " ] ) )
124
+ let space4 = try XCTUnwrap ( URLProtectionSpace . create ( with: response4) , " Failed to create protection space from valid response " )
125
+
126
+ XCTAssertEqual ( space4. authenticationMethod, NSURLAuthenticationMethodNTLM)
127
+ XCTAssertEqual ( space4. protocol, " http " )
128
+ XCTAssertEqual ( space4. host, " apple.com " )
129
+ XCTAssertEqual ( space4. port, 333 )
130
+ XCTAssertEqual ( space4. realm, " \" " )
131
+
132
+ // Some broken headers
133
+ let response5 = try XCTUnwrap ( HTTPURLResponse ( url: URL ( string: " http://apple.com " ) !,
134
+ statusCode: 401 ,
135
+ httpVersion: " HTTP/1.1 " ,
136
+ headerFields: [ " www-authenicate " : " Basic " ] ) )
137
+ let space5 = URLProtectionSpace . create ( with: response5)
138
+ XCTAssertNil ( space5, " Should not create protection space for response without valid header " )
139
+
140
+ let response6 = try XCTUnwrap ( HTTPURLResponse ( url: URL ( string: " http://apple.com " ) !,
141
+ statusCode: 401 ,
142
+ httpVersion: " HTTP/1.1 " ,
143
+ headerFields: [ " www-authenticate " : " NT LM realm= " ] ) )
144
+ let space6 = try XCTUnwrap ( URLProtectionSpace . create ( with: response6) , " Failed to create protection space from valid response " )
145
+
146
+ XCTAssertEqual ( space6. authenticationMethod, NSURLAuthenticationMethodDefault)
147
+ XCTAssertEqual ( space6. protocol, " http " )
148
+ XCTAssertEqual ( space6. host, " apple.com " )
149
+ XCTAssertEqual ( space6. port, 80 )
150
+ XCTAssertNil ( space6. realm)
151
+ }
152
+ #endif
39
153
}
0 commit comments