public async Task TestGetFromJsonAsyncTextPlainUtf16Async()
        {
            string json = Person.Create().Serialize();
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    HttpResponseMessage response = await client.SendAsync(request);

                    Person per = Assert.IsType <Person>(await response.Content.ReadFromJsonAsync(typeof(Person)));
                    per.Validate();

                    request  = new HttpRequestMessage(HttpMethod.Get, uri);
                    response = await client.SendAsync(request);

                    per = await response.Content.ReadFromJsonAsync <Person>();
                    per.Validate();
                }
            },
                async server =>
            {
                var headers = new List <HttpHeaderData> {
                    new HttpHeaderData("Content-Type", "application/json; charset=utf-16")
                };
                await server.HandleRequestAsync(statusCode: HttpStatusCode.OK, headers: headers, bytes: Encoding.Unicode.GetBytes(json));
            });
        }
        public async Task TestGetFromJsonAsync()
        {
            const string          json    = @"{""Name"":""David"",""Age"":24}";
            HttpHeaderData        header  = new HttpHeaderData("Content-Type", "application/json");
            List <HttpHeaderData> headers = new List <HttpHeaderData> {
                header
            };

            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    Person per = (Person)await client.GetFromJsonAsync(uri, typeof(Person));
                    per.Validate();

                    per = (Person)await client.GetFromJsonAsync(uri.ToString(), typeof(Person));
                    per.Validate();

                    per = await client.GetFromJsonAsync <Person>(uri);
                    per.Validate();

                    per = await client.GetFromJsonAsync <Person>(uri.ToString());
                    per.Validate();
                }
            },
                server => server.HandleRequestAsync(content: json, headers: headers));
        }
        public async Task AllowNullRequesturlAsync()
        {
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    client.BaseAddress = uri;

                    Person per = Assert.IsType <Person>(await client.GetFromJsonAsync((string)null, typeof(Person)));
                    per        = Assert.IsType <Person>(await client.GetFromJsonAsync((Uri)null, typeof(Person)));

                    per = await client.GetFromJsonAsync <Person>((string)null);
                    per = await client.GetFromJsonAsync <Person>((Uri)null);
                }
            },
                async server => {
                List <HttpHeaderData> headers = new List <HttpHeaderData> {
                    new HttpHeaderData("Content-Type", "application/json")
                };
                string json = Person.Create().Serialize();

                await server.HandleRequestAsync(content: json, headers: headers);
            });
        }
 public async Task TestGetFromJsonAsyncUnsuccessfulResponseAsync()
 {
     await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
         async (handler, uri) =>
     {
         using (HttpClient client = new HttpClient(handler))
         {
             await Assert.ThrowsAsync <HttpRequestException>(() => client.GetFromJsonAsync(uri, typeof(Person)));
             await Assert.ThrowsAsync <HttpRequestException>(() => client.GetFromJsonAsync <Person>(uri));
         }
     },
         server => server.HandleRequestAsync(statusCode: HttpStatusCode.InternalServerError));
 }
 public async Task EnsureDefaultJsonSerializerOptionsAsync()
 {
     await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
         async (handler, uri) =>
     {
         using (HttpClient client = new HttpClient(handler))
         {
             var request = new HttpRequestMessage(HttpMethod.Get, uri);
             HttpResponseMessage response = await client.SendAsync(request);
             await response.Content.ReadFromJsonAsync(typeof(EnsureDefaultOptions));
         }
     },
         server => server.HandleRequestAsync(headers: _headers, content: "{}"));
 }
Exemple #6
0
        public async Task TestReadFromJsonNoContentTypeAsync()
        {
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    HttpResponseMessage response = await client.SendAsync(request);

                    await Assert.ThrowsAsync <NotSupportedException>(() => response.Content.ReadFromJsonAsync <Person>());
                }
            },
                server => server.HandleRequestAsync(content: "{}"));
        }
 public async Task EnsureDefaultJsonSerializerOptionsAsync()
 {
     await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
         async (handler, uri) =>
     {
         using (HttpClient client = new HttpClient(handler))
         {
             // EnsureDefaultOptions uses a JsonConverter where we validate the JsonSerializerOptions when not provided to JsonContent.Create.
             EnsureDefaultOptions dummyObj = new EnsureDefaultOptions();
             var request     = new HttpRequestMessage(HttpMethod.Post, uri);
             request.Content = JsonContent.Create(dummyObj);
             await SendAsync(client, request);
         }
     },
         server => server.HandleRequestAsync());
 }
        public async Task TestReadFromJsonNoMessageBodyAsync()
        {
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    HttpResponseMessage response = await client.SendAsync(request);

                    // As of now, we pass the message body to the serializer even when its empty which causes the serializer to throw.
                    JsonException ex = await Assert.ThrowsAsync <JsonException>(() => response.Content.ReadFromJsonAsync(typeof(Person)));
                    Assert.Contains("Path: $ | LineNumber: 0 | BytePositionInLine: 0", ex.Message);
                }
            },
                server => server.HandleRequestAsync(headers: _headers));
        }
Exemple #9
0
        public async Task TestDeleteFromJsonAsync()
        {
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    Person person   = Person.Create();
                    Type typePerson = typeof(Person);

                    object response = await client.DeleteFromJsonAsync(uri.ToString(), typeof(Person));
                    Assert.IsType <Person>(response).Validate();

                    response = await client.DeleteFromJsonAsync(uri, typeof(Person));
                    Assert.IsType <Person>(response).Validate();

                    response = await client.DeleteFromJsonAsync(uri.ToString(), typeof(Person), CancellationToken.None);
                    Assert.IsType <Person>(response).Validate();

                    response = await client.DeleteFromJsonAsync <Person>(uri, CancellationToken.None);
                    Assert.IsType <Person>(response).Validate();

                    response = await client.DeleteFromJsonAsync(uri.ToString(), JsonContext.Default.Person);
                    Assert.IsType <Person>(response).Validate();

                    response = await client.DeleteFromJsonAsync(uri, JsonContext.Default.Person);
                    Assert.IsType <Person>(response).Validate();

                    response = await client.DeleteFromJsonAsync(uri.ToString(), JsonContext.Default.Person, CancellationToken.None);
                    Assert.IsType <Person>(response).Validate();

                    response = await client.DeleteFromJsonAsync(uri, JsonContext.Default.Person, CancellationToken.None);
                    Assert.IsType <Person>(response).Validate();
                }
            },
                async server => {
                HttpRequestData request = await server.HandleRequestAsync();
                Assert.Equal("DELETE", request.Method);

                List <HttpHeaderData> headers = new List <HttpHeaderData> {
                    new HttpHeaderData("Content-Type", "application/json")
                };
                string json = Person.Create().Serialize();
                await server.HandleRequestAsync(content: json, headers: headers, statusCode: HttpStatusCode.Accepted);
            });
        }
 public async Task JsonContentMediaTypeValidateOnServerAsync()
 {
     await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
         async (handler, uri) =>
     {
         using (HttpClient client = new HttpClient(handler))
         {
             var request = new HttpRequestMessage(HttpMethod.Post, uri);
             MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse("foo/bar; charset=utf-8");
             request.Content = JsonContent.Create(Person.Create(), mediaType: mediaType);
             await SendAsync(client, request);
         }
     },
         async server => {
         HttpRequestData req = await server.HandleRequestAsync();
         Assert.Equal("foo/bar; charset=utf-8", req.GetSingleHeaderValue("Content-Type"));
     });
 }
Exemple #11
0
        public async Task TestGetFromJsonAsync(string json, bool containsQuotedNumbers)
        {
            HttpHeaderData        header  = new HttpHeaderData("Content-Type", "application/json");
            List <HttpHeaderData> headers = new List <HttpHeaderData> {
                header
            };

            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    Person per = (Person)await client.GetFromJsonAsync(uri, typeof(Person));
                    per.Validate();

                    per = (Person)await client.GetFromJsonAsync(uri.ToString(), typeof(Person));
                    per.Validate();

                    per = await client.GetFromJsonAsync <Person>(uri);
                    per.Validate();

                    per = await client.GetFromJsonAsync <Person>(uri.ToString());
                    per.Validate();

                    if (!containsQuotedNumbers)
                    {
                        per = (Person)await client.GetFromJsonAsync(uri, typeof(Person), JsonContext.Default);
                        per.Validate();

                        per = (Person)await client.GetFromJsonAsync(uri.ToString(), typeof(Person), JsonContext.Default);
                        per.Validate();

                        per = await client.GetFromJsonAsync <Person>(uri, JsonContext.Default.Person);
                        per.Validate();

                        per = await client.GetFromJsonAsync <Person>(uri.ToString(), JsonContext.Default.Person);
                        per.Validate();
                    }
                }
            },
                server => server.HandleRequestAsync(content: json, headers: headers));
        }
        public async Task HttpContentReturnValueIsNull()
        {
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    HttpResponseMessage response = await client.SendAsync(request);
                    object obj = await response.Content.ReadFromJsonAsync(typeof(Person));
                    Assert.Null(obj);

                    request    = new HttpRequestMessage(HttpMethod.Get, uri);
                    response   = await client.SendAsync(request);
                    Person per = await response.Content.ReadFromJsonAsync <Person>();
                    Assert.Null(per);
                }
            },
                server => server.HandleRequestAsync(headers: _headers, content: "null"));
        }
        public async Task SendQuotedCharsetAsync()
        {
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    JsonContent content = JsonContent.Create <Foo>(null);
                    content.Headers.ContentType.CharSet = "\"utf-8\"";

                    var request     = new HttpRequestMessage(HttpMethod.Post, uri);
                    request.Content = content;
                    await SendAsync(client, request);
                }
            },
                async server => {
                HttpRequestData req = await server.HandleRequestAsync();
                Assert.Equal("application/json; charset=\"utf-8\"", req.GetSingleHeaderValue("Content-Type"));
            });
        }
        public async Task HttpContentGetThenReadFromJsonAsync(string json)
        {
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    HttpResponseMessage response = await client.SendAsync(request);
                    object obj = await response.Content.ReadFromJsonAsync(typeof(Person));
                    Person per = Assert.IsType <Person>(obj);
                    per.Validate();

                    request  = new HttpRequestMessage(HttpMethod.Get, uri);
                    response = await client.SendAsync(request);
                    per      = await response.Content.ReadFromJsonAsync <Person>();
                    per.Validate();
                }
            },
                server => server.HandleRequestAsync(headers: _headers, content: json));
        }
        public async Task TestVariousMediaTypes(string mediaType)
        {
            List <HttpHeaderData> customHeaders = new List <HttpHeaderData>
            {
                new HttpHeaderData("Content-Type", $"{mediaType}; charset=\"utf-8\"")
            };

            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    HttpResponseMessage response = await client.SendAsync(request);

                    Person person = await response.Content.ReadFromJsonAsync <Person>();
                    person.Validate();
                }
            },
                server => server.HandleRequestAsync(headers: customHeaders, content: Person.Create().Serialize()));
        }
 public async Task ValidateUtf16IsTranscodedAsync()
 {
     await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
         async (handler, uri) =>
     {
         using (HttpClient client = new HttpClient(handler))
         {
             var request = new HttpRequestMessage(HttpMethod.Post, uri);
             MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-16");
             // Pass new options to avoid using the Default Web Options that use camelCase.
             request.Content = JsonContent.Create(Person.Create(), mediaType: mediaType, options: new JsonSerializerOptions());
             await SendAsync(client, request);
         }
     },
         async server => {
         HttpRequestData req = await server.HandleRequestAsync();
         Assert.Equal("application/json; charset=utf-16", req.GetSingleHeaderValue("Content-Type"));
         Person per = JsonSerializer.Deserialize <Person>(Encoding.Unicode.GetString(req.Body));
         per.Validate();
     });
 }
        public async Task TestGetFromJsonThrowOnInvalidCharSetAsync()
        {
            List <HttpHeaderData> customHeaders = new List <HttpHeaderData>
            {
                new HttpHeaderData("Content-Type", "application/json; charset=\"foo-bar\"")
            };

            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    HttpResponseMessage response = await client.SendAsync(request);

                    InvalidOperationException ex = await Assert.ThrowsAsync <InvalidOperationException>(() => response.Content.ReadFromJsonAsync <Person>());
                    Assert.IsType <ArgumentException>(ex.InnerException);
                }
            },
                server => server.HandleRequestAsync(headers: customHeaders, content: Person.Create().Serialize()));
        }
        public async Task TestJsonContentNullContentTypeAsync()
        {
            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Post, uri);
                    MediaTypeHeaderValue mediaType = MediaTypeHeaderValue.Parse("application/json; charset=utf-16");
                    JsonContent content            = JsonContent.Create(Person.Create(), mediaType: mediaType);
                    content.Headers.ContentType    = null;

                    request.Content = content;
                    await SendAsync(client, request);
                }
            },
                async server => {
                HttpRequestData req = await server.HandleRequestAsync();
                Assert.Equal(0, req.GetHeaderValueCount("Content-Type"));
            });
        }
Exemple #19
0
        [InlineData("application/problem+foo+json")] // more than one '+' not allowed.
        public async Task TestInvalidMediaTypeAsync(string mediaType)
        {
            List <HttpHeaderData> customHeaders = new List <HttpHeaderData>
            {
                new HttpHeaderData("Content-Type", $"{mediaType}; charset=\"utf-8\"")
            };

            await HttpMessageHandlerLoopbackServer.CreateClientAndServerAsync(
                async (handler, uri) =>
            {
                using (HttpClient client = new HttpClient(handler))
                {
                    var request = new HttpRequestMessage(HttpMethod.Get, uri);
                    HttpResponseMessage response = await client.SendAsync(request);

                    Exception ex = await Assert.ThrowsAsync <NotSupportedException>(() => response.Content.ReadFromJsonAsync <Person>());
                    Assert.Contains("application/json", ex.Message);
                    Assert.Contains("application/+json", ex.Message);
                }
            },
                server => server.HandleRequestAsync(headers: customHeaders, content: Person.Create().Serialize()));
        }