Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
white49
Alamofire
提交
1063611e
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,发现更多精彩内容 >>
提交
1063611e
编写于
4月 03, 2016
作者:
C
Christian Noon
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1141 from Alamofire/fix/redirect_with_completion
Fixed redirect with completion property not being called
上级
fad7390a
0c012f99
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
167 addition
and
18 deletion
+167
-18
Source/Manager.swift
Source/Manager.swift
+1
-1
Tests/ResponseTests.swift
Tests/ResponseTests.swift
+166
-17
未找到文件。
Source/Manager.swift
浏览文件 @
1063611e
...
...
@@ -742,7 +742,7 @@ public class Manager {
case
#selector(
NSURLSessionDelegate.URLSession(_:didReceiveChallenge:completionHandler:)
)
:
return
sessionDidReceiveChallenge
!=
nil
case
#selector(
NSURLSessionTaskDelegate.URLSession(_:task:willPerformHTTPRedirection:newRequest:completionHandler:)
)
:
return
taskWillPerformHTTPRedirection
!=
nil
return
(
taskWillPerformHTTPRedirection
!=
nil
||
taskWillPerformHTTPRedirectionWithCompletion
!=
nil
)
case
#selector(
NSURLSessionDataDelegate.URLSession(_:dataTask:didReceiveResponse:completionHandler:)
)
:
return
dataTaskDidReceiveResponse
!=
nil
default
:
...
...
Tests/ResponseTests.swift
浏览文件 @
1063611e
...
...
@@ -269,12 +269,15 @@ 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
()
Alamofire
.
Manager
.
sharedInstance
.
delegate
.
taskWillPerformHTTPRedirection
=
nil
}
// MARK: Tests
...
...
@@ -292,7 +295,7 @@ class RedirectResponseTestCase: BaseTestCase {
var
error
:
NSError
?
// When
Alamofire
.
request
(
.
GET
,
URLString
)
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
...
...
@@ -327,7 +330,7 @@ class RedirectResponseTestCase: BaseTestCase {
var
error
:
NSError
?
// When
Alamofire
.
request
(
.
GET
,
URLString
)
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
...
...
@@ -355,9 +358,11 @@ class RedirectResponseTestCase: BaseTestCase {
let
URLString
=
"https://httpbin.org/redirect-to?url=
\(
redirectURLString
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
Alamofire
.
Manager
.
sharedInstance
.
delegate
let
callbackExpectation
=
expectationWithDescription
(
"Redirect callback should be made"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
manager
.
delegate
delegate
.
taskWillPerformHTTPRedirection
=
{
_
,
_
,
_
,
request
in
callbackExpectation
.
fulfill
()
return
request
}
...
...
@@ -367,7 +372,7 @@ class RedirectResponseTestCase: BaseTestCase {
var
error
:
NSError
?
// When
Alamofire
.
request
(
.
GET
,
URLString
)
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
...
...
@@ -389,15 +394,101 @@ class RedirectResponseTestCase: BaseTestCase {
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
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
Alamofire
.
Manager
.
sharedInstance
.
delegate
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
}
...
...
@@ -407,7 +498,7 @@ class RedirectResponseTestCase: BaseTestCase {
var
error
:
NSError
?
// When
Alamofire
.
request
(
.
GET
,
URLString
)
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
...
...
@@ -415,7 +506,7 @@ class RedirectResponseTestCase: BaseTestCase {
error
=
responseError
expectation
.
fulfill
()
}
}
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
...
...
@@ -431,15 +522,24 @@ class RedirectResponseTestCase: BaseTestCase {
func
testThatTaskOverrideClosureIsCalledMultipleTimesForMultipleHTTPRedirects
()
{
// Given
let
redirectCount
=
5
let
redirectURLString
=
"https://httpbin.org/get"
let
URLString
=
"https://httpbin.org/redirect/
5
"
let
URLString
=
"https://httpbin.org/redirect/
\(
redirectCount
)
"
let
expectation
=
expectationWithDescription
(
"Request should redirect to
\(
redirectURLString
)
"
)
let
delegate
:
Alamofire
.
Manager
.
SessionDelegate
=
Alamofire
.
Manager
.
sharedInstance
.
delegate
var
totalRedirectCount
=
0
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
totalRedirectCount
+=
1
if
let
redirectExpectation
=
redirectExpectations
.
popLast
()
{
redirectExpectation
.
fulfill
()
}
else
{
XCTFail
(
"Too many redirect callbacks were received"
)
}
return
request
}
...
...
@@ -449,7 +549,7 @@ class RedirectResponseTestCase: BaseTestCase {
var
error
:
NSError
?
// When
Alamofire
.
request
(
.
GET
,
URLString
)
manager
.
request
(
.
GET
,
URLString
)
.
response
{
responseRequest
,
responseResponse
,
responseData
,
responseError
in
request
=
responseRequest
response
=
responseResponse
...
...
@@ -469,7 +569,58 @@ class RedirectResponseTestCase: BaseTestCase {
XCTAssertEqual
(
response
?
.
URL
?
.
URLString
??
""
,
redirectURLString
,
"response URL should match the redirect URL"
)
XCTAssertEqual
(
response
?
.
statusCode
??
-
1
,
200
,
"response should have a 200 status code"
)
XCTAssertEqual
(
totalRedirectCount
,
5
,
"total redirect count should be 5"
)
}
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
()
{
...
...
@@ -485,8 +636,6 @@ class RedirectResponseTestCase: BaseTestCase {
// 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.
let
manager
=
Manager
(
configuration
:
NSURLSessionConfiguration
.
ephemeralSessionConfiguration
())
manager
.
delegate
.
taskWillPerformHTTPRedirection
=
{
session
,
task
,
response
,
request
in
var
redirectedRequest
=
request
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录