Skip to content

Commit 1ca24af

Browse files
committed
Updated sandbox GetJWTAuthParams and CreateJwt method as per review comments
1 parent cbd39b6 commit 1ca24af

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

ablytest/sandbox.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,14 @@ func (app *Sandbox) URL(paths ...string) string {
259259
// Source code for the same => https://github.com/ably/echoserver/blob/main/app.js
260260
var CREATE_JWT_URL string = "https://echo.ably.io/createJWT"
261261

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.
263270
func (app *Sandbox) GetJwtAuthParams(expiresIn time.Duration, invalid bool) url.Values {
264271
key, secret := app.KeyParts()
265272
authParams := url.Values{}
@@ -275,7 +282,15 @@ func (app *Sandbox) GetJwtAuthParams(expiresIn time.Duration, invalid bool) url.
275282
return authParams
276283
}
277284

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.
279294
func (app *Sandbox) CreateJwt(expiresIn time.Duration, invalid bool) (string, error) {
280295
u, err := url.Parse(CREATE_JWT_URL)
281296
if err != nil {
@@ -288,12 +303,17 @@ func (app *Sandbox) CreateJwt(expiresIn time.Duration, invalid bool) (string, er
288303
}
289304
res, err := app.client.Do(req)
290305
if err != nil {
306+
res.Body.Close()
291307
return "", fmt.Errorf("client: error making http request: %s", err)
292308
}
309+
defer res.Body.Close()
293310
resBody, err := io.ReadAll(res.Body)
294311
if err != nil {
295312
return "", fmt.Errorf("client: could not read response body: %s", err)
296313
}
314+
if res.StatusCode != 200 {
315+
return "", fmt.Errorf("non-success response received: %v:%s", res.StatusCode, resBody)
316+
}
297317
return string(resBody), nil
298318
}
299319

0 commit comments

Comments
 (0)