Ejemplo n.º 1
0
        public void GetTraces_ReturnsTraces()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs = new TraceObserver(listener, option);

            for (int i = 0; i < 200; i++)
            {
                long start   = Stopwatch.GetTimestamp();
                var  context = CreateRequest();

                KeyValuePair <string, object> begin = new KeyValuePair <string, object>(TraceObserver.BEGIN_REQUEST,
                                                                                        new { httpContext = context, timestamp = start });
                obs.OnNext(begin);

                KeyValuePair <string, object> end = new KeyValuePair <string, object>(TraceObserver.END_REQUEST,
                                                                                      new { httpContext = context, timestamp = start + 100000 });
                obs.OnNext(end);
            }
            Assert.Equal(0, obs._pending.Count);
            Assert.Equal(option.Capacity, obs._queue.Count);
            List <Trace> traces = obs.GetTraces();

            Assert.Equal(option.Capacity, traces.Count);
            Assert.Equal(option.Capacity, obs._queue.Count);

            listener.Dispose();
        }
Ejemplo n.º 2
0
        public void MakeTrace_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            HttpContext   context = CreateRequest();
            Trace         result  = obs.MakeTrace(context, 10000000, 20000000);

            Assert.NotNull(result);
            Assert.NotNull(result.Info);
            Assert.NotEqual(0, result.TimeStamp);
            Assert.True(result.Info.ContainsKey("method"));
            Assert.True(result.Info.ContainsKey("path"));
            Assert.True(result.Info.ContainsKey("headers"));
            Assert.True(result.Info.ContainsKey("timeTaken"));
            Assert.Equal("GET", result.Info["method"]);
            Assert.Equal("/myPath", result.Info["path"]);
            var headers = result.Info["headers"] as Dictionary <string, object>;

            Assert.NotNull(headers);
            Assert.True(headers.ContainsKey("request"));
            Assert.True(headers.ContainsKey("response"));
            var timeTaken = result.Info["timeTaken"] as string;

            Assert.NotNull(timeTaken);
            var expected = ((20000000 - 10000000) / obs.ticksPerMilli).ToString();

            Assert.Equal(expected, timeTaken);
            listener.Dispose();
        }
Ejemplo n.º 3
0
        public void GetHeaders_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            HttpContext   context = CreateRequest();

            var result = obs.GetHeaders(100, context.Request.Headers);

            Assert.NotNull(result);
            Assert.True(result.ContainsKey("header1"));
            Assert.True(result.ContainsKey("header2"));
            Assert.True(result.ContainsKey("status"));
            var header1Val = result["header1"] as string;

            Assert.Equal("header1Value", header1Val);
            var header2Val = result["header2"] as string;

            Assert.Equal("header2Value", header2Val);
            var statusVal = result["status"] as string;

            Assert.Equal("100", statusVal);
            listener.Dispose();
        }
Ejemplo n.º 4
0
        public void GetRequestUri_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            HttpContext   context = CreateRequest();
            var           result  = obs.GetRequestUri(context.Request);

            Assert.Equal("http://localhost:1111/myPath", result);
            listener.Dispose();
        }
Ejemplo n.º 5
0
        public void GetRemoteAddress_NoConnection_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            HttpContext   context = CreateRequest();
            var           result  = obs.GetRemoteAddress(context);

            Assert.Null(result);
            listener.Dispose();
        }
Ejemplo n.º 6
0
        public void GetUserPrincipal_NotAuthenticated_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            HttpContext   context = CreateRequest();
            var           result  = obs.GetUserPrincipal(context);

            Assert.Null(result);
            listener.Dispose();
        }
Ejemplo n.º 7
0
        public void GetProperties_NoProperties_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs       = new TraceObserver(listener, option);
            long          timeStamp = -1;

            obs.GetProperties(new { foo = "bar" }, out HttpContext context, out timeStamp);
            Assert.Equal(0, timeStamp);
            Assert.Null(context);
            listener.Dispose();
        }
Ejemplo n.º 8
0
        public void GetUserPrincipal_Authenticated_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            HttpContext   context = CreateRequest();

            context.User = new ClaimsPrincipal(new MyIdentity());
            var result = obs.GetUserPrincipal(context);

            Assert.Equal("MyTestName", result);
            listener.Dispose();
        }
Ejemplo n.º 9
0
        public void OnNext_IgnoresUnRecognizedEvents()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs = new TraceObserver(listener, option);

            KeyValuePair <string, object> ignore = new KeyValuePair <string, object>("foobar", null);

            obs.OnNext(ignore);
            Assert.Equal(0, obs._pending.Count);
            Assert.Equal(0, obs._queue.Count);
            listener.Dispose();
        }
Ejemplo n.º 10
0
        public void GetTimeTaken_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs      = new TraceObserver(listener, option);
            HttpContext   context  = CreateRequest();
            var           result   = obs.GetTimeTaken(10000000);
            var           expected = (10000000 / obs.ticksPerMilli).ToString();

            Assert.Equal(expected, result);

            listener.Dispose();
        }
Ejemplo n.º 11
0
        public void GetProperties_WithProperties_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs             = new TraceObserver(listener, option);
            long          timeStamp       = -1;
            long          expectedTime    = Stopwatch.GetTimestamp();
            var           expectedContext = CreateRequest();

            obs.GetProperties(new { httpContext = expectedContext, timestamp = expectedTime }, out HttpContext context, out timeStamp);
            Assert.Equal(expectedTime, timeStamp);
            Assert.True(object.ReferenceEquals(expectedContext, context));

            listener.Dispose();
        }
Ejemplo n.º 12
0
        public void OnNext_RemovesPending_AddsToQueue()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            long          start   = Stopwatch.GetTimestamp();
            var           context = CreateRequest();

            KeyValuePair <string, object> begin = new KeyValuePair <string, object>(TraceObserver.BEGIN_REQUEST,
                                                                                    new { httpContext = context, timestamp = start });

            obs.OnNext(begin);

            KeyValuePair <string, object> end = new KeyValuePair <string, object>(TraceObserver.END_REQUEST,
                                                                                  new { httpContext = context, timestamp = start + 100000 });

            obs.OnNext(end);

            Assert.Equal(0, obs._pending.Count);
            Assert.Equal(1, obs._queue.Count);

            Trace result = null;

            Assert.True(obs._queue.TryPeek(out result));
            Assert.NotNull(result.Info);
            Assert.NotEqual(0, result.TimeStamp);
            Assert.True(result.Info.ContainsKey("method"));
            Assert.True(result.Info.ContainsKey("path"));
            Assert.True(result.Info.ContainsKey("headers"));
            Assert.True(result.Info.ContainsKey("timeTaken"));
            Assert.Equal("GET", result.Info["method"]);
            Assert.Equal("/myPath", result.Info["path"]);
            var headers = result.Info["headers"] as Dictionary <string, object>;

            Assert.NotNull(headers);
            Assert.True(headers.ContainsKey("request"));
            Assert.True(headers.ContainsKey("response"));
            var timeTaken = result.Info["timeTaken"] as string;

            Assert.NotNull(timeTaken);
            var expected = ((100000) / obs.ticksPerMilli).ToString();

            Assert.Equal(expected, timeTaken);

            listener.Dispose();
        }
Ejemplo n.º 13
0
        public async void HandleTraceRequestAsync_ReturnsExpected()
        {
            var opts = new TraceOptions();
            DiagnosticListener listener = new DiagnosticListener("test");

            TraceObserver obs     = new TraceObserver(listener, opts);
            var           ep      = new TestTraceEndpoint(opts, obs);
            var           middle  = new TraceEndpointMiddleware(null, ep);
            var           context = CreateRequest("GET", "/trace");
            await middle.HandleTraceRequestAsync(context);

            context.Response.Body.Seek(0, SeekOrigin.Begin);
            StreamReader rdr  = new StreamReader(context.Response.Body);
            string       json = await rdr.ReadToEndAsync();

            Assert.Equal("[]", json);
            listener.Dispose();
        }
Ejemplo n.º 14
0
        public void IsTraceRequest_ReturnsExpected()
        {
            var opts = new TraceOptions();
            DiagnosticListener listener = new DiagnosticListener("test");

            TraceObserver obs     = new TraceObserver(listener, opts);
            var           ep      = new TraceEndpoint(opts, obs);
            var           middle  = new TraceEndpointMiddleware(null, ep);
            var           context = CreateRequest("GET", "/trace");

            Assert.True(middle.IsTraceRequest(context));
            var context2 = CreateRequest("PUT", "/trace");

            Assert.False(middle.IsTraceRequest(context2));
            var context3 = CreateRequest("GET", "/badpath");

            Assert.False(middle.IsTraceRequest(context3));
            listener.Dispose();
        }
Ejemplo n.º 15
0
        public void GetSessionId_WithSession_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            HttpContext   context = CreateRequest();

            var             session     = new TestSession();
            ISessionFeature sessFeature = new SessionFeature();

            sessFeature.Session = session;
            context.Features.Set <ISessionFeature>(sessFeature);

            var result = obs.GetSessionId(context);

            Assert.Equal("TestSessionId", result);
            listener.Dispose();
        }
Ejemplo n.º 16
0
        public void GetRequestParameters_ReturnsExpected()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs     = new TraceObserver(listener, option);
            HttpContext   context = CreateRequest();
            var           result  = obs.GetRequestParameters(context.Request);

            Assert.NotNull(result);
            Assert.True(result.ContainsKey("foo"));
            Assert.True(result.ContainsKey("bar"));
            var fooVal = result["foo"];

            Assert.Equal(1, fooVal.Length);
            Assert.Equal("bar", fooVal[0]);
            var barVal = result["bar"];

            Assert.Equal(1, barVal.Length);
            Assert.Equal("foo", barVal[0]);
            listener.Dispose();
        }
Ejemplo n.º 17
0
        public void OnNext_AddsToPending()
        {
            DiagnosticListener listener = new DiagnosticListener("test");
            TraceOptions       option   = new TraceOptions();

            TraceObserver obs             = new TraceObserver(listener, option);
            long          expectedTime    = Stopwatch.GetTimestamp();
            var           expectedContext = CreateRequest();

            KeyValuePair <string, object> begin = new KeyValuePair <string, object>(TraceObserver.BEGIN_REQUEST,
                                                                                    new { httpContext = expectedContext, timestamp = expectedTime });

            obs.OnNext(begin);

            Assert.Equal(1, obs._pending.Count);
            Assert.True(obs._pending.ContainsKey(expectedContext.TraceIdentifier));
            var pending = obs._pending[expectedContext.TraceIdentifier];

            Assert.Equal(expectedTime, pending.StartTime);

            Assert.Equal(0, obs._queue.Count);
            listener.Dispose();
        }