Osiris/Tests/OsirisTests/HTTPRequestErrorTests.swift
Sami Samhuri d2576b729e
Add Codable support and overhaul the API
This introduces a cleaner, more intuitive API for making HTTP requests
with explicit methods for different content types and built-in Codable
support.

**New**
  - Add explicit request methods: .postJSON(), .postForm(),
    .postMultipart() for clear intent
  - Add direct `Codable` body support with automatic JSON
    encoding/decoding
  - Add `HTTPRequestBody` enum for internal type safety and cleaner
    implementation
  - Add proper query parameter encoding for GET and DELETE requests
    (previously ignored)
  - Add URLSession extensions for streamlined async JSON decoding with
    `HTTPError` for failure response status codes
  - Add comprehensive test coverage

The new API replaces the parameter-based methods using dictionaries with
explicitly-typed ones. Instead of passing a content-type parameter, you
now use purpose-built methods like `postJSON` and `postForm`.

**Breaking changes**
  - Minimum deployment targets raised to iOS 16.0 and macOS 13.0
  - Direct access to `parameters` and `parts` properties deprecated on
    `HTTPRequest`
  - GET and DELETE requests now validate that they don't have request
    bodies, and the new API prevents you from constructing them
2025-06-23 23:55:55 -04:00

72 lines
No EOL
2.3 KiB
Swift

//
// HTTPRequestErrorTests.swift
// OsirisTests
//
// Created by Sami Samhuri on 2025-06-15.
//
@testable import Osiris
import XCTest
class HTTPRequestErrorTests: XCTestCase {
func testHTTPError() {
let error = HTTPRequestError.http
XCTAssertEqual(error.localizedDescription, "HTTP request failed with non-2xx status code")
XCTAssertEqual(error.failureReason, "The server returned an error status code")
XCTAssertEqual(error.recoverySuggestion, "Check the server response for error details")
}
func testUnknownError() {
let error = HTTPRequestError.unknown
XCTAssertEqual(error.localizedDescription, "An unknown error occurred")
XCTAssertEqual(error.failureReason, "An unexpected error occurred during the request")
XCTAssertEqual(error.recoverySuggestion, "Check network connectivity and try again")
}
func testErrorDescriptionIsNeverNil() {
let allErrors: [HTTPRequestError] = [
.http,
.unknown,
.invalidRequestBody
]
for error in allErrors {
XCTAssertNotNil(error.errorDescription)
XCTAssertFalse(error.errorDescription!.isEmpty)
}
}
func testFailureReasonIsNeverNil() {
let allErrors: [HTTPRequestError] = [
.http,
.unknown,
.invalidRequestBody
]
for error in allErrors {
XCTAssertNotNil(error.failureReason)
XCTAssertFalse(error.failureReason!.isEmpty)
}
}
func testRecoverySuggestionIsNeverNil() {
let allErrors: [HTTPRequestError] = [
.http,
.unknown,
.invalidRequestBody
]
for error in allErrors {
XCTAssertNotNil(error.recoverySuggestion)
XCTAssertFalse(error.recoverySuggestion!.isEmpty)
}
}
func testInvalidRequestBodyError() {
let error = HTTPRequestError.invalidRequestBody
XCTAssertEqual(error.localizedDescription, "GET and DELETE requests cannot have a body")
XCTAssertEqual(error.failureReason, "The HTTP method does not support a request body")
XCTAssertEqual(error.recoverySuggestion, "Use query parameters instead of a request body for GET and DELETE requests")
}
}