HttpFiles.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import Foundation
  2. @testable import Trio
  3. /// Helper struct to download files from localhost via HTTP. Must have a HTTP server
  4. /// running on port 8123 that supports listing files and downloading files.
  5. ///
  6. /// You can set two ReplayTests variables `HTTP_FILES_OFFSET` and `HTTP_FILES_LENGTH`
  7. /// to implement paging
  8. ///
  9. /// This struct is only useful during testing as it is missing a number of error checks
  10. struct HttpFiles {
  11. static func listFiles() async throws -> [String] {
  12. let url = URL(string: "http://localhost:8123/list")!
  13. let (data, _) = try await URLSession.shared.data(from: url)
  14. let allFiles = try JSONDecoder().decode([String].self, from: data)
  15. let files: [String]
  16. if let offset = ReplayTests.filesOffset, let length = ReplayTests.filesLength
  17. {
  18. // Both variables exist and are valid integers
  19. let endIndex = min(offset + length, allFiles.count)
  20. let startIndex = min(offset, allFiles.count)
  21. files = Array(allFiles[startIndex ..< endIndex])
  22. } else {
  23. files = allFiles
  24. }
  25. if files.count > 5000 {
  26. fatalError("too many files: \(files.count) \(ProcessInfo.processInfo.environment)")
  27. }
  28. return files
  29. }
  30. static func downloadFile(at: String) async throws -> AlgorithmComparison {
  31. let decoder = JSONDecoder()
  32. decoder.dateDecodingStrategy = .secondsSince1970
  33. let dataUrl = URL(string: "http://localhost:8123\(at)")!
  34. let (data, _) = try await URLSession.shared.data(from: dataUrl)
  35. return try decoder.decode(AlgorithmComparison.self, from: data)
  36. }
  37. }