public void Return_Non_Matching_ETag_When_Server_Does_Not_Return_Not_Modified()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFunc("/api/GetPerson", context =>
                {
                    var id = context.GetRequestParameterValue <int>("id");
                    return(new { Id = id, FirstName = "Clark", LastName = "Kent" });
                });

                string initialETag;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/api/GetPerson?id=1"))
                {
                    initialETag = initialResponse.GetResponseHeader("ETag");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + "/api/GetPerson?id=2");
                request.Headers["If-None-Match"] = initialETag;

                // Act
                string eTag;
                using (var response = request.GetHttpWebResponse())
                {
                    eTag = response.GetResponseHeader("ETag");
                }

                // Visual Assertion
                Trace.WriteLine("1st ETag Header Value: " + initialETag);
                Trace.WriteLine("2nd ETag Header Value: " + eTag);

                // Assert
                Assert.That(initialETag != eTag);
            }
        }
        public void Return_Empty_Body_When_Server_Returns_Not_Modified_Http_Status_Code_304_Because_Of_A_Matching_ETag()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddDirectory("/", "www");

                string initialETag;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/ApiExplorer"))
                {
                    Trace.WriteLine("Initial Response Length: " + initialResponse.GetResponseString().Length);
                    initialETag = initialResponse.GetResponseHeader("ETag");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + "/ApiExplorer");
                request.Headers["If-None-Match"] = initialETag;

                // Act
                int responseLength;
                using (var response = request.GetHttpWebResponse())
                {
                    responseLength = response.GetResponseString().Length;
                }

                // Visual Assertion
                Trace.WriteLine("Not Modified Response Length: " + responseLength);

                // Assert
                Assert.That(responseLength == 0);
            }
        }
        public void Return_Empty_Body_When_Server_Returns_Not_Modified_Http_Status_Code_304_Because_Of_A_Matching_ETag()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFunc("/api/GetPerson", context =>
                {
                    var id = context.GetRequestParameterValue <int>("id");
                    return(new { Id = id, FirstName = "Clark", LastName = "Kent" });
                });

                string initialETag;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/api/GetPerson?id=1"))
                {
                    Trace.WriteLine("Initial Response Length: " + initialResponse.GetResponseString().Length);
                    initialETag = initialResponse.GetResponseHeader("ETag");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + "/api/GetPerson?id=1");
                request.Headers["If-None-Match"] = initialETag;

                // Act
                int responseLength;
                using (var response = request.GetHttpWebResponse())
                {
                    responseLength = response.GetResponseString().Length;
                }

                // Visual Assertion
                Trace.WriteLine("Not Modified Response Length: " + responseLength);

                // Assert
                Assert.That(responseLength == 0);
            }
        }
        public void Return_An_ETag_Header_When_An_Http_200_Response_Is_Returned()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFunc("/api/GetPerson", context =>
                {
                    var id = context.GetRequestParameterValue <int>("id");
                    return(new { Id = id, FirstName = "Clark", LastName = "Kent" });
                });

                // Act
                string eTag;
                using (var response = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/api/GetPerson?id=1"))
                {
                    eTag = response.GetResponseHeader("ETag");
                }

                // Visual Assertion
                Trace.WriteLine("ETag Header Value: " + eTag);

                // Assert
                Assert.NotNull(eTag);
            }
        }
        public void Return_Not_Modified_Http_Status_Code_304_When_Request_ETag_Matches_File_ETag()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFunc("/api/GetPerson", context =>
                {
                    var id = context.GetRequestParameterValue <int>("id");
                    return(new { Id = id, FirstName = "Clark", LastName = "Kent" });
                });

                string initialETag;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/api/GetPerson?id=1"))
                {
                    initialETag = initialResponse.GetResponseHeader("ETag");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + "/api/GetPerson?id=1");
                request.Headers["If-None-Match"] = initialETag;

                // Act
                HttpStatusCode responseCode;
                using (var response = request.GetHttpWebResponse())
                {
                    responseCode = response.StatusCode;
                }

                // Visual Assertion
                Trace.WriteLine("HTTP Status Code: " + responseCode);

                // Assert
                Assert.That(responseCode == HttpStatusCode.NotModified);
            }
        }
        public void Return_Non_Matching_ETag_When_Server_Does_Not_Return_Not_Modified()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <ETagTestsApi>("/api/ETagTestsApi");

                string initialETag;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/api/ETagTestsApi/GetPerson?id=1"))
                {
                    initialETag = initialResponse.GetResponseHeader("ETag");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + "/api/ETagTestsApi/GetPerson?id=2");
                request.Headers["If-None-Match"] = initialETag;

                // Act
                string eTag;
                using (var response = request.GetHttpWebResponse())
                {
                    eTag = response.GetResponseHeader("ETag");
                }

                // Visual Assertion
                Trace.WriteLine("1st ETag Header Value: " + initialETag);
                Trace.WriteLine("2nd ETag Header Value: " + eTag);

                // Assert
                Assert.That(initialETag != eTag);
            }
        }
        public void Return_Matching_ETag_When_Server_Returns_Not_Modified(string path)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFile(path, "www/ApiExplorer/index.html");

                string initialETag;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + path))
                {
                    initialETag = initialResponse.GetResponseHeader("ETag");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + path);
                request.Headers["If-None-Match"] = initialETag;

                // Act
                string eTag;
                using (var response = request.GetHttpWebResponse())
                {
                    eTag = response.GetResponseHeader("ETag");
                }

                // Visual Assertion
                Trace.WriteLine("ETag Header Value: " + eTag);

                // Assert
                Assert.That(initialETag == eTag);
            }
        }
        public void Return_Matching_Last_Modified_Header_When_Server_Returns_Not_Modified(string path)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFile(path, "www/ApiExplorer/index.html");

                string initialLastModified;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + path))
                {
                    initialLastModified = initialResponse.GetResponseHeader("Last-Modified");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + path);
                request.IfModifiedSince = DateTime.Parse(initialLastModified);

                // Act
                string lastModified;
                using (var response = request.GetHttpWebResponse())
                {
                    lastModified = response.GetResponseHeader("Last-Modified");
                }

                // Visual Assertion
                Trace.WriteLine("Last-Modified Header Value: " + lastModified);

                // Assert
                Assert.That(initialLastModified == lastModified);
            }
        }
        public void Return_Not_Modified_Http_Status_Code_304_When_Request_ETag_Matches_File_ETag()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <ETagTestsApi>("/api/ETagTestsApi");

                string initialETag;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/api/ETagTestsApi/GetPerson?id=1"))
                {
                    initialETag = initialResponse.GetResponseHeader("ETag");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + "/api/ETagTestsApi/GetPerson?id=1");
                request.Headers["If-None-Match"] = initialETag;

                // Act
                HttpStatusCode responseCode;
                using (var response = request.GetHttpWebResponse())
                {
                    responseCode = response.StatusCode;
                }

                // Visual Assertion
                Trace.WriteLine("HTTP Status Code: " + responseCode);

                // Assert
                Assert.That(responseCode == HttpStatusCode.NotModified);
            }
        }
        public void Return_Empty_Body_When_Server_Returns_Not_Modified_Http_Status_Code_304_Because_Of_A_Matching_Last_Modified_Header(string path)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFile(path, "www/ApiExplorer/index.html");

                string initialLastModified;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + path))
                {
                    Trace.WriteLine("Initial Response Length: " + initialResponse.GetResponseString().Length);
                    initialLastModified = initialResponse.GetResponseHeader("Last-Modified");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + path);
                request.IfModifiedSince = DateTime.Parse(initialLastModified);

                // Act
                int responseLength;
                using (var response = request.GetHttpWebResponse())
                {
                    responseLength = response.GetResponseString().Length;
                }

                // Visual Assertion
                Trace.WriteLine("Not Modified Response Length: " + responseLength);

                // Assert
                Assert.That(responseLength == 0);
            }
        }
        public void Return_Not_Modified_Http_Status_Code_304_When_Request_ETag_Matches_File_ETag(string path)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFile(path, "www/ApiExplorer/index.html");

                string initialETag;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + path))
                {
                    initialETag = initialResponse.GetResponseHeader("ETag");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + path);
                request.Headers["If-None-Match"] = initialETag;

                // Act
                HttpStatusCode responseCode;
                using (var response = request.GetHttpWebResponse())
                {
                    responseCode = response.StatusCode;
                }

                // Visual Assertion
                Trace.WriteLine("HTTP Status Code: " + responseCode);

                // Assert
                Assert.That(responseCode == HttpStatusCode.NotModified);
            }
        }
        public void Accept_A_Dictionary_Of_Int_ComplexType_As_Input()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <Echo>();

                var dictionary = new Dictionary <int, Echo.ComplexType>();

                for (int i = 1; i <= 30; i++)
                {
                    dictionary.Add(i, new Echo.ComplexType {
                        Id = i, Name = "Name " + i
                    });
                }

                string dictionaryJson = JsonConvert.SerializeObject(dictionary);

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/api/Echo/EchoDictionaryOfIntComplexType?dictionary=" + dictionaryJson);

                // Visual Assertion
                Trace.WriteLine(response);

                // Assert
                Assert.That(response.Contains(dictionaryJson));
            }
        }
        public void Accept_A_Complex_Type_ICollection_As_Input()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <Echo>();

                var someComplexTypeList = new List <Echo.ComplexType>();

                for (var i = 1; i <= 50; i++)
                {
                    someComplexTypeList.Add(new Echo.ComplexType {
                        Id = i, Name = "Name " + i
                    });
                }

                string someComplexTypeListJson = JsonConvert.SerializeObject(someComplexTypeList);

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/api/Echo/EchoComplexTypeICollection?someComplexTypeList=" + someComplexTypeListJson);

                // Visual Assertion
                Trace.WriteLine(response);

                // Assert
                Assert.That(response.Contains(someComplexTypeListJson));
            }
        }
Exemplo n.º 14
0
        public void Return_Not_Modified_Http_Status_Code_304_When_Request_Last_Modified_Header_Matches_File_Last_Modified_DateTime()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddDirectory("/", "www");

                string initialLastModified;
                using (var initialResponse = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/ApiExplorer"))
                {
                    initialLastModified = initialResponse.GetResponseHeader("Last-Modified");
                }

                var request = HttpHelper.GetHttpWebRequest(server.GetUrl() + "/ApiExplorer");
                request.IfModifiedSince = DateTime.Parse(initialLastModified);

                // Act
                HttpStatusCode responseCode;
                using (var response = request.GetHttpWebResponse())
                {
                    responseCode = response.StatusCode;
                }

                // Visual Assertion
                Trace.WriteLine("HTTP Status Code: " + responseCode);

                // Assert
                Assert.That(responseCode == HttpStatusCode.NotModified);
            }
        }
        public void Await_Tasks_And_Return_The_Result()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFunc("/GetStringAsync", context => Task.FromResult("Async"));

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/GetStringAsync");

                // Assert
                Assert.AreEqual(string.Concat("\"Async\""), response);
            }
        }
Exemplo n.º 16
0
        public void Null_The_HttpListener_On_Dispose()
        {
            // Arrange
            var server = NanoTestServer.Start();

            // Pre-Assert
            Assert.That(server.HttpListenerConfiguration.HttpListener != null);

            // Act
            server.Dispose();

            // Assert
            Assert.That(server.HttpListenerConfiguration == null);
        }
        public void Return_An_Http_200_When_The_Requested_Path_Does_Not_Exist_And_The_returnHttp404WhenFileWasNotFound_Parameter_Was_Set_To_False()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddDirectory("/", "www", returnHttp404WhenFileWasNotFound: false);

                // Act
                var response = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/NoSuchDirectory/");

                // Assert
                Assert.That(response.StatusCode == HttpStatusCode.OK);
            }
        }
        public void Return_A_File_That_Exists(string path, string expectedResponse)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddDirectory("/", "www");

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + path);

                // Assert
                Assert.That(response.Contains(expectedResponse));
            }
        }
        public void Return_The_Default_Index_Dot_Html_File_When_Requesting_A_Directory_Path(string path, string expectedResponse)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddDirectory("/", "www");

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + path);

                // Assert
                Assert.That(response.Contains(expectedResponse));
            }
        }
        public void Return_A_File_That_Exists(string path)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFile(path, "www/ApiExplorer/index.html");

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + path);

                // Assert
                Assert.That(response.Contains("Api Explorer"));
            }
        }
        public void Work_Without_Setting_An_Application_Path()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFunc("/SayHi", context => "Hi");

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/SayHi");

                // Visual Assertion
                Trace.WriteLine(response);

                // Assert
                Assert.That(response.Contains("Hi"));
            }
        }
        public void Return_An_Http_404_When_The_Requested_Path_Does_Not_Exist_And_The_returnHttp404WhenFileWasNotFound_Parameter_Was_Set_To_True(string path)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddDirectory("/", "www", returnHttp404WhenFileWasNotFound: true);

                // Act
                var response = HttpHelper.GetHttpWebResponse(server.GetUrl() + path);

                // Visual Assertion
                Trace.WriteLine(response.GetResponseString());

                // Assert
                Assert.That(response.StatusCode == HttpStatusCode.NotFound);
            }
        }
        public void Return_The_ResponseStreamWriter_Result_Of_A_Void_Task()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <Echo>();

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/api/Echo/EchoVoid");

                // Visual Assertion
                Trace.WriteLine(response);

                // Assert
                Assert.AreEqual("Void", response);
            }
        }
        public void Redirect_With_A_Trailing_Slash_When_The_Requested_Path_Exists_But_The_Url_Does_Not_Have_A_Trailing_Slash()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddDirectory("/", "www");

                // Act
                var response = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/ApiExplorer", allowAutoRedirect: false);
                var location = response.GetResponseHeader("location");

                // Visual Assertion
                Trace.WriteLine("Location Header Value: " + location);

                // Assert
                Assert.That(location == "/ApiExplorer/");
            }
        }
        public void Return_An_Last_Modified_Header_When_A_File_Is_Returned()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddDirectory("/", "www");

                // Act
                var response     = HttpHelper.GetHttpWebResponse(server.GetUrl() + "/ApiExplorer");
                var lastModified = response.GetResponseHeader("Last-Modified");

                // Visual Assertion
                Trace.WriteLine("Last-Modified Header Value: " + lastModified);

                // Assert
                Assert.NotNull(lastModified);
            }
        }
Exemplo n.º 26
0
        public void Ensure_Default_InnerClass_Path_Correct()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <NanoConfigurationShould.InnerClass>();
                var someBool = true.ToString().ToLower();

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/api/NanoConfigurationShould/InnerClass/InnerClassBool?somebool=" + someBool);

                // Visual Assertion
                Trace.WriteLine(response);

                // Assert
                Assert.That(response.Contains(someBool));
            }
        }
        public void Return_The_Guid_Result_Of_A_Task_Of_Type_Guid()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <Echo>();
                var someGuid = Guid.NewGuid();

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/api/Echo/EchoGuidAsync?someGuid=" + someGuid);

                // Visual Assertion
                Trace.WriteLine(response);

                // Assert
                Assert.AreEqual(string.Concat("\"", someGuid.ToString(), "\""), response);
            }
        }
        public void Return_An_Http_404_When_The_Requested_File_Is_Not_Found(string path)
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddFile("/ApiExplorer/index.html", "www/ApiExplorer/index.html");

                // Act
                var response = HttpHelper.GetHttpWebResponse(server.GetUrl() + path);

                // Visual Assertion
                Trace.WriteLine(response.StatusCode);
                Trace.WriteLine(response.GetResponseString());

                // Assert
                Assert.That(response.StatusCode == HttpStatusCode.NotFound);
            }
        }
        public void Return_The_Bool_Result_Of_A_Task_Of_Type_Bool()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <Echo>();
                var someBool = true.ToString().ToLower();

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/api/Echo/EchoBoolAsync?someBool=" + someBool);

                // Visual Assertion
                Trace.WriteLine(response);

                // Assert
                Assert.AreEqual(someBool, response);
            }
        }
        public void Accept_A_Decimal_As_Input()
        {
            using (var server = NanoTestServer.Start())
            {
                // Arrange
                server.NanoConfiguration.AddMethods <Echo>();
                var someDecimal = decimal.MaxValue;

                // Act
                var response = HttpHelper.GetResponseString(server.GetUrl() + "/api/Echo/EchoDecimal?someDecimal=" + someDecimal);

                // Visual Assertion
                Trace.WriteLine(response);

                // Assert
                Assert.That(response.Contains(someDecimal.ToString()));
            }
        }