Exemple #1
0
        private async Task SetAuthHeaders(ITestContext testContext, HttpRequestMessage request)
        {
            if (!testContext.HasValidAuthToken())
            {
                this.logger.LogInformation("HasValidAuthToken: false");

                try
                {
                    HttpSession session = await this.httpAuthService.Authenticate();

                    if (session == null)
                    {
                        throw new ApplicationException("Authenticate response is Null");
                    }

                    testContext.SetAuthSession(session);
                    this.logger.LogInformation("Successfully aquired a new token");
                }
                catch (Exception ex)
                {
                    throw new Exception("Unexpected error creating Http session", ex);
                }
            }

            if (string.IsNullOrEmpty(testContext.HttpAuthToken))
            {
                throw new ApplicationException("Context HttpAuthToken IsNullOrEmpty");
            }

            request.Headers.Add(HttpAuthHeaderKey, testContext.HttpAuthToken);
            this.logger.LogDebug($"{HttpAuthHeaderKey}: {testContext.HttpAuthToken}");
        }