예제 #1
0
        public async Task ShouldPutSuccessfulHttpResponseWithPayload()
        {
            // arrange
            var          datetime   = new DateTime(2013, 9, 1, 10, 9, 0);
            const string requestUri = "/fake";
            var          payload    = new FakePayload
            {
                Id               = 190,
                SomeArray        = new[] { "lol1", "lol2", "lol3" },
                DateTimeOffset   = new DateTimeOffset(datetime, TimeSpan.FromHours(8)),
                NonNullDateTime  = datetime,
                Note             = "Note1",
                Token            = Guid.NewGuid(),
                NullableDateTime = null
            };
            FakePayload       result            = null;
            CancellationToken cancellationToken = CancellationToken.None;
            var webHostBuilder = new WebHostBuilder()
                                 .Configure(app =>
            {
                app.Run(async context =>
                {
                    FakePayload content = null;
                    using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8))
                    {
                        content = JsonConvert.DeserializeObject <FakePayload>(reader.ReadToEnd());
                    }
                    context.Response.ContentType = "application/json";
                    var serialized = JsonConvert.SerializeObject(content);
                    await context.Response.WriteAsync(serialized, cancellationToken);
                });
            });
            TestServer server = new TestServer(webHostBuilder);
            HttpClient client = server.CreateClient();

            // act
            result = await client
                     .Request()
                     .Put(requestUri, new JsonContent(payload))
                     .Query("id", payload.Id)
                     .Query("note", payload.Note)
                     .AsJsonAsync <FakePayload>(cancellationToken);

            // assert
            Assert.NotNull(result);
            Assert.True(result.Id == payload.Id);
            Assert.True(result.Note == payload.Note);
            Assert.True(result.DateTimeOffset == payload.DateTimeOffset);
            Assert.True(result.SomeArray.Length == 3);
            Assert.True(result.SomeArray[0] == "lol1");
            Assert.True(result.SomeArray[1] == "lol2");
            Assert.True(result.SomeArray[2] == "lol3");
        }
예제 #2
0
        public void ShouldBuildPostHttpMethodWithJsonPayload()
        {
            // arrange
            const string url     = "http://fakeurl/";
            var          payload = new FakePayload
            {
                Id = 10,
                NonNullDateTime  = new DateTime(2010, 1, 1, 10, 00, 00),
                Note             = "Note1",
                NullableDateTime = new DateTime(2012, 1, 1, 1, 0, 0, 0),
                Token            = Guid.NewGuid()
            };
            var httpClient = new MockedHttpClient();
            IHttpClientVerbBuilder <MockedHttpClient> builder = new HttpClientVerbBuilder <MockedHttpClient>(httpClient);

            // act
            IHttpClientQueryBuilder queryBuilder = builder.PostAsJson(url, payload);

            // assert
            Assert.NotNull(queryBuilder.Content);
            Assert.Matches(queryBuilder.HttpMethod.Method, "POST");
            JsonContent content = Assert.IsType <JsonContent>(queryBuilder.Content);
        }