public void SendBeaconRequestReturnsAnUnknownErrorResponseForWrongHttpResponse()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoPostRequest(string.Empty, string.Empty, null)).DoNotCallBase();

            // when
            spyClient.DoPostRequest(string.Empty, string.Empty, null).ReturnsForAnyArgs(InvalidStatusResponseShort);
            var obtained = target.SendBeaconRequest("156.33.241.5", null);

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(int.MaxValue));
            Assert.That(obtained.Headers, Is.Empty);

            // and when
            spyClient.DoPostRequest(string.Empty, string.Empty, null).ReturnsForAnyArgs(InvalidStatusResponseLong);
            obtained = target.SendBeaconRequest("156.33.241.5", null);

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(int.MaxValue));
            Assert.That(obtained.Headers, Is.Empty);

            spyClient.ReceivedWithAnyArgs(2).DoPostRequest(string.Empty, string.Empty, null);
        }
        public void SendBeaconRequestReturnsErrorCode()
        {
            // given
            var headers = new Dictionary <string, List <string> >
            {
                { "Content-Length", new List <string> {
                      "42"
                  } },
                { "Content-Type", new List <string> {
                      "application/json"
                  } }
            };
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoPostRequest(string.Empty, string.Empty, null)).DoNotCallBase();
            spyClient.DoPostRequest(string.Empty, string.Empty, null).ReturnsForAnyArgs(new HTTPClient.HTTPResponse {
                ResponseCode = 400, Headers = headers, Response = null
            });

            // when
            var obtained = target.SendBeaconRequest("156.33.241.5", null);

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(400));
            Assert.That(obtained.Headers, Is.EqualTo(headers));

            spyClient.ReceivedWithAnyArgs(1).DoPostRequest(string.Empty, string.Empty, null);
        }
Exemple #3
0
        public void SendTimeSyncRequestWorksIfResponseIsNull()
        {
            // given
            var headers = new Dictionary <string, List <string> >
            {
                { "Content-Length", new List <string> {
                      "42"
                  } },
                { "Content-Type", new List <string> {
                      "application/json"
                  } }
            };
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoGetRequest(string.Empty, string.Empty)).DoNotCallBase();
            spyClient.DoGetRequest(string.Empty, string.Empty).ReturnsForAnyArgs(new HTTPClient.HTTPResponse {
                ResponseCode = 200, Headers = headers, Response = null
            });

            // when
            var obtained = target.SendTimeSyncRequest();

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(200));
            Assert.That(obtained.Headers, Is.EqualTo(headers));

            spyClient.ReceivedWithAnyArgs(1).DoGetRequest(string.Empty, string.Empty);
        }
        public void SendBeaconRequestSendsNullDataIfNullWasGiven()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoPostRequest(string.Empty, string.Empty, null)).DoNotCallBase();
            spyClient.DoPostRequest(string.Empty, string.Empty, null).ReturnsForAnyArgs(StatusResponse);

            // when
            var obtained = target.SendBeaconRequest("175.45.176.1", null);

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(200));

            spyClient.Received(1).DoPostRequest(Arg.Is <string>(x => !string.IsNullOrEmpty(x)), Arg.Is <string>(x => !string.IsNullOrEmpty(x)), null);
        }
        public void SendStatusRequestSendsOneHTTPGetRequest()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoGetRequest(string.Empty, string.Empty)).DoNotCallBase();
            spyClient.DoGetRequest(string.Empty, string.Empty).ReturnsForAnyArgs(StatusResponse);


            // when
            var obtained = target.SendStatusRequest();

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(200));

            spyClient.ReceivedWithAnyArgs(1).DoGetRequest(string.Empty, string.Empty);
        }
        public void SendBeaconRequestIsRetriedThreeTimesBeforeGivingUp()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoPostRequest(string.Empty, string.Empty, null)).DoNotCallBase();
            spyClient.WhenForAnyArgs(x => x.DoPostRequest(string.Empty, string.Empty, null)).Do(x => throw new Exception("dummy"));

            // when
            var obtained = target.SendBeaconRequest("156.33.241.5", null);

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(int.MaxValue));
            Assert.That(obtained.Headers, Is.Empty);

            spyClient.ReceivedWithAnyArgs(3).DoPostRequest(string.Empty, string.Empty, null);
        }
        public void SendBeaconRequestSendsOneHTTPPostRequest()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoPostRequest(string.Empty, string.Empty, null)).DoNotCallBase();
            spyClient.DoPostRequest(string.Empty, string.Empty, null).ReturnsForAnyArgs(StatusResponse);


            // when
            var obtained = target.SendBeaconRequest("175.45.176.1", new byte[] { 0xba, 0xad, 0xbe, 0xef });

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(200));

            spyClient.ReceivedWithAnyArgs(1).DoPostRequest(string.Empty, string.Empty, null);
        }
Exemple #8
0
        public void SendTimeSyncRequestReturnsAnUnknownErrorResponseForWrongHttpResponse()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoGetRequest(string.Empty, string.Empty)).DoNotCallBase();
            spyClient.DoGetRequest(string.Empty, string.Empty).ReturnsForAnyArgs(StatusResponse);

            // when
            var obtained = target.SendTimeSyncRequest();

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(int.MaxValue));
            Assert.That(obtained.Headers, Is.Empty);

            spyClient.ReceivedWithAnyArgs(1).DoGetRequest(string.Empty, string.Empty);
        }
Exemple #9
0
        public void SendTimeSyncRequestSendRequestToTimeSyncURL()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoGetRequest(string.Empty, string.Empty)).DoNotCallBase();
            spyClient.DoGetRequest(string.Empty, string.Empty).ReturnsForAnyArgs(TimeSyncResponse);


            // when
            var obtained = target.SendTimeSyncRequest();

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(200));

            spyClient.Received(1).DoGetRequest(TimeSyncURL, null);
        }
        public void SendBeaconRequestSendsGzipCompressedDataIfNonNullDataWasGiven()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoPostRequest(string.Empty, string.Empty, null)).DoNotCallBase();
            spyClient.DoPostRequest(string.Empty, string.Empty, null).ReturnsForAnyArgs(StatusResponse);

            // when
            var obtained = target.SendBeaconRequest("175.45.176.1", System.Text.Encoding.UTF8.GetBytes("The quick brown fox jumps over the lazy dog"));

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(200));

            spyClient.Received(1).DoPostRequest(Arg.Is <string>(x => !string.IsNullOrEmpty(x)),
                                                Arg.Is <string>(x => !string.IsNullOrEmpty(x)),
                                                Arg.Is <byte[]>(x => System.Text.Encoding.UTF8.GetString(Unzip(x)) == "The quick brown fox jumps over the lazy dog"));
        }
        public void SendNewSessionRequestReturnsAnUnknownErrorResponseForUnparseableStatusResponse()
        {
            // given
            HTTPClient target = spyClient;

            spyClient.WhenForAnyArgs(x => x.DoGetRequest(string.Empty, string.Empty)).DoNotCallBase();
            spyClient.DoGetRequest(string.Empty, string.Empty).ReturnsForAnyArgs(
                new HTTPClient.HTTPResponse {
                ResponseCode = 200, Headers = new Dictionary <string, List <string> >(), Response = StatusResponse.Response + "&cp=a"
            });

            // when
            var obtained = target.SendNewSessionRequest();

            // then
            Assert.That(obtained, Is.Not.Null);
            Assert.That(obtained.ResponseCode, Is.EqualTo(int.MaxValue));
            Assert.That(obtained.Headers, Is.Empty);

            spyClient.ReceivedWithAnyArgs(1).DoGetRequest(string.Empty, string.Empty);
        }