public void HandleStatusResponseUpdatesCaptureStateToTrue()
        {
            // given
            var responseAttributes = ResponseAttributes.WithUndefinedDefaults().WithCapture(true).Build();
            var response           = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var session = Substitute.For <ISessionInternals>();

            var target = CreateSendingContext().Build();

            target.DisableCaptureAndClear();
            target.AddSession(session);
            Assert.That(target.IsCaptureOn, Is.False);

            // when
            target.HandleStatusResponse(response);

            // then
            Assert.That(session.ReceivedCalls(), Is.Empty);
            Assert.That(target.IsCaptureOn, Is.True);
        }
        public void HandleStatusResponseRemovesFinishedSessionsIfResponseIsCaptureOff()
        {
            // given
            var responseAttributes = ResponseAttributes.WithUndefinedDefaults().WithCapture(false).Build();
            var response           = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var sessionState = Substitute.For <ISessionState>();

            sessionState.IsFinished.Returns(true);
            var session = Substitute.For <ISessionInternals>();

            session.State.Returns(sessionState);

            var target = CreateSendingContext().Build();

            target.AddSession(session);

            // when
            target.HandleStatusResponse(response);

            // then
            Assert.That(target.SessionCount, Is.EqualTo(0));
            session.Received(1).ClearCapturedData();
        }
        public void HandleStatusResponseUpdatesSendInterval()
        {
            // given
            const int sendInterval       = 999;
            var       responseAttributes = ResponseAttributes.WithUndefinedDefaults()
                                           .WithSendIntervalInMilliseconds(sendInterval).Build();
            var response = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var session = Substitute.For <ISessionInternals>();

            var target = CreateSendingContext().Build();

            target.AddSession(session);
            Assert.That(target.SendInterval, Is.Not.EqualTo(sendInterval));

            // then
            target.HandleStatusResponse(response);
            var obtained = target.SendInterval;

            // then
            Assert.That(session.ReceivedCalls(), Is.Empty);
            Assert.That(obtained, Is.EqualTo(sendInterval));
        }
        public void HandleStatusResponseUpdatesHttpClientConfig()
        {
            mockHttpClientConfig.BaseUrl.Returns("https://localhost:9999/1");
            mockHttpClientConfig.ApplicationId.Returns("some cryptic appId");
            mockHttpClientConfig.ServerId.Returns(42);
            mockHttpClientConfig.SslTrustManager.Returns(Substitute.For <ISSLTrustManager>());

            const int serverId           = 73;
            var       responseAttributes = ResponseAttributes.WithUndefinedDefaults().WithServerId(serverId).Build();
            var       response           = StatusResponse.CreateSuccessResponse(
                mockLogger,
                responseAttributes,
                StatusResponse.HttpOk,
                new Dictionary <string, List <string> >()
                );

            var target = Substitute.ForPartsOf <BeaconSendingContext>(
                mockLogger,
                mockHttpClientConfig,
                mockHttpClientProvider,
                mockTimingProvider,
                mockThreadSuspender
                );

            Assert.That(mockHttpClientConfig.ReceivedCalls(), Is.Empty);

            // when
            target.HandleStatusResponse(response);

            // then
            target.Received(1).CreateHttpClientConfigurationWith(serverId);
            _ = mockHttpClientConfig.Received(2).ServerId;
            _ = mockHttpClientConfig.Received(1).BaseUrl;
            _ = mockHttpClientConfig.Received(1).ApplicationId;
            _ = mockHttpClientConfig.Received(1).SslTrustManager;

            // and when
            IHttpClientConfiguration configCapture = null;

            mockHttpClientProvider.CreateClient(Arg.Do <IHttpClientConfiguration>(c => configCapture = c));

            target.GetHttpClient();

            // then
            Assert.That(configCapture, Is.Not.Null);
            mockHttpClientProvider.Received(1).CreateClient(configCapture);

            Assert.That(configCapture, Is.Not.SameAs(mockHttpClientConfig));
            Assert.That(configCapture.ServerId, Is.EqualTo(serverId));
            Assert.That(configCapture.BaseUrl, Is.EqualTo(mockHttpClientConfig.BaseUrl));
            Assert.That(configCapture.ApplicationId, Is.EqualTo(mockHttpClientConfig.ApplicationId));
            Assert.That(configCapture.SslTrustManager, Is.SameAs(mockHttpClientConfig.SslTrustManager));
        }
        public void ApplicationIdMatchesIfApplicationIdWasNotReceived()
        {
            // given
            mockHttpClientConfig.ApplicationId.Returns("application id");
            var attributes = ResponseAttributes.WithUndefinedDefaults().Build();

            var target = CreateSendingContext().Build();

            // when
            var obtained = target.IsApplicationIdMismatch(attributes);

            // then
            Assert.That(obtained, Is.False);
        }
        public void ConfigurationTimestampReturnsValueFromResponseAttributes()
        {
            // given
            const long timestamp  = 1234;
            var        attributes = ResponseAttributes.WithUndefinedDefaults().WithTimestampInMilliseconds(timestamp).Build();
            var        response   =
                StatusResponse.CreateSuccessResponse(mockLogger, attributes, 200,
                                                     new Dictionary <string, List <string> >());
            var target = CreateSendingContext().Build();

            // when
            target.UpdateFrom(response);

            // then
            Assert.That(target.ConfigurationTimestamp, Is.EqualTo(timestamp));
        }
        public void ApplicationIdMismatchesIfStoredAndReceivedApplicationIdsAreNotEqual()
        {
            // given
            const string applicationId = "application id";

            mockHttpClientConfig.ApplicationId.Returns("application ID");
            var attributes = ResponseAttributes.WithUndefinedDefaults()
                             .WithApplicationId(applicationId)
                             .Build();

            var target = CreateSendingContext().Build();

            // when
            var obtained = target.IsApplicationIdMismatch(attributes);

            // then
            Assert.That(obtained, Is.True);
        }
Beispiel #8
0
        internal BeaconSendingContext(
            ILogger logger,
            IHttpClientConfiguration httpClientConfiguration,
            IHttpClientProvider httpClientProvider,
            ITimingProvider timingProvider,
            IInterruptibleThreadSuspender threadSuspender,
            AbstractBeaconSendingState initialState
            )
        {
            this.logger = logger;
            this.httpClientConfiguration = httpClientConfiguration;
            serverConfiguration          = ServerConfiguration.Default;
            HttpClientProvider           = httpClientProvider;
            this.timingProvider          = timingProvider;
            this.threadSuspender         = threadSuspender;
            lastResponseAttributes       = ResponseAttributes.WithUndefinedDefaults().Build();

            CurrentState = initialState;
        }
        public void UpdateFromDisablesCapturingIfReceivedApplicationIdMismatches()
        {
            // given
            mockHttpClientConfig.ApplicationId.Returns("some application id");
            var attributes = ResponseAttributes.WithUndefinedDefaults()
                             .WithApplicationId("different application id")
                             .Build();
            var response = Substitute.For <IStatusResponse>();

            response.ResponseAttributes.Returns(attributes);
            response.IsErroneousResponse.Returns(false);

            var target           = CreateSendingContext().Build();
            var initialCaptureOn = target.IsCaptureOn;

            // when
            target.UpdateFrom(response);

            // then
            Assert.That(initialCaptureOn, Is.True);
            Assert.That(target.IsCaptureOn, Is.False);
        }
        public void UpdateFromMergesResponseAttributesFromStatusResponse()
        {
            // given
            const int serverId   = 9999;
            var       attributes = ResponseAttributes.WithUndefinedDefaults().WithServerId(serverId).Build();
            var       response   = Substitute.For <IStatusResponse>();

            response.ResponseAttributes.Returns(attributes);
            response.IsErroneousResponse.Returns(false);

            var target            = CreateSendingContext().Build();
            var initialAttributes = target.LastResponseAttributes;

            // when
            var obtained = target.UpdateFrom(response);

            // then
            Assert.That(obtained, Is.EqualTo(target.LastResponseAttributes));
            Assert.That(obtained, Is.Not.EqualTo(initialAttributes));
            Assert.That(obtained, Is.Not.EqualTo(attributes));
            Assert.That(obtained.ServerId, Is.EqualTo(serverId));
        }