Skip to content

Commit 064f128

Browse files
committed
test(helpers): add comprehensive tests for HTTPError
Add tests for the HTTPError type covering initialization, property access, and LocalizedError conformance for various data scenarios. Ensures robust error reporting and future-proofing for HTTP error handling.
1 parent 2db9adf commit 064f128

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
//
2+
// HTTPErrorTests.swift
3+
// Supabase
4+
//
5+
// Created by Guilherme Souza on 07/05/24.
6+
//
7+
8+
import Foundation
9+
import Helpers
10+
import XCTest
11+
12+
final class HTTPErrorTests: XCTestCase {
13+
14+
func testInitialization() {
15+
let data = Data("test error message".utf8)
16+
let response = HTTPURLResponse(
17+
url: URL(string: "https://example.com")!,
18+
statusCode: 400,
19+
httpVersion: "1.1",
20+
headerFields: ["Content-Type": "application/json"]
21+
)!
22+
23+
let error = HTTPError(data: data, response: response)
24+
25+
XCTAssertEqual(error.data, data)
26+
XCTAssertEqual(error.response, response)
27+
}
28+
29+
func testLocalizedErrorDescription_WithUTF8Data() {
30+
let data = Data("Bad Request: Invalid parameters".utf8)
31+
let response = HTTPURLResponse(
32+
url: URL(string: "https://example.com")!,
33+
statusCode: 400,
34+
httpVersion: "1.1",
35+
headerFields: nil
36+
)!
37+
38+
let error = HTTPError(data: data, response: response)
39+
40+
XCTAssertEqual(
41+
error.errorDescription,
42+
"Status Code: 400 Body: Bad Request: Invalid parameters"
43+
)
44+
}
45+
46+
func testLocalizedErrorDescription_WithEmptyData() {
47+
let data = Data()
48+
let response = HTTPURLResponse(
49+
url: URL(string: "https://example.com")!,
50+
statusCode: 404,
51+
httpVersion: "1.1",
52+
headerFields: nil
53+
)!
54+
55+
let error = HTTPError(data: data, response: response)
56+
57+
XCTAssertEqual(error.errorDescription, "Status Code: 404 Body: ")
58+
}
59+
60+
func testLocalizedErrorDescription_WithNonUTF8Data() {
61+
// Create data that can't be converted to UTF-8 string
62+
let bytes: [UInt8] = [0xFF, 0xFE, 0xFD, 0xFC]
63+
let data = Data(bytes)
64+
let response = HTTPURLResponse(
65+
url: URL(string: "https://example.com")!,
66+
statusCode: 500,
67+
httpVersion: "1.1",
68+
headerFields: nil
69+
)!
70+
71+
let error = HTTPError(data: data, response: response)
72+
73+
XCTAssertEqual(error.errorDescription, "Status Code: 500")
74+
}
75+
76+
func testLocalizedErrorDescription_WithJSONData() {
77+
let jsonString = """
78+
{
79+
"error": "Validation failed",
80+
"details": "Email format is invalid"
81+
}
82+
"""
83+
let data = Data(jsonString.utf8)
84+
let response = HTTPURLResponse(
85+
url: URL(string: "https://example.com")!,
86+
statusCode: 422,
87+
httpVersion: "1.1",
88+
headerFields: ["Content-Type": "application/json"]
89+
)!
90+
91+
let error = HTTPError(data: data, response: response)
92+
93+
XCTAssertEqual(
94+
error.errorDescription,
95+
"Status Code: 422 Body: \(jsonString)"
96+
)
97+
}
98+
99+
func testLocalizedErrorDescription_WithSpecialCharacters() {
100+
let message = "Error with special chars: áéíóú ñ ç"
101+
let data = Data(message.utf8)
102+
let response = HTTPURLResponse(
103+
url: URL(string: "https://example.com")!,
104+
statusCode: 400,
105+
httpVersion: "1.1",
106+
headerFields: nil
107+
)!
108+
109+
let error = HTTPError(data: data, response: response)
110+
111+
XCTAssertEqual(
112+
error.errorDescription,
113+
"Status Code: 400 Body: \(message)"
114+
)
115+
}
116+
117+
func testLocalizedErrorDescription_WithLargeData() {
118+
let largeMessage = String(repeating: "A", count: 1000)
119+
let data = Data(largeMessage.utf8)
120+
let response = HTTPURLResponse(
121+
url: URL(string: "https://example.com")!,
122+
statusCode: 413,
123+
httpVersion: "1.1",
124+
headerFields: nil
125+
)!
126+
127+
let error = HTTPError(data: data, response: response)
128+
129+
XCTAssertEqual(
130+
error.errorDescription,
131+
"Status Code: 413 Body: \(largeMessage)"
132+
)
133+
}
134+
135+
136+
func testProperties() {
137+
let data = Data("test error".utf8)
138+
let response = HTTPURLResponse(
139+
url: URL(string: "https://example.com")!,
140+
statusCode: 400,
141+
httpVersion: "1.1",
142+
headerFields: ["Content-Type": "application/json"]
143+
)!
144+
145+
let error = HTTPError(data: data, response: response)
146+
147+
// Test that properties are correctly set
148+
XCTAssertEqual(error.data, data)
149+
XCTAssertEqual(error.response, response)
150+
XCTAssertEqual(error.response.statusCode, 400)
151+
XCTAssertEqual(error.response.url, URL(string: "https://example.com")!)
152+
}
153+
}

0 commit comments

Comments
 (0)