Skip to content

Commit bfb4fa4

Browse files
Added mock-ready WebPushTesting library to the package to help adopters test with the library
1 parent be3909c commit bfb4fa4

File tree

8 files changed

+317
-62
lines changed

8 files changed

+317
-62
lines changed

Package.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ let package = Package(
1313
],
1414
products: [
1515
.library(name: "WebPush", targets: ["WebPush"]),
16+
.library(name: "WebPushTesting", targets: ["WebPush", "WebPushTesting"]),
1617
],
1718
dependencies: [
1819
.package(url: "https://github.com/apple/swift-crypto.git", "3.10.0"..<"5.0.0"),
@@ -33,12 +34,21 @@ let package = Package(
3334
.product(name: "NIOHTTP1", package: "swift-nio"),
3435
]
3536
),
37+
.target(
38+
name: "WebPushTesting",
39+
dependencies: [
40+
.product(name: "Crypto", package: "swift-crypto"),
41+
.product(name: "Logging", package: "swift-log"),
42+
.target(name: "WebPush"),
43+
]
44+
),
3645
.testTarget(name: "WebPushTests", dependencies: [
3746
.product(name: "AsyncHTTPClient", package: "async-http-client"),
3847
.product(name: "Logging", package: "swift-log"),
3948
.product(name: "NIOCore", package: "swift-nio"),
4049
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
4150
.target(name: "WebPush"),
51+
.target(name: "WebPushTesting"),
4252
]),
4353
]
4454
)

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# WebPush
22

33
<p align="center">
4-
<a href="https://swiftpackageindex.com/mochidev/WebPush">
5-
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2FWebPush%2Fbadge%3Ftype%3Dswift-versions" />
4+
<a href="https://swiftpackageindex.com/mochidev/swift-webpush">
5+
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2Fswift-webpush%2Fbadge%3Ftype%3Dswift-versions" />
66
</a>
7-
<a href="https://swiftpackageindex.com/mochidev/WebPush">
8-
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2FWebPush%2Fbadge%3Ftype%3Dplatforms" />
7+
<a href="https://swiftpackageindex.com/mochidev/swift-webpush">
8+
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2Fswift-webpush%2Fbadge%3Ftype%3Dplatforms" />
99
</a>
1010
<a href="https://github.com/mochidev/WebPush/actions?query=workflow%3A%22Test+WebPush%22">
1111
<img src="https://github.com/mochidev/WebPush/workflows/Test%20WebPush/badge.svg" alt="Test Status" />
@@ -40,6 +40,9 @@ targets: [
4040
"WebPush",
4141
]
4242
)
43+
.testTarget(name: "MyPackageTests", dependencies: [
44+
"WebPushTesting",
45+
]
4346
]
4447
```
4548

Sources/WebPush/Helpers/HTTPClientProtocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import AsyncHTTPClient
1010
import Logging
1111
import NIOCore
1212

13-
protocol HTTPClientProtocol: Sendable {
13+
package protocol HTTPClientProtocol: Sendable {
1414
func execute(
1515
_ request: HTTPClientRequest,
1616
deadline: NIODeadline,

Sources/WebPush/Helpers/PrintLogHandler.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
import Foundation
1010
import Logging
1111

12-
struct PrintLogHandler: LogHandler {
12+
package struct PrintLogHandler: LogHandler {
1313
private let label: String
1414

15-
var logLevel: Logger.Level = .info
16-
var metadataProvider: Logger.MetadataProvider?
15+
package var logLevel: Logger.Level = .info
16+
package var metadataProvider: Logger.MetadataProvider?
1717

18-
init(
18+
package init(
1919
label: String,
2020
logLevel: Logger.Level = .info,
2121
metadataProvider: Logger.MetadataProvider? = nil
@@ -26,13 +26,13 @@ struct PrintLogHandler: LogHandler {
2626
}
2727

2828
private var prettyMetadata: String?
29-
var metadata = Logger.Metadata() {
29+
package var metadata = Logger.Metadata() {
3030
didSet {
3131
self.prettyMetadata = self.prettify(self.metadata)
3232
}
3333
}
3434

35-
subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
35+
package subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
3636
get {
3737
self.metadata[metadataKey]
3838
}
@@ -41,7 +41,7 @@ struct PrintLogHandler: LogHandler {
4141
}
4242
}
4343

44-
func log(
44+
package func log(
4545
level: Logger.Level,
4646
message: Logger.Message,
4747
metadata explicitMetadata: Logger.Metadata?,
@@ -66,7 +66,7 @@ struct PrintLogHandler: LogHandler {
6666
print("\(self.timestamp()) [\(level)] \(self.label):\(prettyMetadata.map { " \($0)" } ?? "") [\(source)] \(message)")
6767
}
6868

69-
internal static func prepareMetadata(
69+
private static func prepareMetadata(
7070
base: Logger.Metadata,
7171
provider: Logger.MetadataProvider?,
7272
explicit: Logger.Metadata?

Sources/WebPush/Subscriber.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ public struct Subscriber: SubscriberProtocol, Codable, Hashable, Sendable {
178178
self.userAgentKeyMaterial = userAgentKeyMaterial
179179
self.vapidKeyID = vapidKeyID
180180
}
181+
182+
/// Cast an object that conforms to ``SubscriberProtocol`` to a ``Subscriber``.
183+
public init(_ subscriber: some SubscriberProtocol) {
184+
self.init(
185+
endpoint: subscriber.endpoint,
186+
userAgentKeyMaterial: subscriber.userAgentKeyMaterial,
187+
vapidKeyID: subscriber.vapidKeyID
188+
)
189+
}
181190
}
182191

183192
extension Subscriber: Identifiable {

0 commit comments

Comments
 (0)