|
| 1 | +// |
| 2 | +// ErrorTests.swift |
| 3 | +// swift-webpush |
| 4 | +// |
| 5 | +// Created by Dimitri Bouniol on 2024-12-21. |
| 6 | +// Copyright © 2024 Mochi Development, Inc. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import AsyncHTTPClient |
| 10 | +import Foundation |
| 11 | +import Testing |
| 12 | +@testable import WebPush |
| 13 | + |
| 14 | +@Suite struct ErrorTests { |
| 15 | + @Test func badSubscriberError() { |
| 16 | + #expect(BadSubscriberError() == BadSubscriberError()) |
| 17 | + #expect("\(BadSubscriberError().localizedDescription)" == "The subscription is no longer valid.") |
| 18 | + } |
| 19 | + |
| 20 | + @Test func base64URLDecodingError() { |
| 21 | + #expect(Base64URLDecodingError() == Base64URLDecodingError()) |
| 22 | + #expect("\(Base64URLDecodingError().localizedDescription)" == "The Base64 data could not be decoded.") |
| 23 | + } |
| 24 | + |
| 25 | + @Test func httpError() { |
| 26 | + let response = HTTPClientResponse(status: .notFound) |
| 27 | + #expect(HTTPError(response: response) == HTTPError(response: response)) |
| 28 | + #expect(HTTPError(response: response).hashValue == HTTPError(response: response).hashValue) |
| 29 | + #expect(HTTPError(response: response) != HTTPError(response: HTTPClientResponse(status: .internalServerError))) |
| 30 | + #expect("\(HTTPError(response: response).localizedDescription)" == "A 404 Not Found HTTP error was encountered: \(response).") |
| 31 | + } |
| 32 | + |
| 33 | + @Test func messageTooLargeError() { |
| 34 | + #expect(MessageTooLargeError() == MessageTooLargeError()) |
| 35 | + #expect("\(MessageTooLargeError().localizedDescription)" == "The message was too large, and could not be delivered to the push service.") |
| 36 | + } |
| 37 | + |
| 38 | + @Test func userAgentKeyMaterialError() { |
| 39 | + #expect(UserAgentKeyMaterialError.invalidPublicKey(underlyingError: Base64URLDecodingError()) == .invalidPublicKey(underlyingError: Base64URLDecodingError())) |
| 40 | + #expect(UserAgentKeyMaterialError.invalidPublicKey(underlyingError: Base64URLDecodingError()).hashValue == UserAgentKeyMaterialError.invalidPublicKey(underlyingError: Base64URLDecodingError()).hashValue) |
| 41 | + #expect(UserAgentKeyMaterialError.invalidPublicKey(underlyingError: Base64URLDecodingError()) != .invalidPublicKey(underlyingError: BadSubscriberError())) |
| 42 | + #expect(UserAgentKeyMaterialError.invalidAuthenticationSecret(underlyingError: Base64URLDecodingError()) == .invalidAuthenticationSecret(underlyingError: Base64URLDecodingError())) |
| 43 | + #expect(UserAgentKeyMaterialError.invalidAuthenticationSecret(underlyingError: Base64URLDecodingError()).hashValue == UserAgentKeyMaterialError.invalidAuthenticationSecret(underlyingError: Base64URLDecodingError()).hashValue) |
| 44 | + #expect(UserAgentKeyMaterialError.invalidAuthenticationSecret(underlyingError: Base64URLDecodingError()) != .invalidAuthenticationSecret(underlyingError: BadSubscriberError())) |
| 45 | + #expect(UserAgentKeyMaterialError.invalidPublicKey(underlyingError: Base64URLDecodingError()) != .invalidAuthenticationSecret(underlyingError: Base64URLDecodingError())) |
| 46 | + |
| 47 | + #expect("\(UserAgentKeyMaterialError.invalidPublicKey(underlyingError: Base64URLDecodingError()).localizedDescription)" == "Subscriber Public Key (`p256dh`) was invalid: The Base64 data could not be decoded.") |
| 48 | + #expect("\(UserAgentKeyMaterialError.invalidAuthenticationSecret(underlyingError: Base64URLDecodingError()).localizedDescription)" == "Subscriber Authentication Secret (`auth`) was invalid: The Base64 data could not be decoded.") |
| 49 | + } |
| 50 | + |
| 51 | + @Test func vapidConfigurationError() { |
| 52 | + #expect(VAPID.ConfigurationError.keysNotProvided == .keysNotProvided) |
| 53 | + #expect(VAPID.ConfigurationError.matchingKeyNotFound == .matchingKeyNotFound) |
| 54 | + #expect(VAPID.ConfigurationError.keysNotProvided != .matchingKeyNotFound) |
| 55 | + #expect("\(VAPID.ConfigurationError.keysNotProvided.localizedDescription)" == "VAPID keys not found during initialization.") |
| 56 | + #expect("\(VAPID.ConfigurationError.matchingKeyNotFound.localizedDescription)" == "A VAPID key for the subscriber was not found.") |
| 57 | + } |
| 58 | +} |
0 commit comments