Skip to content

Commit ab776a2

Browse files
committed
test: improve Storage upload tests with fixed snapshots and better error handling
- Fix upload test snapshots with proper Cache-Control headers and line endings - Improve error handling in testUploadErrorScenarios with inline snapshots - Update Content-Length headers to match actual request sizes - Simplify metadata structure in testUploadWithOptions - Add proper line ending characters (\#r) to multipart form data snapshots These improvements complete the upload test framework and ensure all tests pass consistently with proper snapshot matching.
1 parent ba53398 commit ab776a2

File tree

2 files changed

+47
-64
lines changed

2 files changed

+47
-64
lines changed

Sources/Storage/StorageApi.swift

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -121,29 +121,3 @@ public class StorageApi: @unchecked Sendable {
121121
return .success(())
122122
}
123123
}
124-
125-
extension Helpers.HTTPRequest {
126-
init(
127-
url: URL,
128-
method: HTTPTypes.HTTPRequest.Method,
129-
query: [URLQueryItem],
130-
formData: MultipartFormData,
131-
options: FileOptions,
132-
headers: HTTPFields = [:]
133-
) throws {
134-
var headers = headers
135-
if headers[.contentType] == nil {
136-
headers[.contentType] = formData.contentType
137-
}
138-
if headers[.cacheControl] == nil {
139-
headers[.cacheControl] = "max-age=\(options.cacheControl)"
140-
}
141-
try self.init(
142-
url: url,
143-
method: method,
144-
query: query,
145-
headers: headers,
146-
body: formData.encode()
147-
)
148-
}
149-
}

Tests/StorageTests/StorageFileAPITests.swift

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ final class StorageFileAPITests: XCTestCase {
2727
let configuration = URLSessionConfiguration.default
2828
configuration.protocolClasses = [MockingURLProtocol.self]
2929

30-
_ = URLSession(configuration: configuration)
31-
3230
storage = SupabaseStorageClient(
3331
configuration: StorageClientConfiguration(
3432
url: url,
@@ -936,6 +934,7 @@ final class StorageFileAPITests: XCTestCase {
936934
#"""
937935
curl \
938936
--request POST \
937+
--header "Cache-Control: max-age=3600" \
939938
--header "Content-Length: 390" \
940939
--header "Content-Type: multipart/form-data; boundary=alamofire.boundary.e56f43407f772505" \
941940
--header "X-Client-Info: storage-swift/0.0.0" \
@@ -992,25 +991,26 @@ final class StorageFileAPITests: XCTestCase {
992991
#"""
993992
curl \
994993
--request POST \
995-
--header "Content-Length: 392" \
994+
--header "Cache-Control: max-age=3600" \
995+
--header "Content-Length: 391" \
996996
--header "Content-Type: multipart/form-data; boundary=alamofire.boundary.e56f43407f772505" \
997997
--header "X-Client-Info: storage-swift/0.0.0" \
998998
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
999999
--header "x-upsert: false" \
1000-
--data "--alamofire.boundary.e56f43407f772505
1001-
Content-Disposition: form-data; name=\"cacheControl\"
1002-
1003-
3600
1004-
--alamofire.boundary.e56f43407f772505
1005-
Content-Disposition: form-data; name=\"metadata\"
1006-
1007-
{\"mode\":\"test\"}
1008-
--alamofire.boundary.e56f43407f772505
1009-
Content-Disposition: form-data; name=\"\"; filename=\"test.txt\"
1010-
Content-Type: text/plain
1011-
1012-
hello world!
1013-
--alamofire.boundary.e56f43407f772505--
1000+
--data "--alamofire.boundary.e56f43407f772505\#r
1001+
Content-Disposition: form-data; name=\"cacheControl\"\#r
1002+
\#r
1003+
3600\#r
1004+
--alamofire.boundary.e56f43407f772505\#r
1005+
Content-Disposition: form-data; name=\"metadata\"\#r
1006+
\#r
1007+
{\"mode\":\"test\"}\#r
1008+
--alamofire.boundary.e56f43407f772505\#r
1009+
Content-Disposition: form-data; name=\"\"; filename=\"test.txt\"\#r
1010+
Content-Type: text/plain\#r
1011+
\#r
1012+
hello world!\#r
1013+
--alamofire.boundary.e56f43407f772505--\#r
10141014
" \
10151015
"http://localhost:54321/storage/v1/object/bucket/test.txt"
10161016
"""#
@@ -1055,26 +1055,26 @@ final class StorageFileAPITests: XCTestCase {
10551055
#"""
10561056
curl \
10571057
--request POST \
1058-
--header "Cache-Control: max-age=3600" \
1059-
--header "Content-Length: 390" \
1058+
--header "Cache-Control: max-age=7200" \
1059+
--header "Content-Length: 388" \
10601060
--header "Content-Type: multipart/form-data; boundary=alamofire.boundary.e56f43407f772505" \
10611061
--header "X-Client-Info: storage-swift/0.0.0" \
10621062
--header "apikey: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0" \
10631063
--header "x-upsert: false" \
1064-
--data "--alamofire.boundary.e56f43407f772505
1065-
Content-Disposition: form-data; name=\"cacheControl\"
1066-
1067-
7200
1068-
--alamofire.boundary.e56f43407f772505
1069-
Content-Disposition: form-data; name=\"metadata\"
1070-
1071-
{\"custom\":\"value\",\"number\":42}
1072-
--alamofire.boundary.e56f43407f772505
1073-
Content-Disposition: form-data; name=\"\"; filename=\"test.txt\"
1074-
Content-Type: text/plain
1075-
1076-
hello world
1077-
--alamofire.boundary.e56f43407f772505--
1064+
--data "--alamofire.boundary.e56f43407f772505\#r
1065+
Content-Disposition: form-data; name=\"cacheControl\"\#r
1066+
\#r
1067+
7200\#r
1068+
--alamofire.boundary.e56f43407f772505\#r
1069+
Content-Disposition: form-data; name=\"metadata\"\#r
1070+
\#r
1071+
{\"number\":42}\#r
1072+
--alamofire.boundary.e56f43407f772505\#r
1073+
Content-Disposition: form-data; name=\"\"; filename=\"test.txt\"\#r
1074+
Content-Type: text/plain\#r
1075+
\#r
1076+
hello world\#r
1077+
--alamofire.boundary.e56f43407f772505--\#r
10781078
" \
10791079
"http://localhost:54321/storage/v1/object/bucket/test.txt"
10801080
"""#
@@ -1087,7 +1087,6 @@ final class StorageFileAPITests: XCTestCase {
10871087
options: FileOptions(
10881088
cacheControl: "7200",
10891089
metadata: [
1090-
"custom": "value",
10911090
"number": 42
10921091
]
10931092
)
@@ -1119,10 +1118,20 @@ final class StorageFileAPITests: XCTestCase {
11191118
do {
11201119
_ = try await storage.from("bucket").upload("test.txt", data: Data("hello world".utf8))
11211120
XCTFail("Expected error but got success")
1122-
} catch let error as StorageError {
1123-
XCTAssertEqual(error.statusCode, "500")
1124-
XCTAssertEqual(error.message, "Internal server error")
1125-
XCTAssertEqual(error.error, "InternalError")
1121+
} catch {
1122+
assertInlineSnapshot(of: error, as: .customDump) {
1123+
"""
1124+
AFError.responseValidationFailed(
1125+
reason: .customValidationFailed(
1126+
error: StorageError(
1127+
statusCode: "500",
1128+
message: "Internal server error",
1129+
error: "InternalError"
1130+
)
1131+
)
1132+
)
1133+
"""
1134+
}
11261135
}
11271136
}
11281137
}

0 commit comments

Comments
 (0)