@@ -8,6 +8,69 @@ class JWTEncodeTests : XCTestCase {
88 let fixture = " eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiS3lsZSJ9.zxm7xcp1eZtZhp4t-nlw09ATQnnFKIiSN83uG8u6cAg "
99 XCTAssertEqual ( jwt, fixture)
1010 }
11+
12+ func testEncodingWithBuilder( ) {
13+ let algorithm = Algorithm . HS256 ( " secret " )
14+ let jwt = JWT . encode ( algorithm) { builder in
15+ builder. issuer = " fuller.li "
16+ }
17+
18+ assertSuccess ( JWT . decode ( jwt, algorithm) ) { payload in
19+ XCTAssertEqual ( payload as NSDictionary , [ " iss " : " fuller.li " ] )
20+ }
21+ }
22+ }
23+
24+ class JWTPayloadBuilder : XCTestCase {
25+ func testIssuer( ) {
26+ JWT . encode ( . None) { builder in
27+ builder. issuer = " fuller.li "
28+ XCTAssertEqual ( builder. issuer!, " fuller.li " )
29+ XCTAssertEqual ( builder [ " iss " ] as String , " fuller.li " )
30+ }
31+ }
32+
33+ func testAudience( ) {
34+ JWT . encode ( . None) { builder in
35+ builder. audience = " cocoapods "
36+ XCTAssertEqual ( builder. audience!, " cocoapods " )
37+ XCTAssertEqual ( builder [ " aud " ] as String , " cocoapods " )
38+ }
39+ }
40+
41+ func testExpiration( ) {
42+ JWT . encode ( . None) { builder in
43+ let date = NSDate ( timeIntervalSince1970: NSDate ( ) . timeIntervalSince1970)
44+ builder. expiration = date
45+ XCTAssertEqual ( builder. expiration!, date)
46+ XCTAssertEqual ( builder [ " exp " ] as NSTimeInterval , date. timeIntervalSince1970)
47+ }
48+ }
49+
50+ func testNotBefore( ) {
51+ JWT . encode ( . None) { builder in
52+ let date = NSDate ( timeIntervalSince1970: NSDate ( ) . timeIntervalSince1970)
53+ builder. notBefore = date
54+ XCTAssertEqual ( builder. notBefore!, date)
55+ XCTAssertEqual ( builder [ " nbf " ] as NSTimeInterval , date. timeIntervalSince1970)
56+ }
57+ }
58+
59+ func testIssuedAt( ) {
60+ JWT . encode ( . None) { builder in
61+ let date = NSDate ( timeIntervalSince1970: NSDate ( ) . timeIntervalSince1970)
62+ builder. issuedAt = date
63+ XCTAssertEqual ( builder. issuedAt!, date)
64+ XCTAssertEqual ( builder [ " iat " ] as NSTimeInterval , date. timeIntervalSince1970)
65+ }
66+ }
67+
68+ func testCustomAttributes( ) {
69+ JWT . encode ( . None) { builder in
70+ builder [ " user " ] = " kyle "
71+ XCTAssertEqual ( builder [ " user " ] as String , " kyle " )
72+ }
73+ }
1174}
1275
1376class JWTDecodeTests : XCTestCase {
0 commit comments