Skip to content

Commit

Permalink
Added mock-ready WebPushTesting library to the package to help adopte…
Browse files Browse the repository at this point in the history
…rs test with the library
  • Loading branch information
dimitribouniol committed Dec 12, 2024
1 parent be3909c commit bfb4fa4
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 62 deletions.
10 changes: 10 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ let package = Package(
],
products: [
.library(name: "WebPush", targets: ["WebPush"]),
.library(name: "WebPushTesting", targets: ["WebPush", "WebPushTesting"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-crypto.git", "3.10.0"..<"5.0.0"),
Expand All @@ -33,12 +34,21 @@ let package = Package(
.product(name: "NIOHTTP1", package: "swift-nio"),
]
),
.target(
name: "WebPushTesting",
dependencies: [
.product(name: "Crypto", package: "swift-crypto"),
.product(name: "Logging", package: "swift-log"),
.target(name: "WebPush"),
]
),
.testTarget(name: "WebPushTests", dependencies: [
.product(name: "AsyncHTTPClient", package: "async-http-client"),
.product(name: "Logging", package: "swift-log"),
.product(name: "NIOCore", package: "swift-nio"),
.product(name: "ServiceLifecycle", package: "swift-service-lifecycle"),
.target(name: "WebPush"),
.target(name: "WebPushTesting"),
]),
]
)
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# WebPush

<p align="center">
<a href="https://swiftpackageindex.com/mochidev/WebPush">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2FWebPush%2Fbadge%3Ftype%3Dswift-versions" />
<a href="https://swiftpackageindex.com/mochidev/swift-webpush">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2Fswift-webpush%2Fbadge%3Ftype%3Dswift-versions" />
</a>
<a href="https://swiftpackageindex.com/mochidev/WebPush">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2FWebPush%2Fbadge%3Ftype%3Dplatforms" />
<a href="https://swiftpackageindex.com/mochidev/swift-webpush">
<img src="https://img.shields.io/endpoint?url=https%3A%2F%2Fswiftpackageindex.com%2Fapi%2Fpackages%2Fmochidev%2Fswift-webpush%2Fbadge%3Ftype%3Dplatforms" />
</a>
<a href="https://github.com/mochidev/WebPush/actions?query=workflow%3A%22Test+WebPush%22">
<img src="https://github.com/mochidev/WebPush/workflows/Test%20WebPush/badge.svg" alt="Test Status" />
Expand Down Expand Up @@ -40,6 +40,9 @@ targets: [
"WebPush",
]
)
.testTarget(name: "MyPackageTests", dependencies: [
"WebPushTesting",
]
]
```

Expand Down
2 changes: 1 addition & 1 deletion Sources/WebPush/Helpers/HTTPClientProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import AsyncHTTPClient
import Logging
import NIOCore

protocol HTTPClientProtocol: Sendable {
package protocol HTTPClientProtocol: Sendable {
func execute(
_ request: HTTPClientRequest,
deadline: NIODeadline,
Expand Down
16 changes: 8 additions & 8 deletions Sources/WebPush/Helpers/PrintLogHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import Foundation
import Logging

struct PrintLogHandler: LogHandler {
package struct PrintLogHandler: LogHandler {
private let label: String

var logLevel: Logger.Level = .info
var metadataProvider: Logger.MetadataProvider?
package var logLevel: Logger.Level = .info
package var metadataProvider: Logger.MetadataProvider?

init(
package init(
label: String,
logLevel: Logger.Level = .info,
metadataProvider: Logger.MetadataProvider? = nil
Expand All @@ -26,13 +26,13 @@ struct PrintLogHandler: LogHandler {
}

private var prettyMetadata: String?
var metadata = Logger.Metadata() {
package var metadata = Logger.Metadata() {
didSet {
self.prettyMetadata = self.prettify(self.metadata)
}
}

subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
package subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
self.metadata[metadataKey]
}
Expand All @@ -41,7 +41,7 @@ struct PrintLogHandler: LogHandler {
}
}

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

internal static func prepareMetadata(
private static func prepareMetadata(
base: Logger.Metadata,
provider: Logger.MetadataProvider?,
explicit: Logger.Metadata?
Expand Down
9 changes: 9 additions & 0 deletions Sources/WebPush/Subscriber.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ public struct Subscriber: SubscriberProtocol, Codable, Hashable, Sendable {
self.userAgentKeyMaterial = userAgentKeyMaterial
self.vapidKeyID = vapidKeyID
}

/// Cast an object that conforms to ``SubscriberProtocol`` to a ``Subscriber``.
public init(_ subscriber: some SubscriberProtocol) {
self.init(
endpoint: subscriber.endpoint,
userAgentKeyMaterial: subscriber.userAgentKeyMaterial,
vapidKeyID: subscriber.vapidKeyID
)
}
}

extension Subscriber: Identifiable {
Expand Down
Loading

0 comments on commit bfb4fa4

Please sign in to comment.