Skip to content

Commit

Permalink
Add option to create a custom data type (#121)
Browse files Browse the repository at this point in the history
* feat: Add option to create a custom data type

* doc: Update README.md

* fix: Apply PR suggestions
  • Loading branch information
alexanderwe authored Jul 28, 2022
1 parent f887c0e commit 5d86f27
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
4 changes: 4 additions & 0 deletions Mocker.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
8ED91F36283AFDC300EA8E99 /* wetransfer_bot_avatar.png in Resources */ = {isa = PBXBuildFile; fileRef = 8ED91F32283AFDC300EA8E99 /* wetransfer_bot_avatar.png */; };
8ED91F37283AFDC300EA8E99 /* example.json in Resources */ = {isa = PBXBuildFile; fileRef = 8ED91F34283AFDC300EA8E99 /* example.json */; };
8ED91F38283AFDC300EA8E99 /* sample-redirect-get.data in Resources */ = {isa = PBXBuildFile; fileRef = 8ED91F35283AFDC300EA8E99 /* sample-redirect-get.data */; };
A4B51386288FD81B00C52F4A /* Mock+DataType.swift in Sources */ = {isa = PBXBuildFile; fileRef = A4B51385288FD81B00C52F4A /* Mock+DataType.swift */; };
/* End PBXBuildFile section */

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

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -98,6 +100,7 @@
503446151F3DB4660039D5E4 /* Mocker.swift */,
503446161F3DB4660039D5E4 /* MockingURLProtocol.swift */,
501B8FC3247E89C600B885F4 /* XCTest+Mocker.swift */,
A4B51385288FD81B00C52F4A /* Mock+DataType.swift */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -294,6 +297,7 @@
files = (
503446191F3DB4660039D5E4 /* MockingURLProtocol.swift in Sources */,
501B8FC4247E89C600B885F4 /* XCTest+Mocker.swift in Sources */,
A4B51386288FD81B00C52F4A /* Mock+DataType.swift in Sources */,
503446181F3DB4660039D5E4 /* Mocker.swift in Sources */,
503446171F3DB4660039D5E4 /* Mock.swift in Sources */,
);
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,22 @@ URLSession.shared.dataTask(with: exampleURL) { (data, response, error) in
}.resume()
```

##### Custom DataType
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.

```swift
let xmlURL = URL(string: "https://www.wetransfer.com/sample-xml.xml")!

Mock(fileExtensions: "png", dataType: .init(name: "xml", headerValue: "text/xml"), statusCode: 200, data: [
.get: try! Data(contentsOf: MockedData.sampleXML)
]).register()

URLSession.shared.dataTask(with: xmlURL) { (data, response, error) in
let sampleXML: Data = data // This is the xml from your resources.
}.resume(
```


##### Delayed responses
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:

Expand Down
35 changes: 35 additions & 0 deletions Sources/Mock+DataType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Mock+DataType.swift
// Mocker
//
// Created by Weiß, Alexander on 26.07.22.
// Copyright © 2022 WeTransfer. All rights reserved.
//

import Foundation

extension Mock {
/// The types of content of a request. Will be used as Content-Type header inside a `Mock`.
public struct DataType {

/// Name of the data type.
public let name: String

/// The header value of the data type.
public let headerValue: String

public init(name: String, headerValue: String) {
self.name = name
self.headerValue = headerValue
}
}
}

extension Mock.DataType {
public static let json = Mock.DataType(name: "json", headerValue: "application/json; charset=utf-8")
public static let html = Mock.DataType(name: "html", headerValue: "text/html; charset=utf-8")
public static let imagePNG = Mock.DataType(name: "imagePNG", headerValue: "image/png")
public static let pdf = Mock.DataType(name: "pdf", headerValue: "application/pdf")
public static let mp4 = Mock.DataType(name: "mp4", headerValue: "video/mp4")
public static let zip = Mock.DataType(name: "zip", headerValue: "application/zip")
}
29 changes: 1 addition & 28 deletions Sources/Mock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,6 @@ public struct Mock: Equatable {
case connect = "CONNECT"
}

/// The types of content of a request. Will be used as Content-Type header inside a `Mock`.
public enum DataType: String {
case json
case html
case imagePNG
case pdf
case mp4
case zip

var headerValue: String {
switch self {
case .json:
return "application/json; charset=utf-8"
case .html:
return "text/html; charset=utf-8"
case .imagePNG:
return "image/png"
case .pdf:
return "application/pdf"
case .mp4:
return "video/mp4"
case .zip:
return "application/zip"
}
}
}

public typealias OnRequest = (_ request: URLRequest, _ httpBodyArguments: [String: Any]?) -> Void

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

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) {
self.urlToMock = url
let generatedURL = URL(string: "https://mocked.wetransfer.com/\(dataType.rawValue)/\(statusCode)/\(data.keys.first!.rawValue)")!
let generatedURL = URL(string: "https://mocked.wetransfer.com/\(dataType.name)/\(statusCode)/\(data.keys.first!.rawValue)")!
self.generatedURL = generatedURL
var request = URLRequest(url: url ?? generatedURL)
request.httpMethod = data.keys.first!.rawValue
Expand Down

0 comments on commit 5d86f27

Please sign in to comment.