@@ -259,7 +259,14 @@ func (app *Sandbox) URL(paths ...string) string {
259
259
// Source code for the same => https://github.com/ably/echoserver/blob/main/app.js
260
260
var CREATE_JWT_URL string = "https://echo.ably.io/createJWT"
261
261
262
- // Returns authParams, required for authUrl as a mode of auth
262
+ // GetJwtAuthParams constructs the authentication parameters required for JWT creation.
263
+ // Required when authUrl is chosen as a mode of auth
264
+ //
265
+ // Parameters:
266
+ // - expiresIn: The duration until the JWT expires.
267
+ // - invalid: A boolean flag indicating whether to use an invalid key secret.
268
+ //
269
+ // Returns: A url.Values object containing the authentication parameters.
263
270
func (app * Sandbox ) GetJwtAuthParams (expiresIn time.Duration , invalid bool ) url.Values {
264
271
key , secret := app .KeyParts ()
265
272
authParams := url.Values {}
@@ -275,7 +282,15 @@ func (app *Sandbox) GetJwtAuthParams(expiresIn time.Duration, invalid bool) url.
275
282
return authParams
276
283
}
277
284
278
- // Returns JWT with given expiry
285
+ // CreateJwt generates a JWT with the specified expiration time.
286
+ //
287
+ // Parameters:
288
+ // - expiresIn: The duration until the JWT expires.
289
+ // - invalid: A boolean flag indicating whether to use an invalid key secret.
290
+ //
291
+ // Returns:
292
+ // - A string containing the generated JWT.
293
+ // - An error if the JWT creation fails.
279
294
func (app * Sandbox ) CreateJwt (expiresIn time.Duration , invalid bool ) (string , error ) {
280
295
u , err := url .Parse (CREATE_JWT_URL )
281
296
if err != nil {
@@ -288,12 +303,17 @@ func (app *Sandbox) CreateJwt(expiresIn time.Duration, invalid bool) (string, er
288
303
}
289
304
res , err := app .client .Do (req )
290
305
if err != nil {
306
+ res .Body .Close ()
291
307
return "" , fmt .Errorf ("client: error making http request: %s" , err )
292
308
}
309
+ defer res .Body .Close ()
293
310
resBody , err := io .ReadAll (res .Body )
294
311
if err != nil {
295
312
return "" , fmt .Errorf ("client: could not read response body: %s" , err )
296
313
}
314
+ if res .StatusCode != 200 {
315
+ return "" , fmt .Errorf ("non-success response received: %v:%s" , res .StatusCode , resBody )
316
+ }
297
317
return string (resBody ), nil
298
318
}
299
319
0 commit comments