Ejemplo n.º 1
0
        public static void AddStopwatchAndRequestKey_Should_Add_Pipelines()
        {
            // arrange
            var pipelines = new Pipelines();
            var context   = NancyContextMock.Create(
                responseContent: "{ \"test\": 1 }",
                responseHeaders: new Dictionary <string, string>
            {
                { "Content-Type", "application/json" }
            });
            TinyIoCContainer container = new TinyIoCContainer();

            // act
            pipelines.AddStopwatchAndRequestKeyPipelines();
            pipelines.BeforeRequest.Invoke(context, new CancellationToken());
            pipelines.AfterRequest.Invoke(context, new CancellationToken());
            context.Response.Headers.Remove("X-Internal-Time");
            context.Response.Headers.Remove("RequestKey");
            pipelines.OnError.Invoke(context, new Exception());

            // assert
            Assert.Single(pipelines.BeforeRequest.PipelineItems);
            Assert.Single(pipelines.AfterRequest.PipelineItems);
            Assert.Single(pipelines.OnError.PipelineItems);
        }
Ejemplo n.º 2
0
        public void LogData_Should_Work_With_Null_AdditionalInfo()
        {
            // arrange
            var requestHeaders = new Dictionary <string, IEnumerable <string> >
            {
                { "X-Forwarded-For", new string[] { "226.225.223.224" } },
                { "Content-Type", new string[] { "application/json" } },
                { "Accept", new string[] { "application/json", "text/xml" } }
            };
            var responseHeaders = new Dictionary <string, string>
            {
                { "Content-Type", "application/json" },
                { "X-Internal-Time", "100" }
            };
            var context = NancyContextMock.Create("GET", responseHeaders: responseHeaders, requestHeaders: requestHeaders);

            context.Items["NancySerilogAdditionalInfo"] = null;

            var config = new NancySerilogConfiguration
            {
                InformationTitle = "{SomeProperty} test additional null"
            };
            var logger = new CommunicationLogger(config);

            // act
            logger.LogData(context);

            // assert
            Assert.Contains("{SomeProperty} test additional null", this.TestOutputHelper.Output);
        }
        public static void GetResponseHeaders_Should_Return_Empty_When_Context_Response_Headers_Is_Null()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(responseHeaders: null);

            // act
            var headers = context.GetResponseHeaders();

            // assert
            Assert.Null(headers);
        }
        public static void GetRequestHeaders_Should_Return_Empty_When_Context_Request_Headers_Is_Empty()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(requestHeaders: null);

            // act
            var headers = context.GetRequestHeaders();

            // assert
            Assert.Empty(headers);
        }
        public static void GetQueryString_Should_Return_Empty_When_Context_Request_Query_Is_Empty()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(requestUrl: "http://url-without-query.com");

            // act
            var query = context.GetQueryString();

            // assert
            Assert.Empty(query);
        }
        public static void GetExecutionTime_Should_Return_Default_When_Context_Request_Headers_Is_Empty()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(requestHeaders: null);

            // act
            var time = context.GetExecutionTime();

            // assert
            Assert.Equal(-1, Convert.ToInt32(time));
        }
        public static void GetResponseLength_Should_Return_Length_Of_Content()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(responseContent: "0123456789");

            // act
            var length = context.GetResponseLength();

            // assert
            Assert.Equal(10, length);
        }
        public static void GetResponseLength_Should_Return_0_When_Context_Response_Content_Is_Null()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(responseContent: null);

            // act
            var length = context.GetResponseLength();

            // assert
            Assert.Equal(0, length);
        }
        public static void GetIp_Should_Return_Default_When_Context_Request_Headers_Is_Null_And_Origin_Is_Null()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(requestHeaders: null, originIp: null);

            // act
            var ip = context.GetIp();

            // assert
            Assert.Equal("??", ip);
        }
        public static void GetRequestKey_Should_Return_Null_When_Context_Request_Headers_Is_Empty()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(requestHeaders: null);

            // act
            var requestKey = context.GetRequestKey();

            // assert
            Assert.Null(requestKey);
        }
        public static void GetRequestBody_Should_Return_Value_As_String_When_ContentType_Is_Not_ApplicationJson_With_Empty_Blacklist()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                requestBody: "0123456789");

            // act
            dynamic body = context.GetRequestBody(new string[] { });

            // assert
            Assert.Equal("0123456789", body["raw_body"]);
        }
        public static void GetStatusCodeFamily_Should_Return_2XX_From_200_OK()
        {
            // arrange
            var       context   = NancyContextMock.Create(responseStatusCode: HttpStatusCode.OK);
            Exception exception = null;

            // act
            var statusCode = context.GetStatusCodeFamily(exception);

            // assert
            Assert.Equal("2XX", statusCode);
        }
        public static void GetStatusCodeFamily_Should_Return_4XX_From_412_PreconditionFailed()
        {
            // arrange
            var       context   = NancyContextMock.Create(responseStatusCode: HttpStatusCode.PreconditionFailed);
            Exception exception = null;

            // act
            var statusCode = context.GetStatusCodeFamily(exception);

            // assert
            Assert.Equal("4XX", statusCode);
        }
        public static void GetStatusCode_Should_Return_500_InternalServerError_When_Exception_Is_Not_Null()
        {
            // arrange
            var       context   = NancyContextMock.Create(responseStatusCode: HttpStatusCode.Accepted);
            Exception exception = new Exception();

            // act
            var statusCode = context.GetStatusCode(exception);

            // assert
            Assert.Equal(500, statusCode);
        }
Ejemplo n.º 15
0
        public void LogData_Should_Work_With_Blacklist_And_Information()
        {
            // arrange
            var originIp        = "127.0.0.1";
            var protocolVersion = "1.1";

            var requestMethod  = "POST";
            var requestBody    = "{ \"test\" : \"123\", \"test2\" : \"123\" }";
            var requestUrl     = "http://localhost/test?query1=xpto1&query1=xpto2&query2=1&query3=";
            var requestHeaders = new Dictionary <string, IEnumerable <string> >
            {
                { "Content-Type", new string[] { "application/json" } },
                { "Accept", new string[] { "application/json", "text/xml" } }
            };

            var responseHeaders = new Dictionary <string, string>
            {
                { "Content-Type", "application/json" },
            };
            var responseStatusCode = HttpStatusCode.BadRequest;
            var Content            = "{ \"xpto\" : \"test\" }";

            var context = NancyContextMock.Create(
                requestMethod, requestUrl, requestBody, requestHeaders,
                responseStatusCode, Content, responseHeaders,
                originIp, protocolVersion);

            context.Items["test"] = "test";

            var config = new NancySerilogConfiguration
            {
                InformationTitle = "{Method} | {Host} | {Path} | {Ip} | {IsSuccessful} | {ProtocolVersion} | {StatusCode} | {StatusDescription} | {StatusCodeFamily} | {ElapsedMilliseconds}",
                Blacklist        = new string[] { "test" },
                Logger           = Log.Logger
            };

            var              logger    = new CommunicationLogger(config);
            IPipelines       pipelines = new Pipelines();
            TinyIoCContainer container = new TinyIoCContainer();

            container.Register <ICommunicationLogger>(logger);
            container.Register(PackUtils.JsonUtility.CamelCaseJsonSerializerSettings);

            pipelines.AddLogPipelines(container);

            // act
            logger.LogData(context);

            // assert
            Assert.Contains("[Information] \"POST\" | \"localhost\" | \"/test\" | \"127.0.0.1\" | False | \"1.1\" | 400 | \"BadRequest\" | \"4XX\" | -1", this.TestOutputHelper.Output);
        }
        public static void GetRequestKey_Should_Return_RequestKey_Value()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseHeaders: new Dictionary <string, string>
            {
                { "RequestKey", "b2f6759b-50fa-b7c3-7955-4fee4693482e" }
            });

            // act
            var requestKey = context.GetRequestKey();

            // assert
            Assert.Equal("b2f6759b-50fa-b7c3-7955-4fee4693482e", requestKey);
        }
        public static void GetRequestKey_Should_Return_Null_When_Context_Request_Headers_Not_Contains_RequestKey()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseHeaders: new Dictionary <string, string>
            {
                { "OtherKey", "b2f6759b-50fa-b7c3-7955-4fee4693482e" }
            });

            // act
            var requestKey = context.GetRequestKey();

            // assert
            Assert.Null(requestKey);
        }
        public static void GetExecutionTime_Should_Return_Execution_Time()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseHeaders: new Dictionary <string, string>
            {
                { "X-Internal-Time", "1230" }
            });

            // act
            var time = context.GetExecutionTime();

            // assert
            Assert.Equal("1230", time.ToString());
        }
        public static void GetExecutionTime_Should_Return_Default_When_Context_Request_Headers_Contains_Invalid_XInternalTime()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseHeaders: new Dictionary <string, string>
            {
                { "X-Internal-Time", "invalid" }
            });

            // act
            var time = context.GetExecutionTime();

            // assert
            Assert.Equal(-1, Convert.ToInt32(time));
        }
Ejemplo n.º 20
0
        public static void WriteStopwatchAndRequestKey_Should_Create_New_Request_Key()
        {
            // arrange
            NancyContext context = NancyContextMock.Create();

            // act
            PipelinesExtension.WriteStopwatchAndRequestKey(context);

            // assert
            Assert.NotNull(context);
            Assert.NotNull(context.Items);
            Assert.Equal(2, context.Items.Count);
            Assert.NotNull(context.Items["RequestKey"]);
            Assert.NotNull(context.Items["Stopwatch"]);
        }
        public static void GetQueryString_Should_Return_Query_Items()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                requestUrl: "http://url-without-query.com?test=1&test=2&www=123,456&xxx=test&yyy=&zzz");

            // act
            var query = context.GetQueryString();

            // assert
            Assert.Equal("1,2", query["test"]);
            Assert.Equal("123,456", query["www"]);
            Assert.Equal("", query["yyy"]);
            Assert.Equal("zzz", query["zzz"]);
        }
Ejemplo n.º 22
0
        public static void ReadStopwatchAndRequestKey_Should_Not_Returns_Headers_When_Response_Is_Null()
        {
            // arrange
            NancyContext context = NancyContextMock.Create();

            context.Response = null;

            // act
            PipelinesExtension.ReadStopwatchAndRequestKey(context);

            // assert
            Assert.NotNull(context);
            Assert.NotNull(context.Response);
            Assert.False(context.Response.Headers.ContainsKey("RequestKey"));
            Assert.False(context.Response.Headers.ContainsKey("X-Internal-Time"));
        }
Ejemplo n.º 23
0
        public static void HandleExceptions_Log_Exception()
        {
            // arrange
            var logger         = new CommunicationLoggerMock();
            var jsonSerializer = JsonUtility.CamelCaseJsonSerializerSettings;
            var exception      = new Exception();
            var context        = NancyContextMock.Create();

            // act
            var response = PipelinesExtension.HandleExceptions(context, exception, jsonSerializer, logger);

            // assert
            Assert.Null(response);
            Assert.Single(logger.Logs);
            Assert.True(bool.Parse(logger.Logs.First().Items["WorksWithException"].ToString()));
        }
        public static void GetIp_Should_Return_Ip_When_Context_Request_Headers_Contains_XForwarwedFor()
        {
            // arrange
            // arrange
            NancyContext context = NancyContextMock.Create(
                requestHeaders: new Dictionary <string, IEnumerable <string> >
            {
                { "X-Forwarded-For", new string[] { "233.233.233.233" } }
            });

            // act
            var ip = context.GetIp();

            // assert
            Assert.Equal("233.233.233.233", ip);
        }
Ejemplo n.º 25
0
        public static void WriteStopwatchAndRequestKey_Should_Not_Returns_Headers_When_Items_Not_Exists()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseContent: "some response",
                responseHeaders: new Dictionary <string, string>());

            // act
            PipelinesExtension.WriteStopwatchAndRequestKey(context);

            // assert
            Assert.NotNull(context);
            Assert.NotNull(context.Response);
            Assert.False(context.Response.Headers.ContainsKey("RequestKey"));
            Assert.False(context.Response.Headers.ContainsKey("X-Internal-Time"));
        }
        public static void GetResponseContent_Should_Return_Empty_When_Context_Response_Content_Is_Null()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseContent: null,
                responseHeaders: new Dictionary <string, string>
            {
                { "Content-Type", "text/plain" }
            });

            // act
            dynamic content = context.GetResponseContent();

            // assert
            Assert.Empty(content["raw_content"]);
        }
        public static void GetRequestBody_Should_Return_Value_As_String_When_ContentType_Is_ApplicationJson_And_Content_Is_An_Invalid_Json()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                requestBody: "{ \"sometest : \"somevalue\"}",
                requestHeaders: new Dictionary <string, IEnumerable <string> >
            {
                { "Content-Type", new string[] { "application/json" } }
            });

            // act
            var body = context.GetRequestBody(null);

            // assert
            Assert.Equal("{ \"sometest : \"somevalue\"}", body);
        }
        public static void GetResponseContent_Should_Return_Value_As_String_When_ContentType_Is_Not_ApplicationJson()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseContent: "0123456789",
                responseHeaders: new Dictionary <string, string>
            {
                { "Content-Type", "text/plain" }
            });

            // act
            dynamic content = context.GetResponseContent();

            // assert
            Assert.Equal("0123456789", content["raw_content"]);
        }
        public static void GetRequestBody_Should_Return_Empty_When_Context_Response_Content_Is_Null_Without_Blacklist()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                requestBody: null,
                requestHeaders: new Dictionary <string, IEnumerable <string> >
            {
                { "Content-Type", new string[] { "text/plain" } }
            });

            // act
            dynamic body = context.GetRequestBody(null);

            // assert
            Assert.Empty(body["raw_body"]);
        }
        public static void GetResponseContent_Should_Return_Value_As_String_When_ContentType_Is_ApplicationJson_And_Content_Is_An_Invalid_Json()
        {
            // arrange
            NancyContext context = NancyContextMock.Create(
                responseContent: "{ \"sometest : \"somevalue\"}",
                responseHeaders: new Dictionary <string, string>
            {
                { "Content-Type", "application/json" }
            });

            // act
            var content = context.GetResponseContent();

            // assert
            Assert.Equal("{ \"sometest : \"somevalue\"}", content);
        }