Skip to content

Commit 5d86f27

Browse files
authored
Add option to create a custom data type (#121)
* feat: Add option to create a custom data type * doc: Update README.md * fix: Apply PR suggestions
1 parent f887c0e commit 5d86f27

File tree

4 files changed

+56
-28
lines changed

4 files changed

+56
-28
lines changed

Mocker.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
8ED91F36283AFDC300EA8E99 /* wetransfer_bot_avatar.png in Resources */ = {isa = PBXBuildFile; fileRef = 8ED91F32283AFDC300EA8E99 /* wetransfer_bot_avatar.png */; };
1919
8ED91F37283AFDC300EA8E99 /* example.json in Resources */ = {isa = PBXBuildFile; fileRef = 8ED91F34283AFDC300EA8E99 /* example.json */; };
2020
8ED91F38283AFDC300EA8E99 /* sample-redirect-get.data in Resources */ = {isa = PBXBuildFile; fileRef = 8ED91F35283AFDC300EA8E99 /* sample-redirect-get.data */; };
21+
A4B51386288FD81B00C52F4A /* Mock+DataType.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B51385288FD81B00C52F4A /* Mock+DataType.swift */; };
2122
/* End PBXBuildFile section */
2223

2324
/* Begin PBXContainerItemProxy section */
@@ -48,6 +49,7 @@
4849
8ED91F32283AFDC300EA8E99 /* wetransfer_bot_avatar.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = wetransfer_bot_avatar.png; sourceTree = "<group>"; };
4950
8ED91F34283AFDC300EA8E99 /* example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = example.json; sourceTree = "<group>"; };
5051
8ED91F35283AFDC300EA8E99 /* sample-redirect-get.data */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "sample-redirect-get.data"; sourceTree = "<group>"; };
52+
A4B51385288FD81B00C52F4A /* Mock+DataType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Mock+DataType.swift"; sourceTree = "<group>"; };
5153
/* End PBXFileReference section */
5254

5355
/* Begin PBXFrameworksBuildPhase section */
@@ -98,6 +100,7 @@
98100
503446151F3DB4660039D5E4 /* Mocker.swift */,
99101
503446161F3DB4660039D5E4 /* MockingURLProtocol.swift */,
100102
501B8FC3247E89C600B885F4 /* XCTest+Mocker.swift */,
103+
A4B51385288FD81B00C52F4A /* Mock+DataType.swift */,
101104
);
102105
path = Sources;
103106
sourceTree = "<group>";
@@ -294,6 +297,7 @@
294297
files = (
295298
503446191F3DB4660039D5E4 /* MockingURLProtocol.swift in Sources */,
296299
501B8FC4247E89C600B885F4 /* XCTest+Mocker.swift in Sources */,
300+
A4B51386288FD81B00C52F4A /* Mock+DataType.swift in Sources */,
297301
503446181F3DB4660039D5E4 /* Mocker.swift in Sources */,
298302
503446171F3DB4660039D5E4 /* Mock.swift in Sources */,
299303
);

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,22 @@ URLSession.shared.dataTask(with: exampleURL) { (data, response, error) in
150150
}.resume()
151151
```
152152

153+
##### Custom DataType
154+
In addition to the already build in static `DataType` implementations it is possible to create custom ones which will be used as the value to the `Content-Type` header key.
155+
156+
```swift
157+
let xmlURL = URL(string: "https://www.wetransfer.com/sample-xml.xml")!
158+
159+
Mock(fileExtensions: "png", dataType: .init(name: "xml", headerValue: "text/xml"), statusCode: 200, data: [
160+
.get: try! Data(contentsOf: MockedData.sampleXML)
161+
]).register()
162+
163+
URLSession.shared.dataTask(with: xmlURL) { (data, response, error) in
164+
let sampleXML: Data = data // This is the xml from your resources.
165+
}.resume(
166+
```
167+
168+
153169
##### Delayed responses
154170
Sometimes you want to test if cancellation of requests is working. In that case, the mocked request should not finish immediately and you need an delay. This can be added easily:
155171

Sources/Mock+DataType.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// Mock+DataType.swift
3+
// Mocker
4+
//
5+
// Created by Weiß, Alexander on 26.07.22.
6+
// Copyright © 2022 WeTransfer. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension Mock {
12+
/// The types of content of a request. Will be used as Content-Type header inside a `Mock`.
13+
public struct DataType {
14+
15+
/// Name of the data type.
16+
public let name: String
17+
18+
/// The header value of the data type.
19+
public let headerValue: String
20+
21+
public init(name: String, headerValue: String) {
22+
self.name = name
23+
self.headerValue = headerValue
24+
}
25+
}
26+
}
27+
28+
extension Mock.DataType {
29+
public static let json = Mock.DataType(name: "json", headerValue: "application/json; charset=utf-8")
30+
public static let html = Mock.DataType(name: "html", headerValue: "text/html; charset=utf-8")
31+
public static let imagePNG = Mock.DataType(name: "imagePNG", headerValue: "image/png")
32+
public static let pdf = Mock.DataType(name: "pdf", headerValue: "application/pdf")
33+
public static let mp4 = Mock.DataType(name: "mp4", headerValue: "video/mp4")
34+
public static let zip = Mock.DataType(name: "zip", headerValue: "application/zip")
35+
}

Sources/Mock.swift

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,6 @@ public struct Mock: Equatable {
3232
case connect = "CONNECT"
3333
}
3434

35-
/// The types of content of a request. Will be used as Content-Type header inside a `Mock`.
36-
public enum DataType: String {
37-
case json
38-
case html
39-
case imagePNG
40-
case pdf
41-
case mp4
42-
case zip
43-
44-
var headerValue: String {
45-
switch self {
46-
case .json:
47-
return "application/json; charset=utf-8"
48-
case .html:
49-
return "text/html; charset=utf-8"
50-
case .imagePNG:
51-
return "image/png"
52-
case .pdf:
53-
return "application/pdf"
54-
case .mp4:
55-
return "video/mp4"
56-
case .zip:
57-
return "application/zip"
58-
}
59-
}
60-
}
61-
6235
public typealias OnRequest = (_ request: URLRequest, _ httpBodyArguments: [String: Any]?) -> Void
6336

6437
/// The type of the data which is returned.
@@ -119,7 +92,7 @@ public struct Mock: Equatable {
11992

12093
private init(url: URL? = nil, ignoreQuery: Bool = false, cacheStoragePolicy: URLCache.StoragePolicy = .notAllowed, dataType: DataType, statusCode: Int, data: [HTTPMethod: Data], requestError: Error? = nil, additionalHeaders: [String: String] = [:], fileExtensions: [String]? = nil) {
12194
self.urlToMock = url
122-
let generatedURL = URL(string: "https://mocked.wetransfer.com/\(dataType.rawValue)/\(statusCode)/\(data.keys.first!.rawValue)")!
95+
let generatedURL = URL(string: "https://mocked.wetransfer.com/\(dataType.name)/\(statusCode)/\(data.keys.first!.rawValue)")!
12396
self.generatedURL = generatedURL
12497
var request = URLRequest(url: url ?? generatedURL)
12598
request.httpMethod = data.keys.first!.rawValue

0 commit comments

Comments
 (0)