From 5d86f27a8f80d4ba388bc1a379a3c2289a1f3d18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Wei=C3=9F?= Date: Thu, 28 Jul 2022 11:14:13 +0200 Subject: [PATCH] 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 --- Mocker.xcodeproj/project.pbxproj | 4 ++++ README.md | 16 +++++++++++++++ Sources/Mock+DataType.swift | 35 ++++++++++++++++++++++++++++++++ Sources/Mock.swift | 29 +------------------------- 4 files changed, 56 insertions(+), 28 deletions(-) create mode 100644 Sources/Mock+DataType.swift diff --git a/Mocker.xcodeproj/project.pbxproj b/Mocker.xcodeproj/project.pbxproj index ebf6fe2..ffa85e0 100644 --- a/Mocker.xcodeproj/project.pbxproj +++ b/Mocker.xcodeproj/project.pbxproj @@ -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 */ @@ -48,6 +49,7 @@ 8ED91F32283AFDC300EA8E99 /* wetransfer_bot_avatar.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = wetransfer_bot_avatar.png; sourceTree = ""; }; 8ED91F34283AFDC300EA8E99 /* example.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = example.json; sourceTree = ""; }; 8ED91F35283AFDC300EA8E99 /* sample-redirect-get.data */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = "sample-redirect-get.data"; sourceTree = ""; }; + A4B51385288FD81B00C52F4A /* Mock+DataType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Mock+DataType.swift"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -98,6 +100,7 @@ 503446151F3DB4660039D5E4 /* Mocker.swift */, 503446161F3DB4660039D5E4 /* MockingURLProtocol.swift */, 501B8FC3247E89C600B885F4 /* XCTest+Mocker.swift */, + A4B51385288FD81B00C52F4A /* Mock+DataType.swift */, ); path = Sources; sourceTree = ""; @@ -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 */, ); diff --git a/README.md b/README.md index 79deeeb..f9eb3ae 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/Sources/Mock+DataType.swift b/Sources/Mock+DataType.swift new file mode 100644 index 0000000..974c240 --- /dev/null +++ b/Sources/Mock+DataType.swift @@ -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") +} diff --git a/Sources/Mock.swift b/Sources/Mock.swift index 645ec60..fc1e7f0 100644 --- a/Sources/Mock.swift +++ b/Sources/Mock.swift @@ -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. @@ -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