예제 #1
0
        // Adds a service and returns it's ID.
        //
        // Do not use this to test the ServiceFunctions.AddService method.
        // This was designed to help other tests and assumes that the
        // ServiceFunctions.AddService has already been tested.
        private static string AddService(string method, string path)
        {
            var bodyString = @"# BodyString
# Request
Method: {0}
Path: {1}

# Response
ContentType: application/json
StatusCode: 200

# Body
{""color"":""Red""}";

            var request = new TestHttpRequest()
            {
                Method     = "POST",
                Path       = "/__vs/services",
                BodyString = bodyString
                             .Replace("{0}", method)
                             .Replace("{1}", path)
            };
            var response = new TestHttpResponse();
            var context  = new TestHttpContext(request, response);

            Task next() => Task.CompletedTask;

            ServiceFunctions.AddService(context, next).Wait();
            return(response.ReadBody());
        }
예제 #2
0
        public void ServiceFunctions_QueryService_ReturnsServiceStats()
        {
            // Arrange
            var serviceId = AddService("GET", "/api/serviceToBeQueried/1");

            InvokeService("GET", "/api/serviceToBeQueried/1", "Lorem ipsum dolor");
            InvokeService("GET", "/api/serviceToBeQueried/1", "sit amet, consectetur adipiscing");

            var request = new TestHttpRequest()
            {
                Method = "GET",
                Path   = "/__vs/services/" + serviceId
            };
            var response = new TestHttpResponse();
            var context  = new TestHttpContext(request, response);

            Task next() => Task.CompletedTask;

            // Act
            ServiceFunctions.QueryService(context, next).Wait();

            // Assert
            Assert.AreEqual(200, response.StatusCode);
            Assert.AreEqual("text/plain", response.ContentType);
            var expectedResponseBody = new StringBuilder()
                                       .AppendLine($"CallCount: 2")
                                       .AppendLine()
                                       .AppendLine("LastRequestBody:")
                                       .Append("sit amet, consectetur adipiscing")
                                       .ToString();

            Assert.AreEqual(expectedResponseBody, response.ReadBody());
        }
예제 #3
0
        public void ServiceFunctions_AddService_ReturnsOkAndServiceId()
        {
            // Arrange
            #region BodyString
            var bodyString = @"# BodyString
Method: GET
Path: /api/things/53";
            #endregion

            var request = new TestHttpRequest()
            {
                Method     = "POST",
                Path       = "/__vs/services",
                BodyString = bodyString
            };
            var response = new TestHttpResponse();
            var context = new TestHttpContext(request, response);
            Task next() => Task.CompletedTask;

            // Act
            ServiceFunctions.AddService(context, next).Wait();

            // Assert
            Assert.AreEqual(200, response.StatusCode);
            Assert.AreEqual("text/plain", response.ContentType);
            Assert.IsTrue(Guid.TryParse(response.ReadBody(), out Guid serviceId));
            Assert.AreNotEqual(Guid.Empty, serviceId);
        }
예제 #4
0
        public void Service_WriteResponseTo_WritesResponse()
        {
            // Arrange
            var desc = new ServiceDesc()
            {
                Method      = "GET",
                Path        = "/api/resource?x=1",
                ContentType = "application/json",
                StatusCode  = "200",
                Body        = "Lorem ipsum dolor sit amet, consectetur adipiscing"
            };
            var service = new Service(desc);

            var httpResponse = new TestHttpResponse();

            // Act
            service.WriteResponseTo(httpResponse).Wait();

            // Assert
            Assert.AreEqual(200, httpResponse.StatusCode);
            Assert.AreEqual("application/json", httpResponse.ContentType);
            Assert.AreEqual(
                "Lorem ipsum dolor sit amet, consectetur adipiscing",
                httpResponse.ReadBody()
                );
        }
예제 #5
0
        public void ServiceFunctions_InvokeService_InvokesAService()
        {
            // Arrange
            AddService("GET", "/api/things/8bd5c2be-5b8b-4e8d-97bb-cbb9ac8c5066");

            var request = new TestHttpRequest()
            {
                Method     = "GET",
                Path       = "/api/things/8bd5c2be-5b8b-4e8d-97bb-cbb9ac8c5066",
                BodyString = string.Empty
            };
            var response = new TestHttpResponse();
            var context  = new TestHttpContext(request, response);

            Task next() => Task.CompletedTask;

            // Act
            ServiceFunctions.InvokeService(context, next).Wait();

            // Assert
            Assert.AreEqual(200, response.StatusCode);
            Assert.AreEqual("application/json", response.ContentType);
            Assert.AreEqual(@"{""color"":""Red""}", response.ReadBody());
        }