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_url_condition_should_match(string requestUri, string queryUri, bool match) { var client = new MockHttpClient(); client.When(new Uri(queryUri, UriKind.RelativeOrAbsolute)) .Then(HttpStatusCode.OK); Assert.Equal(match ? HttpStatusCode.OK : HttpStatusCode.NotFound, (await client.GetAsync(requestUri)).StatusCode); }
public async Task When_url_query_condition_should_match(string requestUri, string pathRule, bool match) { var client = new MockHttpClient(); client.When(pathRule) .Then(HttpStatusCode.OK); Assert.Equal(match ? HttpStatusCode.OK : HttpStatusCode.NotFound, (await client.GetAsync(requestUri)).StatusCode); }
public TestHttpRepository() { var repo = new MockHttpClient(); repo.When(putRequest => putRequest.HasMethod(HttpMethod.Put)) .Then(async putRequest => { var requestString = await putRequest.Content.ReadAsStringAsync(); repo.When(HttpMethod.Get, putRequest.RequestUri) .Then(x => new HttpResponseMessage(HttpStatusCode.OK) .WithStringContent( requestString, mediaType: putRequest.Content.Headers.ContentType.MediaType)); return(new HttpResponseMessage(HttpStatusCode.OK)); }); repo.BaseAddress = new Uri("http://mockhttpclient.local"); Repo = repo; }
public async Task When_multiple_conditions_should_match_all(string requestUri, string queryUri1, string queryUri2, bool match) { var client = new MockHttpClient(); client.When(queryUri1) .When(queryUri2) .Then(HttpStatusCode.OK); Assert.Equal(match ? HttpStatusCode.OK : HttpStatusCode.NotFound, (await client.GetAsync(requestUri)).StatusCode); }
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_string_content_different_should_not_match() { // Arrange var client = new MockHttpClient(); client .When(x => x.Content.IsString("name=Wayne")) .Then(HttpStatusCode.OK); // Act var result = await client.GetAsync(new Uri("http://mockhttphandler.local")); // Assert Assert.NotEqual(HttpStatusCode.OK, result.StatusCode); }
public async Task When_byte_array_different_should_not_match() { // Arrange var client = new MockHttpClient(); client .When(x => x.Content.IsByteArray(new byte[] { 11, 22, 33 })) .Then(HttpStatusCode.OK); // Act var result = await client.PostAsync(new Uri("http://mockhttphandler.local"), new ByteArrayContent(new byte[] { 10, 20, 30 })); // Assert Assert.NotEqual(HttpStatusCode.OK, result.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 When_content_not_json_should_not_match() { // Arrange var client = new MockHttpClient(); client .When(x => x.Content.IsJson <TestDto>(x => true)) .Then(HttpStatusCode.OK); // Act var result = await client.PostAsync(new Uri("http://mockhttphandler.local"), new StringContent("not-a-json-string")); // Assert Assert.NotEqual(HttpStatusCode.OK, result.StatusCode); }
public async Task Should_record_request_history() { var client = new MockHttpClient(); client.When("*").Then(HttpStatusCode.OK); await client.GetAsync("http://mockhttphandler.local/1"); await client.GetAsync("http://mockhttphandler.local/2"); await client.GetAsync("http://mockhttphandler.local/3"); Assert.Equal(3, client.RequestHistory.Count); Assert.Equal("/1", client.RequestHistory[0].RequestUri.AbsolutePath); Assert.Equal("/2", client.RequestHistory[1].RequestUri.AbsolutePath); Assert.Equal("/3", client.RequestHistory[2].RequestUri.AbsolutePath); }
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 When_content_same_json_as_anonymous_should_match() { // Arrange var client = new MockHttpClient(); client .When(x => x.Content.IsJson(new { FirstName = "Wayne", })) .Then(HttpStatusCode.OK); // Act var result = await client.PostAsync(new Uri("http://mockhttphandler.local"), new StringContent(@"{""Firstname"":""Wayne"", ""IsMale"":true}")); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); }
public async Task When_string_content_same_should_match() { // Arrange var client = new MockHttpClient(); var content = "name=Wayne"; client .When(x => x.Content.IsString(content)) .Then(HttpStatusCode.OK); // Act var result = await client.PostAsync(new Uri("http://mockhttphandler.local"), new StringContent(content)); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); }
public async Task When_json_predicate_pass_then_should_match() { // Arrange var client = new MockHttpClient(); client .When(x => x.Content.IsJson <TestDto>(i => i.Firstname == "Wayne" && i.IsMale == true)) .Then(HttpStatusCode.OK); // Act var result = await client.PostAsJsonAsync(new Uri("http://mockhttphandler.local"), new TestDto { Firstname = "Wayne", IsMale = true }); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); }
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 When_content_same_json_should_match() { // Arrange var testDto = new TestDto { Firstname = "Wayne", IsMale = true }; var client = new MockHttpClient(); client .When(x => x.Content.IsJson(testDto)) .Then(HttpStatusCode.OK); var content = JsonConvert.SerializeObject(testDto); // Act var result = await client.PostAsync(new Uri("http://mockhttphandler.local"), new StringContent(content)); // Assert Assert.Equal(HttpStatusCode.OK, result.StatusCode); }
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 When_invalid_urlQuery_should_throw(string urlQuery) { var client = new MockHttpClient(); Assert.Throws <InvalidUrlQueryException>(() => client.When(urlQuery).Then(HttpStatusCode.OK)); }