public void ReadFromStreamShouldReturnNullIfContentCannotBeParsed()
        {
            var stream       = new MemoryStream();
            var streamWriter = new StreamWriter(stream);

            streamWriter.WriteLine(@"{""Integer"":1,""RequiredString"":""Test""");
            streamWriter.Flush();

            var result = FormattersHelper.ReadFromStream <RequestModel>(stream, ContentType.ApplicationJson, Encoding.UTF8);

            Assert.Null(result);
        }
        public void ReadFromStreamShouldThrowExceptionIfNoModelIsDifferentType()
        {
            Test.AssertException <InvalidDataException>(
                () =>
            {
                var stream       = new MemoryStream();
                var streamWriter = new StreamWriter(stream);
                streamWriter.WriteLine(@"test");
                streamWriter.Flush();

                FormattersHelper.ReadFromStream <RequestModel>(stream, ContentType.TextPlain, Encoding.UTF8);
            },
                "Expected stream content to be formatted to RequestModel when using 'text/plain', but instead received String.");
        }
        public void ReadFromStreamShouldThrowExceptionIfNoFormatterIsFound()
        {
            Test.AssertException <NullReferenceException>(
                () =>
            {
                var stream       = new MemoryStream();
                var streamWriter = new StreamWriter(stream);
                streamWriter.WriteLine(@"test");
                streamWriter.Flush();

                FormattersHelper.ReadFromStream <RequestModel>(stream, ContentType.TextHtml, Encoding.UTF8);
            },
                "Formatter able to process 'text/html' could not be resolved from the services provider. Before running this test case, the formatter should be registered in the 'StartsFrom' method and cannot be null.");
        }
        /// <inheritdoc />
        public IAndHttpResponseTestBuilder WithBody <TBody>(TBody body, string contentType, Encoding encoding)
        {
            var parsedBody = FormattersHelper.ReadFromStream <TBody>(this.httpResponse.Body, contentType, encoding);

            if (Reflection.AreNotDeeplyEqual(body, parsedBody))
            {
                this.ThrowNewHttpResponseAssertionException(
                    "body",
                    "to be the given object",
                    "in fact it was different");
            }

            return(this.WithContentType(contentType));
        }
        public void ReadFromStreamShouldWorkCorrectly()
        {
            var stream       = new MemoryStream();
            var streamWriter = new StreamWriter(stream);

            streamWriter.WriteLine(@"{""Integer"":1,""RequiredString"":""Test""}");
            streamWriter.Flush();

            var result = FormattersHelper.ReadFromStream <RequestModel>(stream, ContentType.ApplicationJson, Encoding.UTF8);

            Assert.NotNull(result);
            Assert.Equal(1, result.Integer);
            Assert.Equal("Test", result.RequiredString);
            Assert.Null(result.NonRequiredString);
            Assert.Equal(0, result.NotValidateInteger);
        }
        /// <inheritdoc />
        public IAndHttpResponseTestBuilder WithBodyOfType <TBody>(string contentType, Encoding encoding)
        {
            try
            {
                FormattersHelper.ReadFromStream <TBody>(this.httpResponse.Body, contentType, encoding);
            }
            catch (InvalidDataException)
            {
                this.ThrowNewHttpResponseAssertionException(
                    "body",
                    $"to be of {typeof(TBody).ToFriendlyTypeName()} type when using '{contentType}'",
                    $"in fact it was not");
            }

            return(this.WithContentType(contentType));
        }