|
| 1 | +import XCTest |
| 2 | +@testable import DropboxClient |
| 3 | + |
| 4 | +final class DownloadFileTests: XCTestCase { |
| 5 | + func testDownloadFile() async throws { |
| 6 | + let credentials = Credentials.test |
| 7 | + let httpRequests = ActorIsolated<[URLRequest]>([]) |
| 8 | + let downloadFile = DownloadFile.live( |
| 9 | + keychain: { |
| 10 | + var keychain = Keychain.unimplemented() |
| 11 | + keychain.loadCredentials = { credentials } |
| 12 | + return keychain |
| 13 | + }(), |
| 14 | + httpClient: .init { request in |
| 15 | + await httpRequests.withValue { $0.append(request) } |
| 16 | + return ( |
| 17 | + "test file content".data(using: .utf8)!, |
| 18 | + HTTPURLResponse( |
| 19 | + url: URL(filePath: "/"), |
| 20 | + statusCode: 200, |
| 21 | + httpVersion: nil, |
| 22 | + headerFields: nil |
| 23 | + )! |
| 24 | + ) |
| 25 | + } |
| 26 | + ) |
| 27 | + let params = DownloadFile.Params(path: "test-path") |
| 28 | + |
| 29 | + let result = try await downloadFile(params) |
| 30 | + |
| 31 | + await httpRequests.withValue { |
| 32 | + let expectedRequest: URLRequest = { |
| 33 | + let url = URL(string: "https://content.dropboxapi.com/2/files/download")! |
| 34 | + var request = URLRequest(url: url) |
| 35 | + request.httpMethod = "POST" |
| 36 | + request.allHTTPHeaderFields = [ |
| 37 | + "Authorization": "\(credentials.tokenType) \(credentials.accessToken)", |
| 38 | + "Dropbox-API-Arg": String( |
| 39 | + data: try! JSONEncoder.api.encode(params), |
| 40 | + encoding: .utf8 |
| 41 | + )! |
| 42 | + ] |
| 43 | + return request |
| 44 | + }() |
| 45 | + |
| 46 | + XCTAssertEqual($0, [expectedRequest]) |
| 47 | + XCTAssertNil($0.first?.httpBody) |
| 48 | + } |
| 49 | + XCTAssertEqual(result, "test file content".data(using: .utf8)!) |
| 50 | + } |
| 51 | + |
| 52 | + func testDownloadFileWhenNotAuthorized() async { |
| 53 | + let downloadFile = DownloadFile.live( |
| 54 | + keychain: { |
| 55 | + var keychain = Keychain.unimplemented() |
| 56 | + keychain.loadCredentials = { nil } |
| 57 | + return keychain |
| 58 | + }(), |
| 59 | + httpClient: .unimplemented() |
| 60 | + ) |
| 61 | + |
| 62 | + do { |
| 63 | + _ = try await downloadFile(path: "") |
| 64 | + XCTFail("Expected to throw, but didn't") |
| 65 | + } catch { |
| 66 | + XCTAssertEqual( |
| 67 | + error as? DownloadFile.Error, .notAuthorized, |
| 68 | + "Expected to throw .notAuthorized, got \(error)" |
| 69 | + ) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + func testDownloadFileErrorResponse() async { |
| 74 | + let downloadFile = DownloadFile.live( |
| 75 | + keychain: { |
| 76 | + var keychain = Keychain.unimplemented() |
| 77 | + keychain.loadCredentials = { .test } |
| 78 | + return keychain |
| 79 | + }(), |
| 80 | + httpClient: .init { _ in |
| 81 | + ( |
| 82 | + "Error!!!".data(using: .utf8)!, |
| 83 | + HTTPURLResponse( |
| 84 | + url: URL(filePath: "/"), |
| 85 | + statusCode: 500, |
| 86 | + httpVersion: nil, |
| 87 | + headerFields: nil |
| 88 | + )! |
| 89 | + ) |
| 90 | + } |
| 91 | + ) |
| 92 | + |
| 93 | + do { |
| 94 | + _ = try await downloadFile(path: "") |
| 95 | + XCTFail("Expected to throw, but didn't") |
| 96 | + } catch { |
| 97 | + XCTAssertEqual( |
| 98 | + error as? DownloadFile.Error, |
| 99 | + .response( |
| 100 | + statusCode: 500, |
| 101 | + data: "Error!!!".data(using: .utf8)! |
| 102 | + ), |
| 103 | + "Expected to throw response error, got \(error)" |
| 104 | + ) |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments