diff --git a/ably/realtime_conn_spec_integration_test.go b/ably/realtime_conn_spec_integration_test.go index a45028ad..e1d187cb 100644 --- a/ably/realtime_conn_spec_integration_test.go +++ b/ably/realtime_conn_spec_integration_test.go @@ -3027,17 +3027,10 @@ func TestRealtimeConn_RTC8a_ExplicitAuthorizeWhileConnected(t *testing.T) { app := ablytest.MustSandbox(nil) defer safeclose(t, app) - key, secret := app.KeyParts() - authParams := url.Values{} - authParams.Add("environment", app.Environment) - authParams.Add("returnType", "jwt") - authParams.Add("keyName", key) - authParams.Add("keySecret", secret) - rec, optn := ablytest.NewHttpRecorder() rest, err := ably.NewREST( - ably.WithAuthURL("https://echo.ably.io/createJWT"), - ably.WithAuthParams(authParams), + ably.WithAuthURL(ablytest.CREATE_JWT_URL), + ably.WithAuthParams(app.GetJwtAuthParams(30*time.Second)), ably.WithEnvironment(app.Environment), ably.WithKey(""), optn[0], @@ -3052,7 +3045,7 @@ func TestRealtimeConn_RTC8a_ExplicitAuthorizeWhileConnected(t *testing.T) { // first request is jwt request jwtRequest := rec.Request(0).URL - assert.Equal(t, "echo.ably.io/createJWT", jwtRequest.Host+jwtRequest.Path) + assert.Equal(t, ablytest.CREATE_JWT_URL, "https://"+jwtRequest.Host+jwtRequest.Path) // response is jwt token jwtResponse, err := io.ReadAll(rec.Response(0).Body) assert.NoError(t, err) diff --git a/ablytest/sandbox.go b/ablytest/sandbox.go index 469e47bd..9dc90901 100644 --- a/ablytest/sandbox.go +++ b/ablytest/sandbox.go @@ -256,6 +256,43 @@ func (app *Sandbox) URL(paths ...string) string { return "https://" + app.Environment + "-rest.ably.io/" + path.Join(paths...) } +// Source code for the same => https://github.com/ably/echoserver/blob/main/app.js +var CREATE_JWT_URL string = "https://echo.ably.io/createJWT" + +// Returns authParams, required for authUrl as a mode of auth +func (app *Sandbox) GetJwtAuthParams(expiresIn time.Duration) url.Values { + key, secret := app.KeyParts() + authParams := url.Values{} + authParams.Add("environment", app.Environment) + authParams.Add("returnType", "jwt") + authParams.Add("keyName", key) + authParams.Add("keySecret", secret) + authParams.Add("expiresIn", fmt.Sprint(expiresIn.Milliseconds())) + return authParams +} + +// Returns JWT with given expiry +func (app *Sandbox) CreateJwt(expiresIn time.Duration) (string, error) { + u, err := url.Parse(CREATE_JWT_URL) + if err != nil { + return "", err + } + u.RawQuery = app.GetJwtAuthParams(expiresIn).Encode() + req, err := http.NewRequest(http.MethodGet, u.String(), nil) + if err != nil { + return "", fmt.Errorf("client: could not create request: %s", err) + } + res, err := app.client.Do(req) + if err != nil { + return "", fmt.Errorf("client: error making http request: %s", err) + } + resBody, err := io.ReadAll(res.Body) + if err != nil { + return "", fmt.Errorf("client: could not read response body: %s", err) + } + return string(resBody), nil +} + func NewHTTPClient() *http.Client { const timeout = time.Minute return &http.Client{