@@ -10,6 +10,72 @@ import (
1010 "github.com/cloudentity/oauth2c/internal/oauth2"
1111)
1212
13+ func TestTokenResponseExtraFields (t * testing.T ) {
14+ body := []byte (`{
15+ "access_token": "token",
16+ "expires_in": 3600,
17+ "token_type": "Bearer",
18+ "email": "me@email.com",
19+ "environment_id": "envid-token-here",
20+ "legal_entity_name": "oauth environment provider"
21+ }` )
22+
23+ var resp oauth2.TokenResponse
24+ require .NoError (t , json .Unmarshal (body , & resp ))
25+
26+ require .Equal (t , "token" , resp .AccessToken )
27+ require .Equal (t , oauth2 .FlexibleInt64 (3600 ), resp .ExpiresIn )
28+ require .Equal (t , "Bearer" , resp .TokenType )
29+
30+ out , err := json .Marshal (resp )
31+ require .NoError (t , err )
32+
33+ var roundTrip map [string ]any
34+ require .NoError (t , json .Unmarshal (out , & roundTrip ))
35+
36+ require .Equal (t , "token" , roundTrip ["access_token" ])
37+ require .Equal (t , "Bearer" , roundTrip ["token_type" ])
38+ require .Equal (t , "me@email.com" , roundTrip ["email" ])
39+ require .Equal (t , "envid-token-here" , roundTrip ["environment_id" ])
40+ require .Equal (t , "oauth environment provider" , roundTrip ["legal_entity_name" ])
41+ require .NotContains (t , roundTrip , "raw" )
42+ }
43+
44+ func TestTokenResponseNoExtraFields (t * testing.T ) {
45+ body := []byte (`{"access_token": "token", "expires_in": 3600, "token_type": "Bearer"}` )
46+
47+ var resp oauth2.TokenResponse
48+ require .NoError (t , json .Unmarshal (body , & resp ))
49+
50+ out , err := json .Marshal (resp )
51+ require .NoError (t , err )
52+
53+ var roundTrip map [string ]any
54+ require .NoError (t , json .Unmarshal (out , & roundTrip ))
55+
56+ require .Equal (t , "token" , roundTrip ["access_token" ])
57+ require .Equal (t , "Bearer" , roundTrip ["token_type" ])
58+ require .Len (t , roundTrip , 3 )
59+ }
60+
61+ func TestTokenResponseTypedFieldsWinOnConflict (t * testing.T ) {
62+ body := []byte (`{"access_token": "from-server", "expires_in": "3600"}` )
63+
64+ var resp oauth2.TokenResponse
65+ require .NoError (t , json .Unmarshal (body , & resp ))
66+
67+ resp .AccessToken = "overridden"
68+
69+ out , err := json .Marshal (resp )
70+ require .NoError (t , err )
71+
72+ var roundTrip map [string ]any
73+ require .NoError (t , json .Unmarshal (out , & roundTrip ))
74+
75+ require .Equal (t , "overridden" , roundTrip ["access_token" ])
76+ require .EqualValues (t , 3600 , roundTrip ["expires_in" ])
77+ }
78+
1379func TestUnmarshalExpires (t * testing.T ) {
1480 tests := map [string ]struct {
1581 bytes []byte
0 commit comments