1
1
import Foundation
2
2
import XCTest
3
- import JWT
3
+ @ testable import JWT
4
4
5
5
class DecodeTests : XCTestCase {
6
6
func testDecodingValidJWTAsClaimSet( ) throws {
@@ -93,7 +93,7 @@ class DecodeTests: XCTestCase {
93
93
94
94
func testInvalidNotBeforeClaim( ) {
95
95
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 " )
97
97
}
98
98
99
99
func testUnmetNotBeforeClaim( ) {
@@ -134,15 +134,15 @@ class DecodeTests: XCTestCase {
134
134
135
135
func testAudiencesClaim( ) {
136
136
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
138
138
XCTAssertEqual ( payload. count, 1 )
139
139
XCTAssertEqual ( payload [ " aud " ] as! [ String ] , [ " maxine " , " katie " ] )
140
140
}
141
141
}
142
142
143
143
func testAudienceClaim( ) {
144
144
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
146
146
XCTAssertEqual ( payload as! [ String : String ] , [ " aud " : " kyle " ] )
147
147
}
148
148
}
@@ -161,7 +161,7 @@ class DecodeTests: XCTestCase {
161
161
162
162
func testNoneAlgorithm( ) {
163
163
let jwt = " eyJhbGciOiJub25lIiwidHlwIjoiSldUIn0.eyJ0ZXN0IjoiaW5nIn0. "
164
- assertSuccess ( try decode ( jwt, algorithm: . none) ) { payload in
164
+ assertSuccess ( try decode ( jwt, algorithm: . none) as ClaimSet ) { payload in
165
165
XCTAssertEqual ( payload as! [ String : String ] , [ " test " : " ing " ] )
166
166
}
167
167
}
@@ -173,36 +173,36 @@ class DecodeTests: XCTestCase {
173
173
174
174
func testMatchesAnyAlgorithm( ) {
175
175
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 )
177
177
}
178
178
179
179
func testHS384Algorithm( ) {
180
180
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
182
182
XCTAssertEqual ( payload as! [ String : String ] , [ " some " : " payload " ] )
183
183
}
184
184
}
185
185
186
186
func testHS512Algorithm( ) {
187
187
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 " ] )
190
190
}
191
191
}
192
192
}
193
193
194
194
// MARK: Helpers
195
195
196
- func assertSuccess( _ decoder: @autoclosure ( ) throws -> Payload , closure: ( ( Payload ) -> Void ) ? = nil ) {
196
+ func assertSuccess( _ decoder: @autoclosure ( ) throws -> ClaimSet , closure: ( ( [ String : Any ] ) -> Void ) ? = nil ) {
197
197
do {
198
- let payload = try decoder ( )
199
- closure ? ( payload )
198
+ let claims = try decoder ( )
199
+ closure ? ( claims . claims as [ String : Any ] )
200
200
} catch {
201
201
XCTFail ( " Failed to decode while expecting success. \( error) " )
202
202
}
203
203
}
204
204
205
- func assertFailure( _ decoder: @autoclosure ( ) throws -> Payload , closure: ( ( InvalidToken ) -> Void ) ? = nil ) {
205
+ func assertFailure( _ decoder: @autoclosure ( ) throws -> ClaimSet , closure: ( ( InvalidToken ) -> Void ) ? = nil ) {
206
206
do {
207
207
_ = try decoder ( )
208
208
XCTFail ( " Decoding succeeded, expected a failure. " )
@@ -213,7 +213,7 @@ func assertFailure(_ decoder: @autoclosure () throws -> Payload, closure: ((Inva
213
213
}
214
214
}
215
215
216
- func assertDecodeError( _ decoder: @autoclosure ( ) throws -> Payload , error: String ) {
216
+ func assertDecodeError( _ decoder: @autoclosure ( ) throws -> ClaimSet , error: String ) {
217
217
assertFailure ( try decoder ( ) ) { failure in
218
218
switch failure {
219
219
case . decodeError( let decodeError) :
0 commit comments