예제 #1
0
        public void TestAsyncMethod()
        {
            var client = new LogControlClient(BASE_URL);
            client.Headers.Add("Foo", FOO);
            client.Headers.Add("RequestId", REQUEST_ID);
            int onResponseCount = 0;

            client.OnComplete += (data) => {
                onResponseCount++;
                CheckLog(data.Url, data.Request, data.Response, data.Status);
            };
            var t1 = client.AddJokeAsync(new Joke() { Question = JOKE, Answer = ANSWER, DateAdded = DateTime.Now });
            var t2 = client.SetLogStatusAsync(Logs.ErrorLog, State.ON);
            var t3 = client.SetLogStatusAsync(Logs.MessageLog, State.OFF);
            Info toSet = new Info();
            toSet.Status["TransLog1"] = State.ON;
            toSet.Status["TransLog2"] = State.OFF;
            toSet.Status["TransLog3"] = State.ON;
            toSet.Status["TransLog4"] = State.OFF;
            System.Threading.Tasks.Task.WaitAll(t1, t2, t3);

            var t4 = client.SetMultiAsync(toSet);
            var t5 = client.GetLoggingStatusAsync();
            CheckResult(t4.Result, "1");
            CheckResult(t5.Result, "2");
            Assert.AreEqual(5, onResponseCount, "OnResponse event called wrong number of times");
        }
예제 #2
0
 public void TestDefaultOverride()
 {
     var client = new LogControlClient(BASE_URL);
     Info toSet = new Info();
     toSet.Status["TransLog1"] = State.ON;
     toSet.Status["TransLog2"] = State.OFF;
     toSet.Status["TransLog3"] = State.ON;
     toSet.Status["TransLog4"] = State.OFF;
     var finfo = client.SetMulti(toSet, 5);
     CheckResult(finfo, "1");
     //Check default
     Assert.AreEqual(5, finfo.LogLevel, "Default logLevel should be overridden");
 }
예제 #3
0
        void Test200Error(Action<LogControlClient, Info> requestAction)
        {
            var client = new LogControlClient(BAD_200_BASE_URL);
            client.Headers.Add("Foo", FOO);
            client.Headers.Add("RequestId", REQUEST_ID);
            bool onCompleteCalled = false;

            client.OnStart += (d) => {
                System.Diagnostics.Debug.WriteLine("Sending request for method {0} (Full URL: {1}) {2}", d.Url, d.Method, d.Stage);
                System.Diagnostics.Debug.WriteLine("Request headers {0} (ms)", d.RequestHeaders);
            };

            client.OnComplete += (d) => {
                onCompleteCalled = true;
                System.Diagnostics.Debug.WriteLine("Sending request for method {0} (Full URL: {1}) {2}", d.Url, d.Method, d.Stage);
                System.Diagnostics.Debug.WriteLine("Request: {0}", Encoding.UTF8.GetString(d.Request));
                System.Diagnostics.Debug.WriteLine("Response: {0}", Encoding.UTF8.GetString(d.Response));
                System.Diagnostics.Debug.WriteLine("Request duration: {0} ms", d.DurationMilliseconds, null);
                string requestHeadersString = string.Join("\r\n", d.RequestHeaders.AllKeys.Select(k => { return k + ":" + d.RequestHeaders[k]; }).ToArray());
                System.Diagnostics.Debug.WriteLine("Request headers: {0}", requestHeadersString, null);
                string responseHeadersString = string.Join("\r\n", d.ResponseHeaders.AllKeys.Select(k => { return k + ":" + d.ResponseHeaders[k]; }).ToArray());
                System.Diagnostics.Debug.WriteLine("Response headers: {0}", responseHeadersString, null);
            };

            client.OnFailure += (d) => {
                Assert.Fail("OnFailure shouldn't be called on HTTP 200");
            };

            Info toSet = new Info();
            toSet.Status["TransLog1"] = State.ON;
            toSet.Status["TransLog2"] = State.OFF;
            toSet.Status["TransLog3"] = State.ON;
            toSet.Status["TransLog4"] = State.OFF;
            try
            {
                requestAction(client, toSet);
                Assert.Fail("Error should be thrown");
            }
            catch(BabelRequestException err)
            {
                Assert.AreEqual(1, err.Errors.Count, "Should contain 1 error");
                Assert.AreEqual("RESPONSE_FORMAT_ERROR", err.Errors[0].Code, "Should have proper code");

            }
            Assert.IsTrue(onCompleteCalled, "OnComplete should be called");
        }
예제 #4
0
        void Test500Error(Action<LogControlClient, Info> requestAction, int errType, string expectedHttpStatus, int expectedErrorCount, string expectedErrorCode, string expectedErrorMessage, string expectedExceptionMessage, string expectedExceptionTypeName)
        {
            var client = new LogControlClient(BASE_URL, 9);
            client.Headers.Add("ShouldError", errType.ToString());

            bool onFailureCalled = false;
            bool onStartCalled = false;

            client.OnStart += (d) => {
                onStartCalled = true;
                System.Diagnostics.Debug.WriteLine("Sending request for method {0} (Full URL: {1}) {2}", d.Url, d.Method, d.Stage);
                System.Diagnostics.Debug.WriteLine("Request headers {0} (ms)", d.RequestHeaders);
            };

            client.OnComplete += (d) => {
                Assert.Fail("OnComplete shouldn't be called");
            };

            client.OnFailure += (d) => {
                Assert.AreEqual(expectedHttpStatus, d.Status, "Wrong HTTP Status");
                onFailureCalled = true;
            };

            Info toSet = new Info();
            toSet.Status["TransLog1"] = State.ON;
            try
            {
                requestAction(client, toSet);
                Assert.Fail("Error should be thrown");
            }
            catch(BabelException err)
            {
                Assert.AreEqual(expectedExceptionTypeName, err.GetType().Name, "Bad exception type");
                Assert.AreEqual(expectedErrorCount, err.Errors.Count, "Wrong error count");
                Assert.AreEqual(expectedErrorCode, err.Errors[0].Code, "Wrong error code");
                Assert.AreEqual(expectedErrorMessage, err.Errors[0].Message, "Wrong error code");
                Assert.AreEqual(expectedExceptionMessage, err.Message, "Wrong error code");
            }
            Assert.IsTrue(onStartCalled, "OnStart should be called");
            Assert.IsTrue(onFailureCalled, "OnComplete should be called");
        }
예제 #5
0
        public void TestMethod()
        {
            var client = new LogControlClient(BASE_URL);
            client.Headers.Add("Foo", FOO);
            client.Headers.Add("RequestId", REQUEST_ID);
            int onResponseCount = 0;

            client.OnComplete += (data) =>
                {
                    onResponseCount ++;
                    CheckLog(data.Url, data.Request, data.Response, data.Status);
                };

            client.OnStart += (d)=>
            {
                System.Diagnostics.Debug.WriteLine("Sending request for method {0} (Full URL: {1}) {2}", d.Url, d.Method, d.Stage);
                System.Diagnostics.Debug.WriteLine("Request headers {0} (ms)", d.RequestHeaders);
            };

            client.OnComplete += (d) => {
                System.Diagnostics.Debug.WriteLine("Sending request for method {0} (Full URL: {1}) {2}", d.Url, d.Method, d.Stage);
                System.Diagnostics.Debug.WriteLine("Request: {0}", Encoding.UTF8.GetString(d.Request));
                System.Diagnostics.Debug.WriteLine("Response: {0}", Encoding.UTF8.GetString(d.Response));
                System.Diagnostics.Debug.WriteLine("Request duration: {0} ms", d.DurationMilliseconds);
                string requestHeadersString = string.Join("\r\n", d.RequestHeaders.AllKeys.Select(k => { return k + ":" + d.RequestHeaders[k]; }).ToArray());
                System.Diagnostics.Debug.WriteLine("Request headers: {0}", requestHeadersString);
                string responseHeadersString = string.Join("\r\n", d.ResponseHeaders.AllKeys.Select(k => { return k + ":" + d.ResponseHeaders[k]; }).ToArray());
                System.Diagnostics.Debug.WriteLine("Response headers: {0}", responseHeadersString);
            };

            client.OnFailure += (d)=>
            {
                System.Diagnostics.Debug.WriteLine("Sending request for method {0} (Full URL: {1}) {2}", d.Url, d.Method, d.Stage);
                System.Diagnostics.Debug.WriteLine("Request: {0}", Encoding.UTF8.GetString(d.Request));
                System.Diagnostics.Debug.WriteLine("Response: {0}", Encoding.UTF8.GetString(d.Response));
                System.Diagnostics.Debug.WriteLine("Request duration: {0} (ms)", d.DurationMilliseconds);
                string requestHeadersString = string.Join("\r\n", d.RequestHeaders.AllKeys.Select(k => { return k + ":" + d.RequestHeaders[k]; }).ToArray());
                System.Diagnostics.Debug.WriteLine("Request headers: {0}", requestHeadersString);
                string responseHeadersString = string.Join("\r\n", d.ResponseHeaders.AllKeys.Select(k => { return k + ":" + d.ResponseHeaders[k]; }).ToArray());
                System.Diagnostics.Debug.WriteLine("Response headers: {0}", responseHeadersString);
                System.Diagnostics.Debug.WriteLine("Transport specific error code: {0}", d.Status);
            };

            client.AddJoke(new Joke() { Question = JOKE, Answer = ANSWER, DateAdded = DateTime.Now });
            client.SetLogStatus(Logs.ErrorLog, State.ON);
            client.SetLogStatus(Logs.MessageLog, State.OFF);
            Info toSet = new Info();
            toSet.Status["TransLog1"] = State.ON;
            toSet.Status["TransLog2"] = State.OFF;
            toSet.Status["TransLog3"] = State.ON;
            toSet.Status["TransLog4"] = State.OFF;
            var finfo = client.SetMulti(toSet);
            CheckResult(finfo, "1");
            //Check default
            Assert.AreEqual(1, finfo.LogLevel, "LogLevel should be set to 1 by default");

            Info info = client.GetLoggingStatus();
            CheckResult(info, "2");

            Assert.AreEqual(5, onResponseCount, "OnResponse event called wrong number of times");
        }