Skip to content

Commit 0c90f72

Browse files
Damir TursunovićDamir Tursunović
authored andcommitted
Create unit tests for supported extensions and update readme
1 parent 62ad9b0 commit 0c90f72

File tree

5 files changed

+52
-7
lines changed

5 files changed

+52
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,12 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
170170
}
171171
```
172172

173+
173174
If you incorporate background-uploading, we strongly recommend you to inspect any failed uploads that may have occured in the background. Please refer to [Starting a new Session](#Starting a new Session) for more information.
174175

176+
## TUS Protocol Extensions
177+
The client assumes by default that the server implements the [Creation TUS protocol extension](https://tus.io/protocols/resumable-upload.html#protocol-extensions). If your server does not support that, please ensure to provide an empty array for the `supportedExtensions` parameter in the client initializer.
178+
175179
## Example app
176180

177181
Please refer to the [example app](/TUSKitExample) inside this project to see how to add photos from a PHPicker, using SwiftUI. You can also use the `PHPicker` mechanic for UIKit.

Sources/TUSKit/TUSClient.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ public final class TUSClient {
9090
/// You can also pass an absolute path, e.g. "file://uploads/TUS"
9191
/// - session: A URLSession you'd like to use. Will default to `URLSession.shared`.
9292
/// - chunkSize: The amount of bytes the data to upload will be chunked by. Defaults to 512 kB.
93+
/// - supportedExtensions: The TUS protocol extensions that the client should use. For now, the available supported extensions are `.creation`. Defaults to `[.creation]`.
94+
///
95+
/// - Important: The client assumes by default that your server implements the Creation TUS protocol extension. If your server does not support that,
96+
/// make sure that you provide an empty array in the `supportExtensions` parameter.
9397
/// - Throws: File related errors when it can't make a directory at the designated path.
9498
public init(server: URL, sessionIdentifier: String, storageDirectory: URL? = nil, session: URLSession = URLSession.shared, chunkSize: Int = 500 * 1024, supportedExtensions: [TUSProtocolExtension] = [.creation]) throws {
9599
self.sessionIdentifier = sessionIdentifier

Sources/TUSKit/TUSProtocolExtension.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
// Created by Brad Patras on 7/14/22.
66
//
77

8+
/// Available [TUS protocol extensions](https://tus.io/protocols/resumable-upload.html#protocol-extensions) that
9+
/// the client supports.
810
public enum TUSProtocolExtension {
911
case creation
1012
}
11-
12-
extension Array where Element == TUSProtocolExtension {
13-
public static let all: [TUSProtocolExtension] = [.creation]
14-
}
15-

Tests/TUSKitTests/Support/Support.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ func clearDirectory(dir: URL) {
3030
}
3131
}
3232

33-
func makeClient(storagePath: URL?) -> TUSClient {
33+
func makeClient(storagePath: URL?, supportedExtensions: [TUSProtocolExtension] = [.creation]) -> TUSClient {
3434
let liveDemoPath = URL(string: "https://tusd.tusdemo.net/files")!
3535

3636
// We don't use a live URLSession, we mock it out.
3737
let configuration = URLSessionConfiguration.default
3838
configuration.protocolClasses = [MockURLProtocol.self]
3939
do {
40-
let client = try TUSClient(server: liveDemoPath, sessionIdentifier: "TEST", storageDirectory: storagePath, session: URLSession.init(configuration: configuration))
40+
let client = try TUSClient(server: liveDemoPath,
41+
sessionIdentifier: "TEST",
42+
storageDirectory: storagePath,
43+
session: URLSession.init(configuration: configuration),
44+
supportedExtensions: supportedExtensions)
4145
return client
4246
} catch {
4347
XCTFail("Could not create TUSClient instance \(error)")

Tests/TUSKitTests/TUSClient/TUSClientTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,42 @@ final class TUSClientTests: XCTestCase {
9090
XCTAssertEqual(0, client.remainingUploads)
9191
}
9292

93+
// MARK: - Supported Extensions
94+
95+
func testClientExcludesCreationStep() throws {
96+
prepareNetworkForSuccesfulStatusCall(data: data)
97+
client = makeClient(storagePath: fullStoragePath, supportedExtensions: [])
98+
client.delegate = tusDelegate
99+
100+
// Act
101+
try client.upload(data: data)
102+
waitForUploadsToFinish()
103+
104+
// Assert (ensure that the create HTTP request has not been called)
105+
XCTAssertFalse(MockURLProtocol.receivedRequests.contains(where: {
106+
$0.httpMethod == "POST" &&
107+
$0.url?.absoluteString == "https://tusd.tusdemo.net/files" &&
108+
$0.allHTTPHeaderFields?["Upload-Extension"] == "creation"
109+
}))
110+
}
111+
112+
func testClientIncludesCreationStep() throws {
113+
prepareNetworkForSuccesfulStatusCall(data: data)
114+
client = makeClient(storagePath: fullStoragePath, supportedExtensions: [.creation])
115+
client.delegate = tusDelegate
116+
117+
// Act
118+
try client.upload(data: data)
119+
waitForUploadsToFinish()
120+
121+
// Assert (ensure that the create HTTP request has not been called)
122+
XCTAssert(MockURLProtocol.receivedRequests.contains(where: {
123+
$0.httpMethod == "POST" &&
124+
$0.url?.absoluteString == "https://tusd.tusdemo.net/files" &&
125+
$0.allHTTPHeaderFields?["Upload-Extension"] == "creation"
126+
}))
127+
}
128+
93129
/// Upload data, a certain amount of times, and wait for it to be done.
94130
/// Can optionally prepare a failing upload too.
95131
@discardableResult

0 commit comments

Comments
 (0)