コード例 #1
0
        public HttpResponseMessage GetStringContent(JsonCollectionType jsonCollectionType)
        {
            var response = new HttpResponseMessage();

            if (RequestURL != null)
            {
                response.StatusCode = HttpStatusCode.OK;
                response.Content    = new StringContent(GetJsonCollectionTypeResponse(jsonCollectionType));
            }
            else if (RequestURL != null && RequestURL.IndexOf(ConstHttpStatus400) >= 0)
            {
                response.StatusCode = HttpStatusCode.BadRequest;
            }
            else if (RequestURL != null && RequestURL.IndexOf(ConstHttpStatus404) >= 0)
            {
                response.StatusCode = HttpStatusCode.NotFound;
                response.Content    = new StringContent(
                    "{ \"timestampUtc\": \"2018-08-26T06:16:38.3973386Z\", " +
                    " \"exceptionType\": \"EntityNotFoundException\", " +
                    " \"httpStatusCode\": 404, " +
                    " \"httpStatus\": \"NotFound\" } ");
            }

            return(response);
        }
コード例 #2
0
        private string GetJsonCollectionTypeResponse(JsonCollectionType jsonCollectionType)
        {
            string resp = string.Empty;

            switch (jsonCollectionType)
            {
            case JsonCollectionType.Album:
                resp = "[{" +
                       "  \"userId\": \"1\"," +
                       "  \"id\": \"1\"," +
                       "  \"title\": \"quidem molestiae enim\"" +
                       "}]";
                break;

            case JsonCollectionType.Photo:
                resp = "[{" +
                       "  \"albumId\": \"1\"," +
                       "  \"id\": \"1\"," +
                       "  \"title\": \"accusamus beatae ad facilis cum similique qui sunt\"," +
                       "  \"url\": \"https://via.placeholder.com/600/92c952\"" +
                       "}]";
                break;

            case JsonCollectionType.Comment:
                resp = "[{" +
                       "  \"postId\": \"1\"," +
                       "  \"id\": \"1\"," +
                       "  \"email\": \"[email protected]\"," +
                       "  \"name\": \"quidem molestiae enim\"" +
                       "}]";
                break;
            }
            return(resp);
        }
コード例 #3
0
        public Mock <HttpMessageHandler> CreateMockHttpMessageStub(JsonCollectionType jsonCollectionType)
        {
            var handlerMock = new Mock <HttpMessageHandler>(MockBehavior.Default);

            handlerMock
            .Protected()
            // Setup the PROTECTED method to mock
            .Setup <Task <HttpResponseMessage> >(
                "SendAsync",
                ItExpr.IsAny <HttpRequestMessage>(),
                ItExpr.IsAny <CancellationToken>()
                )
            // prepare the expected response of the mocked http call
            .ReturnsAsync(GetStringContent(jsonCollectionType))
            .Verifiable();
            return(handlerMock);
        }