public void ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_ReturnsNull()
        {
            var content = new StringContent("{}");
            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Equal(null, content.ReadAsAsync<List<string>>(formatters).Result);
        }
        public void ReadAsAsyncOfT_WhenNoMatchingFormatterFoundForContentWithNoMediaType_Throws()
        {
            var content = new StringContent("{}");
            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Throws<InvalidOperationException>(() => content.ReadAsAsync<List<string>>(formatters),
                "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type ''undefined''.");
        }
Esempio n. 3
0
        public async Task ReadAsAsyncOfT_InvokesFormatterEvenIfContentLengthIsZero()
        {
            var content = new StringContent("");

            _formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
            _formatterMock.Object.SupportedMediaTypes.Add(content.Headers.ContentType);
            _formatterMock
            .Setup(
                f =>
                f.ReadFromStreamAsync(
                    It.IsAny <Type>(),
                    It.IsAny <Stream>(),
                    It.IsAny <HttpContent>(),
                    It.IsAny <IFormatterLogger>()
                    )
                )
            .Returns(Task.FromResult <object>(result: null));

            await content.ReadAsAsync <string>(_formatters);

            _formatterMock.Verify(
                f =>
                f.ReadFromStreamAsync(
                    typeof(string),
                    It.IsAny <Stream>(),
                    content,
                    It.IsAny <IFormatterLogger>()
                    ),
                Times.Once()
                );
        }
        public Task ReadAsAsyncOfT_cancellationToken_PassesCancellationTokenFurther()
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.Cancel();
            HttpContent content = new StringContent("42", Encoding.Default, "application/json");

            return(Assert.ThrowsAsync <OperationCanceledException>(() => content.ReadAsAsync <int>(cts.Token)));
        }
        public async Task ReadAsAsync_type_cancellationToken_PassesCancellationTokenFurther()
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            cts.Cancel();
            HttpContent content = new StringContent("42", Encoding.Default, "application/json");

            await Assert.ThrowsAsync <OperationCanceledException>(() => content.ReadAsAsync(typeof(int), cts.Token));
        }
        public void ReadAsAsyncOfT_WhenTypeIsValueTypeAndNoMediaType_ReturnsDefault()
        {
            var content = new StringContent("123456");

            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Equal(0, content.ReadAsAsync <int>(formatters).Result);
        }
        public void ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_ReturnsNull()
        {
            var content = new StringContent("{}");

            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Equal(null, content.ReadAsAsync <List <string> >(formatters).Result);
        }
        public void ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_Throws()
        {
            var content = new StringContent("{}");
            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Throws<UnsupportedMediaTypeException>(() => content.ReadAsAsync<List<string>>(formatters),
                "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'application/octet-stream'.");
        }
        public void ReadAsAsyncOfT_WhenNoMatchingFormatterFound_Throws()
        {
            var content = new StringContent("{}");
            content.Headers.ContentType = _mediaType;
            content.Headers.ContentType.CharSet = "utf-16";
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Throws<InvalidOperationException>(() => content.ReadAsAsync<List<string>>(formatters),
                "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'foo/bar'.");
        }
        public void ReadAsAsyncOfT_WhenNoMatchingFormatterFoundForContentWithNoMediaType_Throws()
        {
            var content = new StringContent("{}");

            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Throws <InvalidOperationException>(() => content.ReadAsAsync <List <string> >(formatters),
                                                      "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type ''undefined''.");
        }
        public void ReadAsAsyncOfT_WhenTypeIsValueTypeAndNoMediaType_Throws()
        {
            var content = new StringContent("123456");

            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Throws <UnsupportedMediaTypeException>(() => content.ReadAsAsync <int>(formatters),
                                                          "No MediaTypeFormatter is available to read an object of type 'Int32' from content with media type 'application/octet-stream'.");
        }
Esempio n. 12
0
        public static void ReadingFormData()
        {
            var content = new StringContent("a[]=1&a[]=5&a[]=333", Encoding.UTF8, "application/x-www-form-urlencoded");

            //  var body = await content.ReadAsFormDataAsync();
            var body = content.ReadAsAsync<JObject>().Result;
            
            // Verify that the data we read was the same as we submitted to the `StringContent`.
            var arr = (JArray)body["a"];
            Helpers.AssertEquality(Helpers.__, arr.ToString()); 
        }
        public void ReadAsAsyncOfT_WhenNoMatchingFormatterFound_Throws()
        {
            var content = new StringContent("{}");

            content.Headers.ContentType         = _mediaType;
            content.Headers.ContentType.CharSet = "utf-16";
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Throws <InvalidOperationException>(() => content.ReadAsAsync <List <string> >(formatters),
                                                      "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'foo/bar'.");
        }
        public void ReadAsAsyncOfT_InvokesFormatterEvenIfContentLengthIsZero()
        {
            var content = new StringContent("");

            _formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
            _formatterMock.Object.SupportedMediaTypes.Add(content.Headers.ContentType);

            var result = content.ReadAsAsync <string>(_formatters);

            result.WaitUntilCompleted();
            _formatterMock.Verify(f => f.ReadFromStreamAsync(typeof(string), It.IsAny <Stream>(), content.Headers, It.IsAny <IFormatterLogger>()), Times.Once());
        }
        public async Task ReadAsAsyncOfT_WhenTypeIsReferenceTypeAndNoMediaType_Throws()
        {
            var content = new StringContent("{}");

            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new MockMediaTypeFormatter {
                                                            CallBase = true
                                                        } };

            var exception = await
                            Assert.ThrowsAsync <UnsupportedMediaTypeException>(() =>
                                                                               content.ReadAsAsync <List <string> >(formatters, _cancellationToken));

            Assert.Equal(
                "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'application/octet-stream'.",
                exception.Message);
        }
        public async Task ReadAsAsyncOfT_WhenNoMatchingFormatterFound_Throws()
        {
            var content = new StringContent("{}");

            content.Headers.ContentType         = _mediaType;
            content.Headers.ContentType.CharSet = "utf-16";
            var formatters = new MediaTypeFormatter[] { new MockMediaTypeFormatter {
                                                            CallBase = true
                                                        } };

            var exception = await
                            Assert.ThrowsAsync <UnsupportedMediaTypeException>(() =>
                                                                               content.ReadAsAsync <List <string> >(formatters, _cancellationToken));

            Assert.Equal(
                "No MediaTypeFormatter is available to read an object of type 'List`1' from content with media type 'foo/bar'.",
                exception.Message);
        }
        public void ReadAsAsyncOfT_InvokesFormatterEvenIfContentLengthIsZero()
        {
            var content = new StringContent("");
            _formatterMock.Setup(f => f.CanReadType(typeof(string))).Returns(true);
            _formatterMock.Object.SupportedMediaTypes.Add(content.Headers.ContentType);

            var result = content.ReadAsAsync<string>(_formatters);

            result.WaitUntilCompleted();
            _formatterMock.Verify(f => f.ReadFromStreamAsync(typeof(string), It.IsAny<Stream>(), content.Headers, It.IsAny<IFormatterLogger>()), Times.Once());
        }
        public void ReadAsAsyncOfT_WhenTypeIsValueTypeAndNoMediaType_ReturnsDefault()
        {
            var content = new StringContent("123456");
            content.Headers.ContentType = null;
            var formatters = new MediaTypeFormatter[] { new JsonMediaTypeFormatter() };

            Assert.Equal(0, content.ReadAsAsync<int>(formatters).Result);
        }
        public void ReadAsAsyncOfT_cancellationToken_PassesCancellationTokenFurther()
        {
            CancellationTokenSource cts = new CancellationTokenSource();
            cts.Cancel();
            HttpContent content = new StringContent("42", Encoding.Default, "application/json");

            Assert.Throws<TaskCanceledException>(() => content.ReadAsAsync<int>(cts.Token).Wait());
        }