コード例 #1
0
        public async Task HttpAuthorizerShouldRaiseExceptionWhenTimingOutAsync()
        {
            // Arrange
            int    hostPort      = _HostPort++;
            string hostUrl       = "http://localhost:" + (hostPort).ToString();
            string FakeTokenAuth = "{auth: 'b928ab800c5c554a47ad:f41b9934520700d8474928d03ea7d808cab0cc7fcec082676f6b73ca0d9ab2b'}";

            var server = FluentMockServer.Start(hostPort);

            server
            .Given(
                Requests.WithUrl("/authz").UsingPost()
                )
            .RespondWith(
                Responses
                .WithStatusCode(200)
                .WithHeader("Content-Type", "application/json")
                .WithBody(FakeTokenAuth)
                );

            ChannelAuthorizationFailureException channelException = null;

            // Act
            var testHttpAuthorizer = new HttpAuthorizer(hostUrl + "/authz")
            {
                Timeout = TimeSpan.FromTicks(1),
            };

            // Try to generate the error multiple times as it does not always error the first time
            for (int attempt = 0; attempt < TimeoutRetryAttempts; attempt++)
            {
                try
                {
                    await testHttpAuthorizer.AuthorizeAsync("private-test", "fsfsdfsgsfs");
                }
                catch (Exception e)
                {
                    channelException = e as ChannelAuthorizationFailureException;
                }

                if (channelException != null && channelException.PusherCode == ErrorCodes.ChannelAuthorizationTimeout)
                {
                    break;
                }
            }

            // Assert
            AssertTimeoutError(channelException);
        }
コード例 #2
0
        public async Task HttpAuthorizerShouldRaiseExceptionWhenRequestTimeoutAsync()
        {
            // Arrange
            int    hostPort      = _HostPort++;
            string hostUrl       = "http://localhost:" + (hostPort).ToString();
            string FakeTokenAuth = "{auth: 'b928ab800c5c554a47ad:f41b9934520700d8474928d03ea7d808cab0cc7fcec082676f6b73ca0d9ab2b'}";

            var server = FluentMockServer.Start(hostPort);

            server
            .Given(
                Requests.WithUrl("/authz").UsingPost()
                )
            .RespondWith(
                Responses
                .WithStatusCode(408)
                .WithHeader("Content-Type", "application/json")
                .WithBody(FakeTokenAuth)
                );

            ChannelAuthorizationFailureException channelException = null;

            // Act
            var testHttpAuthorizer = new HttpAuthorizer(hostUrl + "/authz");

            try
            {
                await testHttpAuthorizer.AuthorizeAsync("private-test", "fsfsdfsgsfs");
            }
            catch (Exception e)
            {
                channelException = e as ChannelAuthorizationFailureException;
            }

            // Assert
            AssertTimeoutError(channelException);
        }
コード例 #3
0
        public async Task HttpAuthorizerShouldRaiseExceptionIfAuthorizerUrlNotFoundAsync()
        {
            string channelName   = "private-unauthorized-test";
            string socketId      = Guid.NewGuid().ToString("N");
            int    hostPort      = _HostPort++;
            string hostUrl       = "http://localhost:" + (hostPort).ToString();
            string FakeTokenAuth = "NotFound";

            var server = FluentMockServer.Start(hostPort);

            server
            .Given(
                Requests.WithUrl("/authz").UsingPost()
                )
            .RespondWith(
                Responses
                .WithStatusCode(200)
                .WithHeader("Content-Type", "application/json")
                .WithBody(FakeTokenAuth)
                );

            ChannelAuthorizationFailureException exception = null;
            var testHttpAuthorizer = new HttpAuthorizer(hostUrl + "/notfound");

            try
            {
                await testHttpAuthorizer.AuthorizeAsync(channelName, socketId).ConfigureAwait(false);
            }
            catch (ChannelAuthorizationFailureException e)
            {
                exception = e;
            }

            Assert.IsNotNull(exception, $"Expecting a {nameof(ChannelAuthorizationFailureException)}");
            Assert.IsTrue(exception.Message.Contains("404"));
            Assert.AreEqual(ErrorCodes.ChannelAuthorizationError, exception.PusherCode);
        }
コード例 #4
0
 private static void AssertTimeoutError(ChannelAuthorizationFailureException channelException)
 {
     Assert.IsNotNull(channelException, $"Expected a {nameof(ChannelAuthorizationFailureException)}");
     Assert.AreEqual(ErrorCodes.ChannelAuthorizationTimeout, channelException.PusherCode);
 }