Esempio n. 1
0
        public async Task Request_Get_RelativeTemplateUri()
        {
            HttpClient client = JsonTestClients.ExpectJson(
                new Uri(BaseUri, "foo/1234/bar?diddly=bonk"), HttpMethod.Get,
                responseBody: "Success!"
                );

            using (client)
            {
                HttpRequest request =
                    BaseRequest.WithRelativeUri("foo/{variable}/bar")
                    .WithQueryParameter("diddly", "bonk")
                    .WithTemplateParameter("variable", 1234)
                    .WithTemplateParameter("diddly", "bonk")
                    .UseJson().ExpectJson();

                using (HttpResponseMessage response = await client.GetAsync(request))
                {
                    Assert.True(response.IsSuccessStatusCode);
                    Assert.NotNull(response.Content);
                    Assert.Equal(WellKnownMediaTypes.Json, response.Content.Headers.ContentType.MediaType);

                    string responseBody = await response.ReadContentAsAsync <string>();

                    Assert.Equal("Success!", responseBody);
                }
            }
        }
Esempio n. 2
0
        public async Task Request_PostAsJson_RelativeUri()
        {
            HttpClient client = JsonTestClients.ExpectJson(
                new Uri(BaseUri, "foo/bar"), HttpMethod.Post, responseBody: 1234,
                assertion: async request =>
            {
                MessageAssert.AcceptsMediaType(request, WellKnownMediaTypes.Json);

                await MessageAssert.BodyIsAsync(request, "\"1234\"");
            }
                );

            using (client)
            {
                HttpRequest request =
                    BaseRequest.WithRelativeUri("foo/bar")
                    .UseJson().ExpectJson();

                int responseBody = await
                                   client.PostAsJsonAsync(request,
                                                          postBody : 1234.ToString()
                                                          )
                                   .ReadContentAsAsync <int>();

                Assert.Equal(1234, responseBody);
            }
        }
Esempio n. 3
0
        public async Task Request_Post_RelativeUri_ExpectJson()
        {
            HttpClient client = JsonTestClients.ExpectJson(
                new Uri(BaseUri, "foo/bar"), HttpMethod.Post,
                responseBody: "Success!",
                assertion: async request =>
            {
                MessageAssert.AcceptsMediaType(request, WellKnownMediaTypes.Json);

                await MessageAssert.BodyIsAsync(request,
                                                "{\"Foo\":\"Bar\",\"Baz\":1234}"
                                                );
            }
                );

            using (client)
            {
                HttpRequest request =
                    BaseRequest.WithRelativeUri("foo/bar")
                    .UseJson().ExpectJson();

                HttpResponseMessage response = await
                                               client.PostAsJsonAsync(request,
                                                                      postBody : new
                {
                    Foo = "Bar",
                    Baz = 1234
                }
                                                                      );

                using (response)
                {
                    Assert.True(response.IsSuccessStatusCode);
                    Assert.NotNull(response.Content?.Headers?.ContentType);
                    Assert.Equal(WellKnownMediaTypes.Json, response.Content.Headers.ContentType.MediaType);

                    string responseBody = await response.ReadContentAsAsync <string>();

                    Assert.Equal("Success!", responseBody);
                }
            }
        }