Skip to content

Commit d21ada4

Browse files
Added sanity test making sure messages are sent out
1 parent e8d0419 commit d21ada4

File tree

4 files changed

+57
-18
lines changed

4 files changed

+57
-18
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ let package = Package(
4444
),
4545
.testTarget(name: "WebPushTests", dependencies: [
4646
.product(name: "AsyncHTTPClient", package: "async-http-client"),
47+
.product(name: "Crypto", package: "swift-crypto"),
4748
.product(name: "Logging", package: "swift-log"),
4849
.product(name: "NIOCore", package: "swift-nio"),
4950
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ targets: [
3737
.target(
3838
name: "MyPackage",
3939
dependencies: [
40-
"WebPush",
40+
.product(name: "WebPush", package: "swift-webpush"),
41+
...
4142
]
4243
),
4344
.testTarget(
4445
name: "MyPackageTests",
4546
dependencies: [
46-
"WebPushTesting",
47+
.product(name: "WebPushTesting", package: "swift-webpush"),
48+
...
4749
]
4850
),
4951
]

Tests/WebPushTests/VAPIDConfiguration+Testing.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Foundation
1010
import WebPush
1111

1212
extension VAPID.Configuration {
13+
/// Make a new configuration useful for testing against.
1314
static func makeTesting() -> VAPID.Configuration {
1415
VAPID.Configuration(
1516
key: VAPID.Key(),

Tests/WebPushTests/WebPushTests.swift

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
// Copyright © 2024 Mochi Development, Inc. All rights reserved.
77
//
88

9+
import AsyncHTTPClient
10+
@preconcurrency import Crypto
911
import Foundation
1012
import Logging
1113
import ServiceLifecycle
@@ -14,27 +16,60 @@ import Testing
1416

1517
@Suite("WebPush Manager")
1618
struct 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

Comments
 (0)