-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added testing coverage for WebPushTesting module
- Loading branch information
1 parent
22d0b12
commit 8be059b
Showing
4 changed files
with
151 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// Subscriber+Testing.swift | ||
// swift-webpush | ||
// | ||
// Created by Dimitri Bouniol on 2024-12-20. | ||
// Copyright © 2024 Mochi Development, Inc. All rights reserved. | ||
// | ||
|
||
@preconcurrency import Crypto | ||
import Foundation | ||
import WebPush | ||
|
||
extension Subscriber { | ||
/// A mocked subscriber to send messages to. | ||
public static let mockedSubscriber = Subscriber( | ||
endpoint: URL(string: "https://example.com/subscriber")!, | ||
userAgentKeyMaterial: .mockedKeyMaterial, | ||
vapidKeyID: .mockedKeyID1 | ||
) | ||
|
||
/// Make a mocked subscriber with a unique private key and salt. | ||
static func makeMockedSubscriber(endpoint: URL = URL(string: "https://example.com/subscriber")!) -> (subscriber: Subscriber, privateKey: P256.KeyAgreement.PrivateKey) { | ||
let subscriberPrivateKey = P256.KeyAgreement.PrivateKey(compactRepresentable: false) | ||
var authenticationSecret: [UInt8] = Array(repeating: 0, count: 16) | ||
for index in authenticationSecret.indices { authenticationSecret[index] = .random(in: .min ... .max) } | ||
|
||
let subscriber = Subscriber( | ||
endpoint: endpoint, | ||
userAgentKeyMaterial: UserAgentKeyMaterial(publicKey: subscriberPrivateKey.publicKey, authenticationSecret: Data(authenticationSecret)), | ||
vapidKeyID: .mockedKeyID1 | ||
) | ||
|
||
return (subscriber, subscriberPrivateKey) | ||
} | ||
} | ||
|
||
extension SubscriberProtocol where Self == Subscriber { | ||
/// A mocked subscriber to send messages to. | ||
public static func mockedSubscriber() -> Subscriber { | ||
.mockedSubscriber | ||
} | ||
} | ||
|
||
extension UserAgentKeyMaterial { | ||
/// The private key component of ``mockedKeyMaterial``. | ||
public static let mockedKeyMaterialPrivateKey = try! P256.KeyAgreement.PrivateKey(rawRepresentation: Data(base64Encoded: "BS2nTTf5wAdVvi5Om3AjSmlsCpz91XgK+uCLaIJ0T/M=")!) | ||
|
||
/// A mocked user-agent-key material to attach to a subscriber. | ||
public static let mockedKeyMaterial = try! UserAgentKeyMaterial( | ||
publicKey: "BMXVxJELqTqIqMka5N8ujvW6RXI9zo_xr5BQ6XGDkrsukNVPyKRMEEfzvQGeUdeZaWAaAs2pzyv1aoHEXYMtj1M", | ||
authenticationSecret: "IzODAQZN6BbGvmm7vWQJXg" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters