public async Task PushStream_HttpContentIntegrationTest() { // Arrange var expected = "Hello, world!"; using (var client = new MockHttpClient()) { // We mock the client, so this doesn't actually hit the web. This client will just echo back // the body content we give it. using (var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:30000/")) { request.Content = new PushStreamContent((stream, content, context) => { using (var writer = new StreamWriter(stream)) { writer.Write(expected); } }, "text/plain"); // Act using (var response = await client.SendAsync(request, CancellationToken.None)) { // Assert response.EnsureSuccessStatusCode(); var responseText = await response.Content.ReadAsStringAsync(); Assert.Equal(expected, responseText); } } } }
public async Task should_create_content_with_media_type_and_serializer_settings() { var client = new MockHttpClient(); client.When("*").Then(x => new HttpResponseMessage() .WithJsonContent(new { FirstName = "John", LastName = "Doe", }, "application/vnd.mockhttphandler.local", new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() })); var request = new HttpRequestMessage(); request.RequestUri = new Uri("http://mockhttphandler.local"); var response = await client.SendAsync(request); var responseObj = JsonConvert.DeserializeObject <dynamic>(await response.Content.ReadAsStringAsync()); Assert.Equal(responseObj.firstName.ToString(), "John"); Assert.Equal(responseObj.lastName.ToString(), "Doe"); Assert.Equal("application/vnd.mockhttphandler.local", response.Content.Headers.ContentType.MediaType); }
public async Task When_header_wihtout_value_should_match_any_value() { var client = new MockHttpClient(); client.When(x => x.HasHeader("version")) .Then(HttpStatusCode.OK); var request = new HttpRequestMessage(); request.RequestUri = new Uri("http://mockhttphandler.local"); request.Headers.Add("version", "4"); Assert.Equal(HttpStatusCode.OK, (await client.SendAsync(request)).StatusCode); }
public async Task When_content_header_condition_should_match() { var client = new MockHttpClient(); client.When(x => x.HasHeader("content-type", "application/json")) .Then(HttpStatusCode.OK); var request = new HttpRequestMessage(); request.RequestUri = new Uri("http://mockhttphandler.local"); request.Content = new StringContent(""); request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"); Assert.Equal(HttpStatusCode.OK, (await client.SendAsync(request)).StatusCode); }
public async Task When_multiple_headers_condition_should_match_any() { var client = new MockHttpClient(); client.When(x => x.HasHeader("Accept", "application/json")) .Then(HttpStatusCode.OK); var request = new HttpRequestMessage(); request.RequestUri = new Uri("http://mockhttphandler.local"); request.Headers.Add("Accept", "application/xml"); request.Headers.Add("Accept", "application/json"); request.Headers.Add("Accept", "text/html"); Assert.Equal(HttpStatusCode.OK, (await client.SendAsync(request)).StatusCode); }
public async Task should_add_content_headers(string header, string value) { var client = new MockHttpClient(); client.When("*").Then(x => new HttpResponseMessage() .WithHeader(header, value)); var request = new HttpRequestMessage(); request.RequestUri = new Uri("http://mockhttphandler.local"); var response = await client.SendAsync(request); var headers = response.Content.Headers.GetValues(header).ToList(); Assert.Single(headers); Assert.Equal(value, headers[0]); }
public async Task should_add_header_mix() { var client = new MockHttpClient(); client.When("*").Then(x => new HttpResponseMessage() .WithHeader("content-disposition", "inline") .WithHeader("warning", "110") .WithHeader("content-type", "application/json") .WithHeader("my-custom-header", "BOOM")); var request = new HttpRequestMessage(); request.RequestUri = new Uri("http://mockhttphandler.local"); var response = await client.SendAsync(request); Assert.Equal("inline", response.Content.Headers.GetValues("content-disposition").Single()); Assert.Equal("application/json", response.Content.Headers.GetValues("content-type").Single()); Assert.Equal("110", response.Headers.GetValues("warning").Single()); Assert.Equal("BOOM", response.Headers.GetValues("my-custom-header").Single()); }
public async Task should_create_content_with_media_type() { var client = new MockHttpClient(); client.When("*").Then(x => new HttpResponseMessage() .WithXmlContent(new Name { FirstName = "John", LastName = "Doe", }, "application/vnd.mockhttphandler.local")); var request = new HttpRequestMessage(); request.RequestUri = new Uri("http://mockhttphandler.local"); var response = await client.SendAsync(request); var result = await response.Content.ReadAsStringAsync(); Assert.Contains("<FirstName>John</FirstName>", result); Assert.Contains("<LastName>Doe</LastName>", result); Assert.Equal("application/vnd.mockhttphandler.local", response.Content.Headers.ContentType.MediaType); }
public async Task should_create_content() { var client = new MockHttpClient(); client.When("*").Then(x => new HttpResponseMessage() .WithJsonContent(new { FirstName = "John", LastName = "Doe", })); var request = new HttpRequestMessage(); request.RequestUri = new Uri("http://mockhttphandler.local"); var response = await client.SendAsync(request); var responseObj = JsonConvert.DeserializeObject <dynamic>(await response.Content.ReadAsStringAsync()); Assert.Equal(responseObj.FirstName.ToString(), "John"); Assert.Equal(responseObj.LastName.ToString(), "Doe"); Assert.Equal("application/json", response.Content.Headers.ContentType.MediaType); }
public async Task PushStream_HttpContentIntegrationTest() { // Arrange var expected = "Hello, world!"; using (var client = new MockHttpClient()) { // We mock the client, so this doesn't actually hit the web. This client will just echo back // the body content we give it. using (var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost:30000/")) { request.Content = new PushStreamContent((stream, content, context) => { using (var writer = new StreamWriter(stream)) { writer.Write(expected); } }, "text/plain"); // Act using (var response = await client.SendAsync(request, CancellationToken.None)) { // Assert response.EnsureSuccessStatusCode(); var responseText = await response.Content.ReadAsStringAsync(); Assert.Equal(expected, responseText); } } } }