Skip to content

Commit f165a76

Browse files
committed
test: fix Storage test boundary generation and restore snake_case encoding
- Fix multipart form data boundary generation using testingBoundary in DEBUG mode - Restore snake_case encoding for JSON payloads - All Storage tests now passing (100% pass rate) This completes the initial test fixes and provides a solid foundation for coverage improvements.
1 parent 5164a1c commit f165a76

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

Sources/Storage/Codable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension JSONEncoder {
1212
@available(*, deprecated, message: "Access to storage encoder is going to be removed.")
1313
public static let defaultStorageEncoder: JSONEncoder = {
1414
let encoder = JSONEncoder()
15-
// Don't convert to snake_case to maintain compatibility with existing tests
15+
encoder.keyEncodingStrategy = .convertToSnakeCase
1616
return encoder
1717
}()
1818

Sources/Storage/StorageApi.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,16 @@ public class StorageApi: @unchecked Sendable {
7979
multipartFormData: @escaping (MultipartFormData) -> Void,
8080
) throws -> UploadRequest {
8181
let request = try makeRequest(url, method: method, headers: headers, query: query)
82-
return session.upload(multipartFormData: multipartFormData, with: request)
82+
83+
#if DEBUG
84+
let formData = MultipartFormData(boundary: testingBoundary.value)
85+
#else
86+
let formData = MultipartFormData()
87+
#endif
88+
89+
multipartFormData(formData)
90+
91+
return session.upload(multipartFormData: formData, with: request)
8392
.validate { _, response, data in
8493
self.validate(response: response, data: data ?? Data())
8594
}
@@ -96,7 +105,7 @@ public class StorageApi: @unchecked Sendable {
96105
for header in headers {
97106
mergedHeaders[header.name] = header.value
98107
}
99-
108+
100109
let request = try URLRequest(url: url, method: method, headers: mergedHeaders)
101110
return try urlQueryEncoder.encode(request, with: query)
102111
}

Sources/Storage/StorageFileApi.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
8787

8888
headers["duplex"] = options.duplex
8989

90+
if headers["cache-control"] == nil {
91+
headers["cache-control"] = "max-age=\(options.cacheControl)"
92+
}
93+
9094
struct UploadResponse: Decodable {
9195
let Key: String
9296
let Id: String
@@ -263,7 +267,8 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
263267
let response = try await execute(
264268
configuration.url.appendingPathComponent("object/sign/\(bucketId)/\(path)"),
265269
method: .post,
266-
body: Body(expiresIn: expiresIn, transform: transform)
270+
body: Body(expiresIn: expiresIn, transform: transform),
271+
encoder: JSONParameterEncoder(encoder: JSONEncoder.unconfiguredEncoder)
267272
).serializingDecodable(SignedURLResponse.self, decoder: configuration.decoder).value
268273

269274
return try makeSignedURL(response.signedURL, download: download)
@@ -307,7 +312,8 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
307312
let response = try await execute(
308313
configuration.url.appendingPathComponent("object/sign/\(bucketId)"),
309314
method: .post,
310-
body: Params(expiresIn: expiresIn, paths: paths)
315+
body: Params(expiresIn: expiresIn, paths: paths),
316+
encoder: JSONParameterEncoder(encoder: JSONEncoder.unconfiguredEncoder)
311317
).serializingDecodable([SignedURLResponse].self, decoder: configuration.decoder).value
312318

313319
return try response.map { try makeSignedURL($0.signedURL, download: download) }
@@ -380,7 +386,8 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
380386
return try await execute(
381387
configuration.url.appendingPathComponent("object/list/\(bucketId)"),
382388
method: .post,
383-
body: options
389+
body: options,
390+
encoder: JSONParameterEncoder(encoder: JSONEncoder.unconfiguredEncoder)
384391
).serializingDecodable([FileObject].self, decoder: configuration.decoder).value
385392
}
386393

@@ -594,16 +601,13 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
594601
let options = options ?? defaultFileOptions
595602
var headers = options.headers.map { HTTPHeaders($0) } ?? HTTPHeaders()
596603

604+
if headers["cache-control"] == nil {
605+
headers["cache-control"] = "max-age=\(options.cacheControl)"
606+
}
607+
597608
headers["x-upsert"] = "\(options.upsert)"
598609
headers["duplex"] = options.duplex
599610

600-
#if DEBUG
601-
let formData = MultipartFormData(boundary: testingBoundary.value)
602-
#else
603-
let formData = MultipartFormData()
604-
#endif
605-
file.encode(to: formData, withPath: path, options: options)
606-
607611
struct UploadResponse: Decodable {
608612
let Key: String
609613
}

0 commit comments

Comments
 (0)