public async Task PutAsync()
        {
            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingPut())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody(req => $"You PUT: {req.Body}"));

            AffinitySession session = client.CreateAffinitySession();

            using (HttpResponseMessage response = await session.PutAsync("/wat/123", new StringContent("Hi there, friend.")))
            {
                response.EnsureSuccessStatusCode();
                Assert.AreEqual("You PUT: Hi there, friend.", await response.Content.ReadAsStringAsync());
            }
        }
示例#2
0
        public async Task BaseAddress_with_trailing_slash_is_applied_correctly_to_each_request()
        {
            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingGet())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("GET Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat").UsingPost())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("POST Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingPut())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("PUT Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingDelete())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("DELETE Response"));

            string baseAddressWithoutTrailingSlash = "http://localhost:" + mockServer.Ports.First();
            string baseAddressWithTrailingSlash    = baseAddressWithoutTrailingSlash + "/";

            client = new PrizmDocRestClient(baseAddressWithTrailingSlash);

            AffinitySession session = client.CreateAffinitySession();

            using (HttpResponseMessage response = await session.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/wat/123")))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat/123", response.RequestMessage.RequestUri.ToString());
            }

            using (HttpResponseMessage response = await session.GetAsync("/wat/123"))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat/123", response.RequestMessage.RequestUri.ToString());
            }

            using (HttpResponseMessage response = await session.PostAsync("/wat", new StringContent("body")))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat", response.RequestMessage.RequestUri.ToString());
            }

            using (HttpResponseMessage response = await session.PutAsync("/wat/123", new StringContent("body")))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat/123", response.RequestMessage.RequestUri.ToString());
            }

            using (HttpResponseMessage response = await session.DeleteAsync("/wat/123"))
            {
                Assert.AreEqual(baseAddressWithoutTrailingSlash + "/wat/123", response.RequestMessage.RequestUri.ToString());
            }
        }
        public async Task DefaultRequestHeaders_are_correctly_applied_to_each_request()
        {
            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingGet())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("GET Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat").UsingPost())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("POST Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingPut())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("PUT Response"));

            mockServer
            .Given(Request.Create().WithPath("/wat/123").UsingDelete())
            .RespondWith(Response.Create().WithStatusCode(200).WithBody("DELETE Response"));

            string baseAddress = "http://localhost:" + mockServer.Ports.First();

            client = new PrizmDocRestClient(baseAddress)
            {
                DefaultRequestHeaders =
                {
                    { "Some-Header",       "An example value"      },
                    { "Some-Other-Header", "Another example value" },
                }
            };

            AffinitySession session = client.CreateAffinitySession();

            using (HttpResponseMessage response = await session.SendAsync(new HttpRequestMessage(HttpMethod.Get, "/wat/123")))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }

            using (HttpResponseMessage response = await session.GetAsync("/wat/123"))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }

            using (HttpResponseMessage response = await session.PostAsync("/wat", new StringContent("body")))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }

            using (HttpResponseMessage response = await session.PutAsync("/wat/123", new StringContent("body")))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }

            using (HttpResponseMessage response = await session.DeleteAsync("/wat/123"))
            {
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Header"));
                Assert.AreEqual("An example value", response.RequestMessage.Headers.GetValues("Some-Header").SingleOrDefault());
                Assert.IsTrue(response.RequestMessage.Headers.Contains("Some-Other-Header"));
                Assert.AreEqual("Another example value", response.RequestMessage.Headers.GetValues("Some-Other-Header").SingleOrDefault());
            }
        }