@@ -3,6 +3,7 @@ import { exchangeAuthCode } from ".";
3
3
import { MemoryStorage , StorageKeys } from "../sessionManager" ;
4
4
import { setActiveStorage } from "./token" ;
5
5
import createFetchMock from "vitest-fetch-mock" ;
6
+ import { frameworkSettings } from "./exchangeAuthCode" ;
6
7
7
8
const fetchMock = createFetchMock ( vi ) ;
8
9
@@ -89,7 +90,7 @@ describe("exhangeAuthCode", () => {
89
90
} ) ;
90
91
} ) ;
91
92
92
- it ( "should encode a simple string " , async ( ) => {
93
+ it ( "should exchange tokens, set storage and clear temp values " , async ( ) => {
93
94
const store = new MemoryStorage ( ) ;
94
95
setActiveStorage ( store ) ;
95
96
@@ -133,5 +134,62 @@ describe("exhangeAuthCode", () => {
133
134
StorageKeys . codeVerifier ,
134
135
) ;
135
136
expect ( postCodeVerifier ) . toBeNull ( ) ;
137
+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
138
+ const [ url , options ] = fetchMock . mock . calls [ 0 ] ;
139
+ expect ( url ) . toBe ( "http://test.kinde.com/oauth2/token" ) ;
140
+ expect ( options ) . toMatchObject ( {
141
+ method : "POST" ,
142
+ headers : {
143
+ "Content-type" : "application/x-www-form-urlencoded; charset=UTF-8" ,
144
+ } ,
145
+ } ) ;
146
+ } ) ;
147
+
148
+ it ( "set the framework and version on header" , async ( ) => {
149
+ const store = new MemoryStorage ( ) ;
150
+ setActiveStorage ( store ) ;
151
+
152
+ const state = "state" ;
153
+
154
+ await store . setItems ( {
155
+ [ StorageKeys . state ] : state ,
156
+ } ) ;
157
+
158
+ frameworkSettings . framework = "Framework" ;
159
+ frameworkSettings . frameworkVersion = "Version" ;
160
+
161
+ const input = "hello" ;
162
+
163
+ const urlParams = new URLSearchParams ( ) ;
164
+ urlParams . append ( "code" , input ) ;
165
+ urlParams . append ( "state" , state ) ;
166
+ urlParams . append ( "client_id" , "test" ) ;
167
+
168
+ fetchMock . mockResponseOnce (
169
+ JSON . stringify ( {
170
+ access_token : "access_token" ,
171
+ refresh_token : "refresh_token" ,
172
+ id_token : "id_token" ,
173
+ } ) ,
174
+ ) ;
175
+
176
+ await exchangeAuthCode ( {
177
+ urlParams,
178
+ domain : "http://test.kinde.com" ,
179
+ clientId : "test" ,
180
+ redirectURL : "http://test.kinde.com" ,
181
+ } ) ;
182
+
183
+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
184
+ const [ url , options ] = fetchMock . mock . calls [ 0 ] ;
185
+ expect ( url ) . toBe ( "http://test.kinde.com/oauth2/token" ) ;
186
+ expect ( options ) . toMatchObject ( {
187
+ method : "POST" ,
188
+ headers : {
189
+ "Content-type" : "application/x-www-form-urlencoded; charset=UTF-8" ,
190
+ "Kinde-SDK" : "Framework/Version" ,
191
+ } ,
192
+ } ) ;
136
193
} ) ;
194
+
137
195
} ) ;
0 commit comments