Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
white49
Alamofire
提交
2b91b8fc
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,发现更多精彩内容 >>
提交
2b91b8fc
编写于
4月 09, 2016
作者:
C
Cédric Luthi
提交者:
Christian Noon
4月 09, 2016
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added status code and content type values into NSError userInfo dictionaries.
上级
8d9f79a8
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
86 addition
and
46 deletion
+86
-46
Source/Error.swift
Source/Error.swift
+9
-0
Source/Validation.swift
Source/Validation.swift
+25
-2
Tests/ValidationTests.swift
Tests/ValidationTests.swift
+52
-44
未找到文件。
Source/Error.swift
浏览文件 @
2b91b8fc
...
...
@@ -39,6 +39,15 @@ public struct Error {
case
PropertyListSerializationFailed
=
-
6007
}
/// Custom keys contained within certain NSError `userInfo` dictionaries generated by Alamofire.
public
struct
UserInfoKeys
{
/// The content type user info key for a `.ContentTypeValidationFailed` error stored as a `String` value.
public
static
let
ContentType
=
"ContentType"
/// The status code user info key for a `.StatusCodeValidationFailed` error stored as an `Int` value.
public
static
let
StatusCode
=
"StatusCode"
}
/**
Creates an `NSError` with the given error code and failure reason.
...
...
Source/Validation.swift
浏览文件 @
2b91b8fc
...
...
@@ -80,7 +80,17 @@ extension Request {
return
.
Success
}
else
{
let
failureReason
=
"Response status code was unacceptable:
\(
response
.
statusCode
)
"
return
.
Failure
(
Error
.
error
(
code
:
.
StatusCodeValidationFailed
,
failureReason
:
failureReason
))
let
error
=
NSError
(
domain
:
Error
.
Domain
,
code
:
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
,
userInfo
:
[
NSLocalizedFailureReasonErrorKey
:
failureReason
,
Error
.
UserInfoKeys
.
StatusCode
:
response
.
statusCode
]
)
return
.
Failure
(
error
)
}
}
}
...
...
@@ -149,18 +159,31 @@ extension Request {
}
}
let
contentType
:
String
let
failureReason
:
String
if
let
responseContentType
=
response
.
MIMEType
{
contentType
=
responseContentType
failureReason
=
(
"Response content type
\"\(
responseContentType
)\"
does not match any acceptable "
+
"content types:
\(
acceptableContentTypes
)
"
)
}
else
{
contentType
=
""
failureReason
=
"Response content type was missing and acceptable content type does not match
\"
*/*
\"
"
}
return
.
Failure
(
Error
.
error
(
code
:
.
ContentTypeValidationFailed
,
failureReason
:
failureReason
))
let
error
=
NSError
(
domain
:
Error
.
Domain
,
code
:
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
,
userInfo
:
[
NSLocalizedFailureReasonErrorKey
:
failureReason
,
Error
.
UserInfoKeys
.
ContentType
:
contentType
]
)
return
.
Failure
(
error
)
}
}
...
...
Tests/ValidationTests.swift
浏览文件 @
2b91b8fc
...
...
@@ -43,7 +43,7 @@ class StatusCodeValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNil
(
error
)
}
func
testThatValidationForRequestWithUnacceptableStatusCodeResponseFails
()
{
...
...
@@ -64,13 +64,14 @@ class StatusCodeValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
error
,
"error should not be nil"
)
XCTAssertNotNil
(
error
)
if
let
error
=
error
{
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
,
"domain should be Alamofire error domain"
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
,
"code should be status code validation failure"
)
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
)
XCTAssertEqual
(
error
.
userInfo
[
Error
.
UserInfoKeys
.
StatusCode
]
as?
Int
,
404
)
}
else
{
XCTFail
(
"error should
be an NSError
"
)
XCTFail
(
"error should
not be nil
"
)
}
}
...
...
@@ -92,13 +93,14 @@ class StatusCodeValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
error
,
"error should not be nil"
)
XCTAssertNotNil
(
error
)
if
let
error
=
error
{
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
,
"domain should be Alamofire error domain"
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
,
"code should be status code validation failure"
)
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
)
XCTAssertEqual
(
error
.
userInfo
[
Error
.
UserInfoKeys
.
StatusCode
]
as?
Int
,
201
)
}
else
{
XCTFail
(
"error should
be an NSError
"
)
XCTFail
(
"error should
not be nil
"
)
}
}
}
...
...
@@ -126,7 +128,7 @@ class ContentTypeValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNil
(
error
)
}
func
testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceeds
()
{
...
...
@@ -149,7 +151,7 @@ class ContentTypeValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNil
(
error
)
}
func
testThatValidationForRequestWithUnacceptableContentTypeResponseFails
()
{
...
...
@@ -170,13 +172,14 @@ class ContentTypeValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
error
,
"error should not be nil"
)
XCTAssertNotNil
(
error
)
if
let
error
=
error
{
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
,
"domain should be Alamofire error domain"
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
,
"code should be content type validation failure"
)
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
)
XCTAssertEqual
(
error
.
userInfo
[
Error
.
UserInfoKeys
.
ContentType
]
as?
String
,
"application/xml"
)
}
else
{
XCTFail
(
"error should
be an NSError
"
)
XCTFail
(
"error should
not be nil
"
)
}
}
...
...
@@ -201,10 +204,11 @@ class ContentTypeValidationTestCase: BaseTestCase {
XCTAssertNotNil
(
error
,
"error should not be nil"
)
if
let
error
=
error
{
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
,
"domain should be Alamofire error domain"
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
,
"code should be content type validation failure"
)
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
)
XCTAssertEqual
(
error
.
userInfo
[
Error
.
UserInfoKeys
.
ContentType
]
as?
String
,
"application/xml"
)
}
else
{
XCTFail
(
"error should
be an NSError
"
)
XCTFail
(
"error should
not be nil
"
)
}
}
...
...
@@ -226,7 +230,7 @@ class ContentTypeValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNil
(
error
)
}
func
testThatValidationForRequestWithAcceptableWildcardContentTypeResponseSucceedsWhenResponseIsNil
()
{
...
...
@@ -297,13 +301,13 @@ class ContentTypeValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
response
,
"response should not be nil"
)
XCTAssertNotNil
(
data
,
"data should not be nil"
)
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNotNil
(
response
)
XCTAssertNotNil
(
data
)
XCTAssertNil
(
error
)
if
let
response
=
response
{
XCTAssertEqual
(
response
.
statusCode
,
204
,
"response status code should be 204"
)
XCTAssertNil
(
response
.
MIMEType
,
"response mime type should be nil"
)
XCTAssertEqual
(
response
.
statusCode
,
204
)
XCTAssertNil
(
response
.
MIMEType
)
}
}
}
...
...
@@ -330,7 +334,7 @@ class MultipleValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNil
(
error
)
}
func
testThatValidationForRequestWithUnacceptableStatusCodeAndContentTypeResponseFailsWithStatusCodeError
()
{
...
...
@@ -352,13 +356,14 @@ class MultipleValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
error
,
"error should not be nil"
)
XCTAssertNotNil
(
error
)
if
let
error
=
error
{
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
,
"domain should be Alamofire error domain"
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
,
"code should be status code validation failure"
)
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
)
XCTAssertEqual
(
error
.
userInfo
[
Error
.
UserInfoKeys
.
StatusCode
]
as?
Int
,
200
)
}
else
{
XCTFail
(
"error should
be an NSError
"
)
XCTFail
(
"error should
not be nil
"
)
}
}
...
...
@@ -381,13 +386,14 @@ class MultipleValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
error
,
"error should not be nil"
)
XCTAssertNotNil
(
error
)
if
let
error
=
error
{
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
,
"domain should be Alamofire error domain"
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
,
"code should be content type validation failure"
)
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
)
XCTAssertEqual
(
error
.
userInfo
[
Error
.
UserInfoKeys
.
ContentType
]
as?
String
,
"application/xml"
)
}
else
{
XCTFail
(
"error should
be an NSError
"
)
XCTFail
(
"error should
not be nil
"
)
}
}
}
...
...
@@ -416,7 +422,7 @@ class AutomaticValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNil
(
error
)
}
func
testThatValidationForRequestWithUnacceptableStatusCodeResponseFails
()
{
...
...
@@ -437,13 +443,14 @@ class AutomaticValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
error
,
"error should not be nil"
)
XCTAssertNotNil
(
error
)
if
let
error
=
error
{
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
,
"domain should be Alamofire error domain"
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
,
"code should be status code validation failure"
)
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
StatusCodeValidationFailed
.
rawValue
)
XCTAssertEqual
(
error
.
userInfo
[
Error
.
UserInfoKeys
.
StatusCode
]
as?
Int
,
404
)
}
else
{
XCTFail
(
"error should
be an NSError
"
)
XCTFail
(
"error should
not be nil
"
)
}
}
...
...
@@ -468,7 +475,7 @@ class AutomaticValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNil
(
error
)
}
func
testThatValidationForRequestWithAcceptableComplexContentTypeResponseSucceeds
()
{
...
...
@@ -494,7 +501,7 @@ class AutomaticValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNil
(
error
,
"error should be nil"
)
XCTAssertNil
(
error
)
}
func
testThatValidationForRequestWithUnacceptableContentTypeResponseFails
()
{
...
...
@@ -518,13 +525,14 @@ class AutomaticValidationTestCase: BaseTestCase {
waitForExpectationsWithTimeout
(
timeout
,
handler
:
nil
)
// Then
XCTAssertNotNil
(
error
,
"error should not be nil"
)
XCTAssertNotNil
(
error
)
if
let
error
=
error
{
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
,
"domain should be Alamofire error domain"
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
,
"code should be content type validation failure"
)
XCTAssertEqual
(
error
.
domain
,
Error
.
Domain
)
XCTAssertEqual
(
error
.
code
,
Error
.
Code
.
ContentTypeValidationFailed
.
rawValue
)
XCTAssertEqual
(
error
.
userInfo
[
Error
.
UserInfoKeys
.
ContentType
]
as?
String
,
"application/xml"
)
}
else
{
XCTFail
(
"error should
be an NSError
"
)
XCTFail
(
"error should
not be nil
"
)
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录