Skip to content

Commit 05b097b

Browse files
authored
Replace occurrences of internal .data with Data(contentsOf:) (#92)
1 parent 85cf4d9 commit 05b097b

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

README.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ It's recommend to create a class with all your mocked data accessible. An exampl
7474

7575
```swift
7676
public final class MockedData {
77-
public static let botAvatarImageResponseHead: Data = Bundle(for: MockedData.self).url(forResource: "Resources/Responses/bot-avatar-image-head", withExtension: "data")!.data
77+
public static let botAvatarImageResponseHead: Data = try! Data(contentsOf: Bundle(for: MockedData.self).url(forResource: "Resources/Responses/bot-avatar-image-head", withExtension: "data")!)
7878
public static let botAvatarImageFileUrl: URL = Bundle(for: MockedData.self).url(forResource: "wetransfer_bot_avater", withExtension: "png")!
7979
public static let exampleJSON: URL = Bundle(for: MockedData.self).url(forResource: "Resources/JSON Files/example", withExtension: "json")!
8080
}
@@ -85,7 +85,7 @@ public final class MockedData {
8585
let originalURL = URL(string: "https://www.wetransfer.com/example.json")!
8686

8787
let mock = Mock(url: originalURL, dataType: .json, statusCode: 200, data: [
88-
.get : MockedData.exampleJSON.data // Data containing the JSON response
88+
.get : try! Data(contentsOf: MockedData.exampleJSON) // Data containing the JSON response
8989
])
9090
mock.register()
9191

@@ -108,7 +108,7 @@ Some URLs like authentication URLs contain timestamps or UUIDs in the query. To
108108
let originalURL = URL(string: "https://www.example.com/api/authentication?oauth_timestamp=151817037")!
109109

110110
let mock = Mock(url: originalURL, ignoreQuery: true, dataType: .json, statusCode: 200, data: [
111-
.get : MockedData.exampleJSON.data // Data containing the JSON response
111+
.get : try! Data(contentsOf: MockedData.exampleJSON) // Data containing the JSON response
112112
])
113113
mock.register()
114114

@@ -128,7 +128,7 @@ URLSession.shared.dataTask(with: originalURL) { (data, response, error) in
128128
let imageURL = URL(string: "https://www.wetransfer.com/sample-image.png")!
129129

130130
Mock(fileExtensions: "png", dataType: .imagePNG, statusCode: 200, data: [
131-
.get: MockedData.botAvatarImageFileUrl.data
131+
.get: try! Data(contentsOf: MockedData.botAvatarImageFileUrl)
132132
]).register()
133133

134134
URLSession.shared.dataTask(with: imageURL) { (data, response, error) in
@@ -141,8 +141,8 @@ URLSession.shared.dataTask(with: imageURL) { (data, response, error) in
141141
let exampleURL = URL(string: "https://www.wetransfer.com/api/endpoint")!
142142

143143
Mock(url: exampleURL, dataType: .json, statusCode: 200, data: [
144-
.head: MockedData.headResponse.data,
145-
.get: MockedData.exampleJSON.data
144+
.head: try! Data(contentsOf: MockedData.headResponse),
145+
.get: try! Data(contentsOf: MockedData.exampleJSON)
146146
]).register()
147147

148148
URLSession.shared.dataTask(with: exampleURL) { (data, response, error) in
@@ -157,8 +157,8 @@ Sometimes you want to test if cancellation of requests is working. In that case,
157157
let exampleURL = URL(string: "https://www.wetransfer.com/api/endpoint")!
158158

159159
var mock = Mock(url: exampleURL, dataType: .json, statusCode: 200, data: [
160-
.head: MockedData.headResponse.data,
161-
.get: MockedData.exampleJSON.data
160+
.head: try! Data(contentsOf: MockedData.headResponse),
161+
.get: try! Data(contentsOf: MockedData.exampleJSON)
162162
])
163163
mock.delay = DispatchTimeInterval.seconds(5)
164164
mock.register()
@@ -176,8 +176,8 @@ By creating a mock for the short URL and the redirect URL, you can mock redirect
176176

177177
```swift
178178
let urlWhichRedirects: URL = URL(string: "https://we.tl/redirect")!
179-
Mock(url: urlWhichRedirects, dataType: .html, statusCode: 200, data: [.get: MockedData.redirectGET.data]).register()
180-
Mock(url: URL(string: "https://wetransfer.com/redirect")!, dataType: .json, statusCode: 200, data: [.get: MockedData.exampleJSON.data]).register()
179+
Mock(url: urlWhichRedirects, dataType: .html, statusCode: 200, data: [.get: try! Data(contentsOf: MockedData.redirectGET)]).register()
180+
Mock(url: URL(string: "https://wetransfer.com/redirect")!, dataType: .json, statusCode: 200, data: [.get: try! Data(contentsOf: MockedData.exampleJSON)]).register()
181181
```
182182

183183
##### Ignoring URLs

0 commit comments

Comments
 (0)