11import Foundation
22import XCTest
3- import JWT
3+ @ testable import JWT
44
55class DecodeTests : XCTestCase {
66 func testDecodingValidJWTAsClaimSet( ) throws {
@@ -93,7 +93,7 @@ class DecodeTests: XCTestCase {
9393
9494 func testInvalidNotBeforeClaim( ) {
9595 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOlsxNDI4MTg5NzIwXX0.PUL1FQubzzJa4MNXe2D3d5t5cMaqFr3kYlzRUzly-C8 "
96- assertDecodeError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) , error: " Not before claim (nbf) must be an integer " )
96+ assertDecodeError ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) ) as ClaimSet , error: " Not before claim (nbf) must be an integer " )
9797 }
9898
9999 func testUnmetNotBeforeClaim( ) {
@@ -134,15 +134,15 @@ class DecodeTests: XCTestCase {
134134
135135 func testAudiencesClaim( ) {
136136 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsibWF4aW5lIiwia2F0aWUiXX0.-PKvdNLCClrWG7CvesHP6PB0-vxu-_IZcsYhJxBy5JM "
137- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " maxine " ) ) { payload in
137+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " maxine " ) as ClaimSet ) { payload in
138138 XCTAssertEqual ( payload. count, 1 )
139139 XCTAssertEqual ( payload [ " aud " ] as! [ String ] , [ " maxine " , " katie " ] )
140140 }
141141 }
142142
143143 func testAudienceClaim( ) {
144144 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJreWxlIn0.dpgH4JOwueReaBoanLSxsGTc7AjKUvo7_M1sAfy_xVE "
145- assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " kyle " ) ) { payload in
145+ assertSuccess ( try decode ( jwt, algorithm: . hs256( " secret " . data ( using: . utf8) !) , audience: " kyle " ) as ClaimSet ) { payload in
146146 XCTAssertEqual ( payload as! [ String : String ] , [ " aud " : " kyle " ] )
147147 }
148148 }
@@ -161,7 +161,7 @@ class DecodeTests: XCTestCase {
161161
162162 func testNoneAlgorithm( ) {
163163 let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
164- assertSuccess ( try decode ( jwt, algorithm: . none) ) { payload in
164+ assertSuccess ( try decode ( jwt, algorithm: . none) as ClaimSet ) { payload in
165165 XCTAssertEqual ( payload as! [ String : String ] , [ " test " : " ing " ] )
166166 }
167167 }
@@ -173,36 +173,36 @@ class DecodeTests: XCTestCase {
173173
174174 func testMatchesAnyAlgorithm( ) {
175175 let jwt = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.2_8pWJfyPup0YwOXK7g9Dn0cF1E3pdn299t4hSeJy5w. "
176- assertFailure ( try decode ( jwt, algorithms: [ . hs256( " anothersecret " . data ( using: . utf8) !) , . hs256( " secret " . data ( using: . utf8) !) ] ) )
176+ assertFailure ( try decode ( jwt, algorithms: [ . hs256( " anothersecret " . data ( using: . utf8) !) , . hs256( " secret " . data ( using: . utf8) !) ] ) as ClaimSet )
177177 }
178178
179179 func testHS384Algorithm( ) {
180180 let jwt = " eyJhbGciOiJIUzM4NCIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.lddiriKLoo42qXduMhCTKZ5Lo3njXxOC92uXyvbLyYKzbq4CVVQOb3MpDwnI19u4 "
181- assertSuccess ( try decode ( jwt, algorithm: . hs384( " secret " . data ( using: . utf8) !) ) ) { payload in
181+ assertSuccess ( try decode ( jwt, algorithm: . hs384( " secret " . data ( using: . utf8) !) ) as ClaimSet ) { payload in
182182 XCTAssertEqual ( payload as! [ String : String ] , [ " some " : " payload " ] )
183183 }
184184 }
185185
186186 func testHS512Algorithm( ) {
187187 let jwt = " eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJzb21lIjoicGF5bG9hZCJ9.WTzLzFO079PduJiFIyzrOah54YaM8qoxH9fLMQoQhKtw3_fMGjImIOokijDkXVbyfBqhMo2GCNu4w9v7UXvnpA "
188- assertSuccess ( try decode ( jwt, algorithm: . hs512( " secret " . data ( using: . utf8) !) ) ) { payload in
189- XCTAssertEqual ( payload as! [ String : String ] , [ " some " : " payload " ] )
188+ assertSuccess ( try decode ( jwt, algorithm: . hs512( " secret " . data ( using: . utf8) !) ) as ClaimSet ) { claims in
189+ XCTAssertEqual ( claims as! [ String : String ] , [ " some " : " payload " ] )
190190 }
191191 }
192192}
193193
194194// MARK: Helpers
195195
196- func assertSuccess( _ decoder: @autoclosure ( ) throws -> Payload , closure: ( ( Payload ) -> Void ) ? = nil ) {
196+ func assertSuccess( _ decoder: @autoclosure ( ) throws -> ClaimSet , closure: ( ( [ String : Any ] ) -> Void ) ? = nil ) {
197197 do {
198- let payload = try decoder ( )
199- closure ? ( payload )
198+ let claims = try decoder ( )
199+ closure ? ( claims . claims as [ String : Any ] )
200200 } catch {
201201 XCTFail ( " Failed to decode while expecting success. \( error) " )
202202 }
203203}
204204
205- func assertFailure( _ decoder: @autoclosure ( ) throws -> Payload , closure: ( ( InvalidToken ) -> Void ) ? = nil ) {
205+ func assertFailure( _ decoder: @autoclosure ( ) throws -> ClaimSet , closure: ( ( InvalidToken ) -> Void ) ? = nil ) {
206206 do {
207207 _ = try decoder ( )
208208 XCTFail ( " Decoding succeeded, expected a failure. " )
@@ -213,7 +213,7 @@ func assertFailure(_ decoder: @autoclosure () throws -> Payload, closure: ((Inva
213213 }
214214}
215215
216- func assertDecodeError( _ decoder: @autoclosure ( ) throws -> Payload , error: String ) {
216+ func assertDecodeError( _ decoder: @autoclosure ( ) throws -> ClaimSet , error: String ) {
217217 assertFailure ( try decoder ( ) ) { failure in
218218 switch failure {
219219 case . decodeError( let decodeError) :
0 commit comments