66// Copyright © 2024 Mochi Development, Inc. All rights reserved.
77//
88
9+ import AsyncHTTPClient
10+ @preconcurrency import Crypto
911import Foundation
1012import Logging
1113import ServiceLifecycle
@@ -14,27 +16,60 @@ import Testing
1416
1517@Suite ( " WebPush Manager " )
1618struct WebPushManagerTests {
17- @Test func webPushManagerInitializesOnItsOwn( ) async throws {
18- let manager = WebPushManager ( vapidConfiguration: . makeTesting( ) )
19- await withThrowingTaskGroup ( of: Void . self) { group in
20- group. addTask {
21- try await manager. run ( )
19+ @Suite
20+ struct Initialization {
21+ @Test func managerInitializesOnItsOwn( ) async throws {
22+ let manager = WebPushManager ( vapidConfiguration: . makeTesting( ) )
23+ await withThrowingTaskGroup ( of: Void . self) { group in
24+ group. addTask {
25+ try await manager. run ( )
26+ }
27+ group. cancelAll ( )
28+ }
29+ }
30+
31+ @Test func managerInitializesAsService( ) async throws {
32+ let logger = Logger ( label: " ServiceLogger " , factory: { PrintLogHandler ( label: $0, metadataProvider: $1) } )
33+ let manager = WebPushManager (
34+ vapidConfiguration: . makeTesting( ) ,
35+ logger: logger
36+ )
37+ await withThrowingTaskGroup ( of: Void . self) { group in
38+ group. addTask {
39+ try await ServiceGroup ( services: [ manager] , logger: logger) . run ( )
40+ }
41+ group. cancelAll ( )
2242 }
23- group. cancelAll ( )
2443 }
2544 }
2645
27- @Test func webPushManagerInitializesAsService( ) async throws {
28- let logger = Logger ( label: " ServiceLogger " , factory: { PrintLogHandler ( label: $0, metadataProvider: $1) } )
29- let manager = WebPushManager (
30- vapidConfiguration: . makeTesting( ) ,
31- logger: logger
32- )
33- await withThrowingTaskGroup ( of: Void . self) { group in
34- group. addTask {
35- try await ServiceGroup ( services: [ manager] , logger: logger) . run ( )
46+ @Suite ( " Sending Messages " )
47+ struct SendingMessages {
48+ @Test func sendSuccessfulTextMessage( ) async throws {
49+ try await confirmation { requestWasMade in
50+ let vapidConfiguration = VAPID . Configuration. makeTesting ( )
51+
52+ let subscriberPrivateKey = P256 . KeyAgreement. PrivateKey ( compactRepresentable: false )
53+ var authenticationSecret : [ UInt8 ] = Array ( repeating: 0 , count: 16 )
54+ for index in authenticationSecret. indices { authenticationSecret [ index] = . random( in: . min ... . max) }
55+
56+ let subscriber = Subscriber (
57+ endpoint: URL ( string: " https://example.com/subscriber " ) !,
58+ userAgentKeyMaterial: UserAgentKeyMaterial ( publicKey: subscriberPrivateKey. publicKey, authenticationSecret: Data ( authenticationSecret) ) ,
59+ vapidKeyID: vapidConfiguration. primaryKey!. id
60+ )
61+
62+ let manager = WebPushManager (
63+ vapidConfiguration: vapidConfiguration,
64+ logger: Logger ( label: " WebPushManagerTests " , factory: { PrintLogHandler ( label: $0, metadataProvider: $1) } ) ,
65+ executor: . httpClient( MockHTTPClient ( { request in
66+ requestWasMade ( )
67+ return HTTPClientResponse ( status: . created)
68+ } ) )
69+ )
70+
71+ try await manager. send ( string: " hello " , to: subscriber)
3672 }
37- group. cancelAll ( )
3873 }
3974 }
4075
0 commit comments