Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
white49
Alamofire
提交
ca4a8a51
A
Alamofire
项目概览
white49
/
Alamofire
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
Alamofire
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ca4a8a51
编写于
4月 03, 2016
作者:
C
Christian Noon
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Moved the redirect tests into a new SessionDelegateTests file.
上级
8ea36fcc
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
449 addition
and
418 deletion
+449
-418
Alamofire.xcodeproj/project.pbxproj
Alamofire.xcodeproj/project.pbxproj
+8
-0
Tests/ResponseTests.swift
Tests/ResponseTests.swift
+0
-418
Tests/SessionDelegateTests.swift
Tests/SessionDelegateTests.swift
+441
-0
未找到文件。
Alamofire.xcodeproj/project.pbxproj
浏览文件 @
ca4a8a51
...
...
@@ -117,6 +117,9 @@
4C83F41B1B749E0E00203445
/* Stream.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4C83F41A1B749E0E00203445
/* Stream.swift */
;
};
4C83F41C1B749E0E00203445
/* Stream.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4C83F41A1B749E0E00203445
/* Stream.swift */
;
};
4C83F41D1B749E0E00203445
/* Stream.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4C83F41A1B749E0E00203445
/* Stream.swift */
;
};
4C9DCE781CB1BCE2003E6463
/* SessionDelegateTests.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4C9DCE771CB1BCE2003E6463
/* SessionDelegateTests.swift */
;
};
4C9DCE791CB1BCE2003E6463
/* SessionDelegateTests.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4C9DCE771CB1BCE2003E6463
/* SessionDelegateTests.swift */
;
};
4C9DCE7A1CB1BCE2003E6463
/* SessionDelegateTests.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4C9DCE771CB1BCE2003E6463
/* SessionDelegateTests.swift */
;
};
4CA028C51B7466C500C84163
/* ResultTests.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4CA028C41B7466C500C84163
/* ResultTests.swift */
;
};
4CA028C61B7466C500C84163
/* ResultTests.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4CA028C41B7466C500C84163
/* ResultTests.swift */
;
};
4CB928291C66BFBC00CE5F08
/* Notifications.swift in Sources */
=
{
isa
=
PBXBuildFile
;
fileRef
=
4CB928281C66BFBC00CE5F08
/* Notifications.swift */
;
};
...
...
@@ -272,6 +275,7 @@
4C812C5F1B535F6D0017E0BF
/* root-ca-disig.cer */
=
{
isa
=
PBXFileReference
;
lastKnownFileType
=
file
;
name
=
"root-ca-disig.cer"
;
path
=
"disig.sk/root-ca-disig.cer"
;
sourceTree
=
"<group>"
;
};
4C812C601B535F6D0017E0BF
/* testssl-expire.disig.sk.cer */
=
{
isa
=
PBXFileReference
;
lastKnownFileType
=
file
;
name
=
"testssl-expire.disig.sk.cer"
;
path
=
"disig.sk/testssl-expire.disig.sk.cer"
;
sourceTree
=
"<group>"
;
};
4C83F41A1B749E0E00203445
/* Stream.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
Stream.swift
;
sourceTree
=
"<group>"
;
};
4C9DCE771CB1BCE2003E6463
/* SessionDelegateTests.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
SessionDelegateTests.swift
;
sourceTree
=
"<group>"
;
};
4CA028C41B7466C500C84163
/* ResultTests.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
ResultTests.swift
;
sourceTree
=
"<group>"
;
};
4CB928281C66BFBC00CE5F08
/* Notifications.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
Notifications.swift
;
sourceTree
=
"<group>"
;
};
4CCFA7991B2BE71600B6F460
/* URLProtocolTests.swift */
=
{
isa
=
PBXFileReference
;
fileEncoding
=
4
;
lastKnownFileType
=
sourcecode.swift
;
path
=
URLProtocolTests.swift
;
sourceTree
=
"<group>"
;
};
...
...
@@ -377,6 +381,7 @@
F8111E5D19A9674D0040E7D1
/* RequestTests.swift */
,
F8111E5E19A9674D0040E7D1
/* ResponseTests.swift */
,
4CA028C41B7466C500C84163
/* ResultTests.swift */
,
4C9DCE771CB1BCE2003E6463
/* SessionDelegateTests.swift */
,
);
name
=
Core
;
sourceTree
=
"<group>"
;
...
...
@@ -989,6 +994,7 @@
4CF627181BA7CC240011A099
/* RequestTests.swift in Sources */
,
4CF627211BA7CC240011A099
/* TLSEvaluationTests.swift in Sources */
,
4CF627221BA7CC240011A099
/* UploadTests.swift in Sources */
,
4C9DCE7A1CB1BCE2003E6463
/* SessionDelegateTests.swift in Sources */
,
4C80F9F91BB730F6001B46D2
/* String+AlamofireTests.swift in Sources */
,
4CF6271E1BA7CC240011A099
/* MultipartFormDataTests.swift in Sources */
,
4CF627201BA7CC240011A099
/* ServerTrustPolicyTests.swift in Sources */
,
...
...
@@ -1087,6 +1093,7 @@
4C3238E71B3604DB00FE04AE
/* MultipartFormDataTests.swift in Sources */
,
4C33A1431B52089C00873DFF
/* ServerTrustPolicyTests.swift in Sources */
,
4C341BBA1B1A865A00C1B34D
/* CacheTests.swift in Sources */
,
4C9DCE781CB1BCE2003E6463
/* SessionDelegateTests.swift in Sources */
,
4C4CBE7B1BAF700C0024D659
/* String+AlamofireTests.swift in Sources */
,
4CA028C51B7466C500C84163
/* ResultTests.swift in Sources */
,
4CCFA79A1B2BE71600B6F460
/* URLProtocolTests.swift in Sources */
,
...
...
@@ -1113,6 +1120,7 @@
4C3238E81B3604DB00FE04AE
/* MultipartFormDataTests.swift in Sources */
,
4C33A1441B52089C00873DFF
/* ServerTrustPolicyTests.swift in Sources */
,
4C341BBB1B1A865A00C1B34D
/* CacheTests.swift in Sources */
,
4C9DCE791CB1BCE2003E6463
/* SessionDelegateTests.swift in Sources */
,
4C4CBE7C1BAF700C0024D659
/* String+AlamofireTests.swift in Sources */
,
4CA028C61B7466C500C84163
/* ResultTests.swift in Sources */
,
4CCFA79B1B2BE71600B6F460
/* URLProtocolTests.swift in Sources */
,
...
...
Tests/ResponseTests.swift
浏览文件 @
ca4a8a51
...
...
@@ -265,421 +265,3 @@ class ResponseJSONTestCase: BaseTestCase {
}
}
}
// MARK: -
class
RedirectResponseTestCase
:
BaseTestCase
{
var
manager
:
Alamofire
.
Manager
!
// MARK: Setup and Teardown
override
func
setUp
()
{
super
.
setUp
()
manager
=
Alamofire
.
Manager
(
configuration
:
NSURLSessionConfiguration
.
ephemeralSessionConfiguration
())
}
override
func
tearDown
()
{
super
.
tearDown
()
}
// MARK: Tests
func
testThatRequestWillPerformHTTPRedirectionByDefault
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatRequestWillPerformRedirectionMultipleTimesByDefault
()
{
// Given
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect/5"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatTaskOverrideClosureCanPerformHTTPRedirection
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirection
=
{
_
,
_
,
_
,
request
in
callbackExpectation
.
fulfill
()
return
request
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatTaskOverrideClosureWithCompletionCanPerformHTTPRedirection
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirectionWithCompletion
=
{
_
,
_
,
_
,
request
,
completion
in
completion
(
request
)
callbackExpectation
.
fulfill
()
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatTaskOverrideClosureCanCancelHTTPRedirection
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should not redirect to
\(
redirectURLString
)
"
)
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirectionWithCompletion
=
{
_
,
_
,
_
,
_
,
completion
in
callbackExpectation
.
fulfill
()
completion
(
nil
)
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
URLString
,
"response URL should match the origin URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
302
,
"response should have a 302 status code"
)
}
func
testThatTaskOverrideClosureWithCompletionCanCancelHTTPRedirection
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should not redirect to
\(
redirectURLString
)
"
)
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirection
=
{
_
,
_
,
_
,
_
in
callbackExpectation
.
fulfill
()
return
nil
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
URLString
,
"response URL should match the origin URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
302
,
"response should have a 302 status code"
)
}
func
testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects
()
{
// Given
let
redirectCount
=
5
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect/
\(
redirectCount
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
var
redirectExpectations
=
[
XCTestExpectation
]()
for
index
in
0
..<
redirectCount
{
redirectExpectations
.
insert
(
expectationWithDescription
(
"Redirect #
\(
index
)
callback was received"
),
atIndex
:
0
)
}
delegate
.
taskWillPerformHTTPRedirection
=
{
_
,
_
,
_
,
request
in
if
let
redirectExpectation
=
redirectExpectations
.
popLast
()
{
redirectExpectation
.
fulfill
()
}
else
{
XCTFail
(
"Too many redirect callbacks were received"
)
}
return
request
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatTaskOverrideClosureWithCompletionIsCalledMultipleTimesForMultipleHTTPRedirects
()
{
// Given
let
redirectCount
=
5
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect/
\(
redirectCount
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
var
redirectExpectations
=
[
XCTestExpectation
]()
for
index
in
0
..<
redirectCount
{
redirectExpectations
.
insert
(
expectationWithDescription
(
"Redirect #
\(
index
)
callback was received"
),
atIndex
:
0
)
}
delegate
.
taskWillPerformHTTPRedirectionWithCompletion
=
{
_
,
_
,
_
,
request
,
completion
in
if
let
redirectExpectation
=
redirectExpectations
.
popLast
()
{
redirectExpectation
.
fulfill
()
}
else
{
XCTFail
(
"Too many redirect callbacks were received"
)
}
completion
(
request
)
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatRedirectedRequestContainsAllHeadersFromOriginalRequest
()
{
// Given
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
headers
=
[
"Authorization"
:
"1234"
,
"Custom-Header"
:
"foobar"
,
]
// NOTE: It appears that most headers are maintained during a redirect with the exception of the `Authorization`
// header. It appears that Apple's strips the `Authorization` header from the redirected URL request. If you
// need to maintain the `Authorization` header, you need to manually append it to the redirected request.
manager
.
delegate
.
taskWillPerformHTTPRedirection
=
{
session
,
task
,
response
,
request
in
var
redirectedRequest
=
request
if
let
originalRequest
=
task
.
originalRequest
,
headers
=
originalRequest
.
allHTTPHeaderFields
,
authorizationHeaderValue
=
headers
[
"Authorization"
]
{
let
mutableRequest
=
request
.
mutableCopy
()
as!
NSMutableURLRequest
mutableRequest
.
setValue
(
authorizationHeaderValue
,
forHTTPHeaderField
:
"Authorization"
)
redirectedRequest
=
mutableRequest
}
return
redirectedRequest
}
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
var
response
:
Response
<
AnyObject
,
NSError
>
?
// When
manager
.
request
(
.
GET
,
URLString
,
headers
:
headers
)
.
responseJSON
{
closureResponse
in
response
=
closureResponse
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
response
?
.
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
?
.
response
,
"response should not be nil"
)
XCTAssertNotNil
(
response
?
.
data
,
"data should not be nil"
)
XCTAssertTrue
(
response
?
.
result
.
isSuccess
??
false
,
"response result should be a success"
)
if
let
JSON
=
response
?
.
result
.
value
as?
[
String
:
AnyObject
],
headers
=
JSON
[
"headers"
]
as?
[
String
:
String
]
{
XCTAssertEqual
(
headers
[
"Custom-Header"
],
"foobar"
,
"Custom-Header should be equal to foobar"
)
XCTAssertEqual
(
headers
[
"Authorization"
],
"1234"
,
"Authorization header should be equal to 1234"
)
}
}
}
Tests/SessionDelegateTests.swift
0 → 100644
浏览文件 @
ca4a8a51
// SessionDelegateTests.swift
//
// Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
@testable
import
Alamofire
import
Foundation
import
XCTest
class
SessionDelegateTestCase
:
BaseTestCase
{
var
manager
:
Alamofire
.
Manager
!
// MARK: - Setup and Teardown
override
func
setUp
()
{
super
.
setUp
()
manager
=
Alamofire
.
Manager
(
configuration
:
NSURLSessionConfiguration
.
ephemeralSessionConfiguration
())
}
override
func
tearDown
()
{
super
.
tearDown
()
}
// MARK: - Tests - Redirects
func
testThatRequestWillPerformHTTPRedirectionByDefault
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatRequestWillPerformRedirectionMultipleTimesByDefault
()
{
// Given
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect/5"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatTaskOverrideClosureCanPerformHTTPRedirection
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirection
=
{
_
,
_
,
_
,
request
in
callbackExpectation
.
fulfill
()
return
request
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatTaskOverrideClosureWithCompletionCanPerformHTTPRedirection
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirectionWithCompletion
=
{
_
,
_
,
_
,
request
,
completion
in
completion
(
request
)
callbackExpectation
.
fulfill
()
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatTaskOverrideClosureCanCancelHTTPRedirection
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should not redirect to
\(
redirectURLString
)
"
)
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirectionWithCompletion
=
{
_
,
_
,
_
,
_
,
completion
in
callbackExpectation
.
fulfill
()
completion
(
nil
)
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
URLString
,
"response URL should match the origin URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
302
,
"response should have a 302 status code"
)
}
func
testThatTaskOverrideClosureWithCompletionCanCancelHTTPRedirection
()
{
// Given
let
redirectURLString
=
"https://www.apple.com"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should not redirect to
\(
redirectURLString
)
"
)
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirection
=
{
_
,
_
,
_
,
_
in
callbackExpectation
.
fulfill
()
return
nil
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
URLString
,
"response URL should match the origin URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
302
,
"response should have a 302 status code"
)
}
func
testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects
()
{
// Given
let
redirectCount
=
5
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect/
\(
redirectCount
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
var
redirectExpectations
=
[
XCTestExpectation
]()
for
index
in
0
..<
redirectCount
{
redirectExpectations
.
insert
(
expectationWithDescription
(
"Redirect #
\(
index
)
callback was received"
),
atIndex
:
0
)
}
delegate
.
taskWillPerformHTTPRedirection
=
{
_
,
_
,
_
,
request
in
if
let
redirectExpectation
=
redirectExpectations
.
popLast
()
{
redirectExpectation
.
fulfill
()
}
else
{
XCTFail
(
"Too many redirect callbacks were received"
)
}
return
request
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatTaskOverrideClosureWithCompletionIsCalledMultipleTimesForMultipleHTTPRedirects
()
{
// Given
let
redirectCount
=
5
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect/
\(
redirectCount
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
var
redirectExpectations
=
[
XCTestExpectation
]()
for
index
in
0
..<
redirectCount
{
redirectExpectations
.
insert
(
expectationWithDescription
(
"Redirect #
\(
index
)
callback was received"
),
atIndex
:
0
)
}
delegate
.
taskWillPerformHTTPRedirectionWithCompletion
=
{
_
,
_
,
_
,
request
,
completion
in
if
let
redirectExpectation
=
redirectExpectations
.
popLast
()
{
redirectExpectation
.
fulfill
()
}
else
{
XCTFail
(
"Too many redirect callbacks were received"
)
}
completion
(
request
)
}
var
request
:
NSURLRequest
?
var
response
:
NSHTTPURLResponse
?
var
data
:
NSData
?
var
error
:
NSError
?
// When
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
data
=
responseData
error
=
responseError
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
}
func
testThatRedirectedRequestContainsAllHeadersFromOriginalRequest
()
{
// Given
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
headers
=
[
"Authorization"
:
"1234"
,
"Custom-Header"
:
"foobar"
,
]
// NOTE: It appears that most headers are maintained during a redirect with the exception of the `Authorization`
// header. It appears that Apple's strips the `Authorization` header from the redirected URL request. If you
// need to maintain the `Authorization` header, you need to manually append it to the redirected request.
manager
.
delegate
.
taskWillPerformHTTPRedirection
=
{
session
,
task
,
response
,
request
in
var
redirectedRequest
=
request
if
let
originalRequest
=
task
.
originalRequest
,
headers
=
originalRequest
.
allHTTPHeaderFields
,
authorizationHeaderValue
=
headers
[
"Authorization"
]
{
let
mutableRequest
=
request
.
mutableCopy
()
as!
NSMutableURLRequest
mutableRequest
.
setValue
(
authorizationHeaderValue
,
forHTTPHeaderField
:
"Authorization"
)
redirectedRequest
=
mutableRequest
}
return
redirectedRequest
}
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
var
response
:
Response
<
AnyObject
,
NSError
>
?
// When
manager
.
request
(
.
GET
,
URLString
,
headers
:
headers
)
.
responseJSON
{
closureResponse
in
response
=
closureResponse
expectation
.
fulfill
()
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
response
?
.
request
,
"request should not be nil"
)
XCTAssertNotNil
(
response
?
.
response
,
"response should not be nil"
)
XCTAssertNotNil
(
response
?
.
data
,
"data should not be nil"
)
XCTAssertTrue
(
response
?
.
result
.
isSuccess
??
false
,
"response result should be a success"
)
if
let
JSON
=
response
?
.
result
.
value
as?
[
String
:
AnyObject
],
headers
=
JSON
[
"headers"
]
as?
[
String
:
String
]
{
XCTAssertEqual
(
headers
[
"Custom-Header"
],
"foobar"
,
"Custom-Header should be equal to foobar"
)
XCTAssertEqual
(
headers
[
"Authorization"
],
"1234"
,
"Authorization header should be equal to 1234"
)
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录