Example #1
0
        public void Convert_WithJsonBody_CallsConvertOnHttpBodyContentMapperAndAssignsContents()
        {
            const string contentTypeString = "application/json";
            var          response          = new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString }
                },
                Body = new
                {
                    Test  = "tester",
                    Test2 = 1
                }
            };
            var jsonBody        = "{\"Test\":\"tester\",\"Test2\":1}";
            var httpBodyContent = new HttpBodyContent(content: Encoding.UTF8.GetBytes(jsonBody), contentType: new MediaTypeHeaderValue(contentTypeString)
            {
                CharSet = "utf-8"
            });

            var mockHttpBodyContentMapper = Substitute.For <IHttpBodyContentMapper>();

            mockHttpBodyContentMapper.Convert(body: Arg.Any <object>(), headers: response.Headers)
            .Returns(httpBodyContent);

            var mapper = new NancyResponseMapper(mockHttpBodyContentMapper);

            var result = mapper.Convert(response);

            string content;

            using (var stream = new MemoryStream())
            {
                result.Contents(stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    content = reader.ReadToEnd();
                }
            }

            Assert.Equal(jsonBody, content);
            mockHttpBodyContentMapper.Received(1).Convert(body: Arg.Any <object>(), headers: response.Headers);
        }
Example #2
0
        public void Convert_WithPlainTextBody_CallsConvertOnHttpBodyContentMapperAndAssignsContents()
        {
            const string contentTypeString = "text/plain";
            var          response          = new ProviderServiceResponse
            {
                Status  = 200,
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString }
                },
                Body = "This is a plain body"
            };

            var httpBodyContent = new HttpBodyContent(new MediaTypeHeaderValue(contentTypeString)
            {
                CharSet = "utf-8"
            });

            httpBodyContent.GenerateContent(response.Body);

            var mockHttpBodyContentMapper = Substitute.For <IHttpBodyContentMapper>();

            mockHttpBodyContentMapper.Convert(body: Arg.Any <object>(), headers: response.Headers)
            .Returns(httpBodyContent);

            var mapper = new NancyResponseMapper(mockHttpBodyContentMapper);

            var result = mapper.Convert(response);

            string content;

            using (var stream = new MemoryStream())
            {
                result.Contents(stream);
                stream.Position = 0;
                using (var reader = new StreamReader(stream))
                {
                    content = reader.ReadToEnd();
                }
            }

            Assert.Equal(response.Body, content);
            mockHttpBodyContentMapper.Received(1).Convert(body: Arg.Any <object>(), headers: response.Headers);
        }