@@ -362,6 +362,46 @@ class ValidationTests: XCTestCase {
362
362
}
363
363
}
364
364
365
+ class IntegrationTests : XCTestCase {
366
+ func testVerificationFailureWithoutLeeway( ) {
367
+ let token = JWT . encode ( . none) { builder in
368
+ builder. issuer = " fuller.li "
369
+ builder. audience = " cocoapods "
370
+ builder. expiration = Date ( ) . addingTimeInterval ( - 1 ) // Token expired one second ago
371
+ builder. notBefore = Date ( ) . addingTimeInterval ( 1 ) // Token starts being valid in one second
372
+ builder. issuedAt = Date ( ) . addingTimeInterval ( 1 ) // Token is issued one second in the future
373
+ }
374
+
375
+ let expectation = XCTestExpectation ( description: " Verification should fail. " )
376
+ do {
377
+ let _ = try JWT . decode ( token, algorithm: . none)
378
+ XCTFail ( " InvalidToken error should have been thrown. " )
379
+ } catch is InvalidToken {
380
+ expectation. fulfill ( )
381
+ } catch {
382
+ XCTFail ( " Unexpected error type while verifying token. " )
383
+ }
384
+ self . wait ( for: [ expectation] , timeout: 0.5 )
385
+ }
386
+
387
+ func testVerificationSuccessWithLeeway( ) {
388
+ let token = JWT . encode ( . none) { builder in
389
+ builder. issuer = " fuller.li "
390
+ builder. audience = " cocoapods "
391
+ builder. expiration = Date ( ) . addingTimeInterval ( - 1 ) // Token expired one second ago
392
+ builder. notBefore = Date ( ) . addingTimeInterval ( 1 ) // Token starts being valid in one second
393
+ builder. issuedAt = Date ( ) . addingTimeInterval ( 1 ) // Token is issued one second in the future
394
+ }
395
+
396
+ do {
397
+ let _ = try JWT . decode ( token, algorithm: . none, leeway: 2 )
398
+ // Due to leeway no error gets thrown.
399
+ } catch {
400
+ XCTFail ( " Unexpected error type while verifying token. " )
401
+ }
402
+ }
403
+ }
404
+
365
405
// MARK: Helpers
366
406
367
407
func assertSuccess( _ decoder: @autoclosure ( ) throws -> Payload , closure: ( ( Payload ) -> Void ) ? = nil ) {
0 commit comments