@@ -53,6 +53,16 @@ struct WebPushManagerTests {
5353 }
5454 }
5555
56+ @Test func managerUsesDefaultLogging( ) async throws {
57+ let manager = WebPushManager ( vapidConfiguration: . makeTesting( ) )
58+ #expect( manager. backgroundActivityLogger. handler is PrintLogHandler )
59+ }
60+
61+ @Test func managerCanSupressLogging( ) async throws {
62+ let manager = WebPushManager ( vapidConfiguration: . makeTesting( ) , backgroundActivityLogger: nil )
63+ #expect( manager. backgroundActivityLogger. handler is SwiftLogNoOpLogHandler )
64+ }
65+
5666 @Test func managerCanBeMocked( ) async throws {
5767 let manager = WebPushManager . makeMockedManager { _, _, _, _ in }
5868 await withThrowingTaskGroup ( of: Void . self) { group in
@@ -63,6 +73,16 @@ struct WebPushManagerTests {
6373 }
6474 }
6575
76+ @Test func mockedManagerUsesDefaultLogging( ) async throws {
77+ let manager = WebPushManager . makeMockedManager ( messageHandler: { _, _, _, _ in } )
78+ #expect( manager. backgroundActivityLogger. handler is PrintLogHandler )
79+ }
80+
81+ @Test func mockedManagerCanSupressLogging( ) async throws {
82+ let manager = WebPushManager . makeMockedManager ( backgroundActivityLogger: nil , messageHandler: { _, _, _, _ in } )
83+ #expect( manager. backgroundActivityLogger. handler is SwiftLogNoOpLogHandler )
84+ }
85+
6686 /// Enable when https://github.com/swiftlang/swift-testing/blob/jgrynspan/exit-tests-proposal/Documentation/Proposals/NNNN-exit-tests.md gets accepted.
6787// @Test func managerCatchesIncorrectValidity() async throws {
6888// await #expect(exitsWith: .failure) {
@@ -344,6 +364,23 @@ struct WebPushManagerTests {
344364 }
345365 }
346366
367+ @Test func sendSuccessfulMultipleMessages( ) async throws {
368+ try await confirmation ( expectedCount: 3 ) { requestWasMade in
369+ let manager = WebPushManager (
370+ vapidConfiguration: . mockedConfiguration,
371+ backgroundActivityLogger: Logger ( label: " WebPushManagerTests " , factory: { PrintLogHandler ( label: $0, metadataProvider: $1) } ) ,
372+ executor: . httpClient( MockHTTPClient ( { request in
373+ requestWasMade ( )
374+ return HTTPClientResponse ( status: . created)
375+ } ) )
376+ )
377+
378+ try await manager. send ( string: " hello, world! " , to: . mockedSubscriber( ) )
379+ try await manager. send ( data: [ 1 , 2 , 3 ] , to: . mockedSubscriber( ) )
380+ try await manager. send ( json: [ " hello " : " world " ] , to: . mockedSubscriber( ) )
381+ }
382+ }
383+
347384 @Test func sendMessageToSubscriberWithInvalidVAPIDKey( ) async throws {
348385 await confirmation ( expectedCount: 0 ) { requestWasMade in
349386 var subscriber = Subscriber . mockedSubscriber
@@ -536,7 +573,10 @@ struct WebPushManagerTests {
536573 @Test func sendSuccessfulTextMessage( ) async throws {
537574 try await confirmation { requestWasMade in
538575 let manager = WebPushManager . makeMockedManager ( backgroundActivityLogger: Logger ( label: " WebPushManagerTests " , factory: { PrintLogHandler ( label: $0, metadataProvider: $1) } ) ) { message, subscriber, expiration, urgency in
576+ #expect( message. description == " .string(hello) " )
539577 #expect( message. string == " hello " )
578+ try #expect( message. data == Data ( " hello " . utf8) )
579+ #expect( message. json ( as: [ String : String ] . self) == nil )
540580 #expect( subscriber. endpoint. absoluteString == " https://example.com/subscriber " )
541581 #expect( subscriber. vapidKeyID == . mockedKeyID1)
542582 #expect( expiration == . recommendedMaximum)
@@ -551,7 +591,10 @@ struct WebPushManagerTests {
551591 @Test func sendSuccessfulDataMessage( ) async throws {
552592 try await confirmation { requestWasMade in
553593 let manager = WebPushManager . makeMockedManager ( backgroundActivityLogger: Logger ( label: " WebPushManagerTests " , factory: { PrintLogHandler ( label: $0, metadataProvider: $1) } ) ) { message, subscriber, expiration, urgency in
594+ #expect( message. description == " .data(aGVsbG8) " )
554595 try #expect( message. data == Data ( " hello " . utf8Bytes) )
596+ #expect( message. string == nil )
597+ #expect( message. json ( as: [ String : String ] . self) == nil )
555598 #expect( subscriber. endpoint. absoluteString == " https://example.com/subscriber " )
556599 #expect( subscriber. vapidKeyID == . mockedKeyID1)
557600 #expect( expiration == . recommendedMaximum)
@@ -566,7 +609,11 @@ struct WebPushManagerTests {
566609 @Test func sendSuccessfulJSONMessage( ) async throws {
567610 try await confirmation { requestWasMade in
568611 let manager = WebPushManager . makeMockedManager ( backgroundActivityLogger: Logger ( label: " WebPushManagerTests " , factory: { PrintLogHandler ( label: $0, metadataProvider: $1) } ) ) { message, subscriber, expiration, urgency in
612+ #expect( message. description == " .json([ \" hello \" : \" world \" ]) " )
569613 #expect( message. json ( ) == [ " hello " : " world " ] )
614+ #expect( message. string == nil )
615+ try #expect( message. data == Data ( " { \" hello \" : \" world \" } " . utf8) )
616+ #expect( message. json ( as: [ String ] . self) == nil )
570617 #expect( subscriber. endpoint. absoluteString == " https://example.com/subscriber " )
571618 #expect( subscriber. vapidKeyID == . mockedKeyID1)
572619 #expect( expiration == . recommendedMaximum)
0 commit comments