public void ProcessRequest(HttpContextBase context)
        {
            context.Response.ContentType = "application/json";
            context.Response.ContentEncoding = Encoding.UTF8;

            // Great article on verbs to use in API design http://stackoverflow.com/questions/2001773/understanding-rest-verbs-error-codes-and-authentication/2022938#2022938
            ApiRequest apiRequest;
            using (var apiRequestBuilder = new ApiRequestBuilder())
            {
                apiRequest = apiRequestBuilder.Build(context.Request);
            }

            var apiResponse = new ApiResponse();

            if (!string.IsNullOrEmpty(apiRequest.ErrorMessage))
            {
                using (var apiResponseBuilder = new ApiResponseBuilder())
                {
                    apiResponse = apiResponseBuilder.WithBody(
                            string.Format(apiRequest.ErrorMessage))
                            .WithHttpStatusCode(apiRequest.ErrorMessageHttpStatusCode);
                }
            }
            else
            {
                switch (apiRequest.HttpMethod)
                {
                    case "GET":
                        apiResponse = ProcessApiRequest.ProcessGetRequest(apiRequest);
                        break;
                    case "PUT":
                        apiResponse = ProcessApiRequest.ProcessPutRequest(apiRequest);
                        break;
                }

                if (apiResponse == null)
                {
                    using (var apiResponseBuilder = new ApiResponseBuilder())
                    {
                        apiResponse = apiResponseBuilder.WithBody(
                                string.Format("Error: API GET request returned NULL"))
                                .WithHttpStatusCode(HttpStatusCode.InternalServerError);
                    }
                }
            }
            context.Response.StatusCode = apiResponse.HttpStatusCode;
            context.Response.Write(apiResponse.Body);
        }
Exemple #2
0
        public void ApiRequestBuilderSetsErrorMessageHttpStatusCodeTo500AndProvidesAccurateErrorMessageIfRequestUrlIsInvalidAndContainsTooManySegments()
        {
            //Arrange
            var testable = new ApiRequestBuilder();

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(x => x.Request.Url)
                .Returns(new Uri("http://example.com/femah.axd/api/invalid/url/structure"));
            httpContextMock.SetupGet(x => x.Request.HttpMethod).Returns("GET");

            const string expectedJsonBody = "Error: The requested Url 'http://example.com/femah.axd/api/invalid/url/structure' does not match the expected format /femah.axd/api/[service]/[parameter].";

            //Act
            ApiRequest apiRequest = testable.Build(httpContextMock.Object.Request);

            //Asert
            Assert.AreEqual(HttpStatusCode.InternalServerError, apiRequest.ErrorMessageHttpStatusCode);
            Assert.AreEqual(expectedJsonBody, apiRequest.ErrorMessage);
        }
Exemple #3
0
        public void ApiRequestBuilderSetsErrorMessageHttpStatusCodeTo415AndProvidesAccurateErrorMessageIfContentTypeIsNotSetToApplicationJsonAndRequestIsAPut()
        {
            //Arrange
            var testable = new ApiRequestBuilder();

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(x => x.Request.Url)
                .Returns(new Uri("http://example.com/femah.axd/api/featureswitches/TestFeatureSwitch1"));
            httpContextMock.SetupGet(x => x.Request.HttpMethod).Returns("PUT");
            httpContextMock.SetupGet(x => x.Request.ContentType).Returns("incorrect/contenttype");

            const string expectedJsonBody = "Error: Content-Type 'incorrect/contenttype' of request is not supported, expecting 'application/json'.";

            //Act
            ApiRequest apiRequest = testable.Build(httpContextMock.Object.Request);

            //Asert
            Assert.AreEqual(HttpStatusCode.UnsupportedMediaType, apiRequest.ErrorMessageHttpStatusCode);
            Assert.AreEqual(expectedJsonBody, apiRequest.ErrorMessage);
        }
Exemple #4
0
        public void ApiRequestBuilderSetsApiRequestBodyIfHttpContextInputStreamIsNotNull()
        {
            //Arrange
            var testable = new ApiRequestBuilder();

            var httpContextMock = new Mock<HttpContextBase>();
            httpContextMock.Setup(x => x.Request.Url)
                .Returns(new Uri("http://example.com/femah.axd/api/featureswitches"));
            httpContextMock.SetupGet(x => x.Request.HttpMethod).Returns("GET");

            //Build the request Body in JSON
            const string expectedJsonBody = "{\"IsEnabled\":true,\"Name\":\"TestFeatureSwitch1\",\"FeatureType\":\"SimpleFeatureSwitch\",\"Description\":\"Define a short description of the feature switch type here.\",\"ConfigurationInstructions\":\"Add configuration context and instructions to be displayed in the admin UI\"}";
            var inputStream = new MemoryStream();
            var streamWriter = new StreamWriter(inputStream);
            streamWriter.Write(expectedJsonBody);
            streamWriter.Flush();

            httpContextMock.SetupGet(x => x.Request.InputStream).Returns(inputStream);

            //Act
            ApiRequest apiRequest = testable.Build(httpContextMock.Object.Request);

            //Assert
            Assert.AreEqual(expectedJsonBody, apiRequest.Body);
        }