-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathImageTests.swift
More file actions
162 lines (150 loc) · 8.03 KB
/
Copy pathImageTests.swift
File metadata and controls
162 lines (150 loc) · 8.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// ImageTests.swift
// PMHTTP
//
// Created by Kevin Ballard on 1/17/17.
// Copyright © 2017 Postmates. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
import XCTest
import PMHTTP
#if os(iOS) || os(watchOS) || os(tvOS)
class ImageTests: PMHTTPTestCase {
lazy var sampleImage: UIImage = {
let size = CGSize(width: 50, height: 100)
UIGraphicsBeginImageContext(size)
defer { UIGraphicsEndImageContext() }
UIColor.blue.setFill()
UIRectFill(CGRect(origin: .zero, size: size))
return UIGraphicsGetImageFromCurrentImageContext()!
}()
func testGETImage() {
guard let imageData = sampleImage.jpegData(compressionQuality: 0.9) else {
return XCTFail("Could not get JPEG data for sample image")
}
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .ok, headers: ["Content-Type": "image/jpeg"], body: imageData))
}
expectationForRequestSuccess(HTTP.request(GET: "image").parseAsImage()) { [sampleImage] (task, response, image) in
// We can't really compare the two images directly, but we can make sure the size is correct
XCTAssertEqual(image.size, sampleImage.size, "image size")
XCTAssertEqual(image.scale, 1, "image scale")
}
waitForExpectations(timeout: 5, handler: nil)
// With scale
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .ok, headers: ["Content-Type": "image/jpeg"], body: imageData))
}
expectationForRequestSuccess(HTTP.request(GET: "image").parseAsImage(scale: 2)) { (task, response, image) in
XCTAssertEqual(image.scale, 2, "image scale")
}
waitForExpectations(timeout: 5, handler: nil)
}
func testGETImageNoContent() {
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .noContent))
}
expectationForRequestFailure(HTTP.request(GET: "image").parseAsImage()) { (task, response, error) in
XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 204, "response status code")
switch error {
case HTTPManagerError.unexpectedNoContent: break
default: XCTFail("expected error .unexpectedNoContent, found \(error)")
}
}
waitForExpectations(timeout: 5, handler: nil)
}
func testGETImageBadDecode() {
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .ok, headers: ["Content-Type": "image/jpeg"], body: Data([1,2,3])))
}
expectationForRequestFailure(HTTP.request(GET: "image").parseAsImage()) { (task, response, error) in
XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 200, "response status code")
switch error {
case HTTPManagerImageError.cannotDecode: break
default: XCTFail("expected error .cannotDecode, found \(error)")
}
}
waitForExpectations(timeout: 5, handler: nil)
}
func testGETImageBadMIMEType() {
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .ok, headers: ["Content-Type": "application/json"], text: "{ \"ok\": true }"))
}
expectationForRequestFailure(HTTP.request(GET: "image").parseAsImage()) { (task, response, error) in
switch error {
case HTTPManagerError.unexpectedContentType: break
default: XCTFail("expected error .unexpectedContentType: found \(error)")
}
}
waitForExpectations(timeout: 5, handler: nil)
}
func testPOSTImage() {
guard let imageData = sampleImage.jpegData(compressionQuality: 0.9) else {
return XCTFail("Could not get JPEG data for sample image")
}
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .ok, headers: ["Content-Type": "image/jpeg"], body: imageData))
}
expectationForRequestSuccess(HTTP.request(POST: "image").parseAsImage()) { [sampleImage] (task, response, image) in
guard let image = image else {
return XCTFail("expected image, got nil")
}
// We can't really compare the two images directly, but we can make sure the size is correct
XCTAssertEqual(image.size, sampleImage.size, "image size")
XCTAssertEqual(image.scale, 1, "image scale")
}
waitForExpectations(timeout: 5, handler: nil)
// With scale
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .ok, headers: ["Content-Type": "image/jpeg"], body: imageData))
}
expectationForRequestSuccess(HTTP.request(POST: "image").parseAsImage(scale: 2)) { (task, response, image) in
guard let image = image else {
return XCTFail("expected image, got nil")
}
XCTAssertEqual(image.scale, 2, "image scale")
}
waitForExpectations(timeout: 5, handler: nil)
}
func testPOSTImageNoContent() {
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .noContent))
}
expectationForRequestSuccess(HTTP.request(POST: "image").parseAsImage()) { (task, response, image) in
XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 204, "response status code")
XCTAssertNil(image, "image")
}
waitForExpectations(timeout: 5, handler: nil)
}
func testPOSTImageBadDecode() {
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .ok, headers: ["Content-Type": "image/jpeg"], body: Data([1,2,3])))
}
expectationForRequestFailure(HTTP.request(POST: "image").parseAsImage()) { (task, response, error) in
XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 200, "response status code")
switch error {
case HTTPManagerImageError.cannotDecode: break
default: XCTFail("expected error .cannotDecode, found \(error)")
}
}
waitForExpectations(timeout: 5, handler: nil)
}
func testPOSTImageBadMIMEType() {
expectationForHTTPRequest(httpServer, path: "image") { (request, completion) in
completion(HTTPServer.Response(status: .ok, headers: ["Content-Type": "application/json"], text: "{ \"ok\": true }"))
}
expectationForRequestFailure(HTTP.request(POST: "image").parseAsImage()) { (task, response, error) in
switch error {
case HTTPManagerError.unexpectedContentType: break
default: XCTFail("expected error .unexpectedContentType: found \(error)")
}
}
waitForExpectations(timeout: 5, handler: nil)
}
}
#endif