public async Task Can_Delete_Mocks_by_Id() { var url = "kestrelmock/mocks/"; var mockId = Guid.NewGuid().ToString(); var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Id = mockId, Request = new Request { Methods = new System.Collections.Generic.List <string> { "GET" }, Path = "/hello/world" }, Response = new Response { Status = 200, Body = "{ \"hello\": \"world\" }" } }; opts.Add(setting); })); }); }).CreateClient(); HttpResponseMessage getResponse = await client.GetAsync(url); Assert.Equal("OK", getResponse.StatusCode.ToString()); if (getResponse.StatusCode == HttpStatusCode.OK) { var message = await getResponse.Content.ReadAsStringAsync(); Assert.Contains(mockId, message, StringComparison.InvariantCultureIgnoreCase); } HttpResponseMessage deleteResponse = await client.DeleteAsync(url + mockId); HttpResponseMessage checkResultsResponse = await client.GetAsync(url); Assert.Equal("OK", checkResultsResponse.StatusCode.ToString()); if (getResponse.StatusCode == HttpStatusCode.OK) { var message = await checkResultsResponse.Content.ReadAsStringAsync(); Assert.DoesNotContain(mockId, message, StringComparison.InvariantCultureIgnoreCase); } }
public async Task CanReplaceBodyWhenMultiple(string wine, string color, string extraQuery) { var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Request = new Request { Methods = new System.Collections.Generic.List <string> { "GET" }, PathStartsWith = "/api/wines/" }, Response = new Response { Status = 200, Body = "{ \"wine\" : \"123\", \"color\" : \"abcde\", \"year\": 1978," + " \"nested\" : { \"color\" : \"x\" }" + " }", Replace = new Replace { UriTemplate = @"/api/wines/{wine}/{color}?year={year}", UriPathReplacements = new System.Collections.Generic.Dictionary <string, string> { { "wine", wine }, { "color", color } } } } }; opts.Add(setting); })); }); }).CreateClient(); var response = await client.GetAsync($"/api/wines/{wine}/{color}{extraQuery}"); var body = await response.Content.ReadAsStringAsync(); Assert.Contains($"\"wine\":\"{wine}\"", body); Assert.Contains($", \"color\":\"{color}\"", body); Assert.Contains($"{{ \"color\":\"{color}\"", body); Assert.Equal(200, (int)response.StatusCode); }
public async Task KestralMock_matchesRegex_using_verb(string url, string statusCode, string body, string method) { // Arrange var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Request = new Request { Methods = new System.Collections.Generic.List <string> { method }, PathMatchesRegex = ".+\\d{4}.+" }, Response = new Response { Status = (int)Enum.Parse(typeof(HttpStatusCode), statusCode, true), Body = body } }; opts.Add(setting); })); }); }).CreateClient(); HttpResponseMessage response; if (string.IsNullOrWhiteSpace(body)) { response = await client.DeleteAsync(url); } else { response = await client.PutAsync(url, new StringContent(body)); } Assert.Equal(response.StatusCode.ToString(), statusCode); if (response.StatusCode == HttpStatusCode.OK) { var message = await response.Content.ReadAsStringAsync(); Assert.Contains(body, message, StringComparison.InvariantCultureIgnoreCase); } }
public async Task CanMockBodyDoesNotContainResponse(bool bodyContains, int statusCode) { var unwantedBodyContent = "000000"; var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Request = new Request { Methods = new System.Collections.Generic.List <string> { "POST" }, Path = "/api/estimate", BodyDoesNotContain = unwantedBodyContent }, Response = new Response { Status = 200, Body = "BodyDoesNotContain Works" } }; opts.Add(setting); })); }); }).CreateClient(); var content = bodyContains ? new StringContent(unwantedBodyContent) : new StringContent("X"); var response = await client.PostAsync("api/estimate", content); if (!bodyContains) { Assert.Contains("BodyDoesNotContain Works", await response.Content.ReadAsStringAsync()); } Assert.Equal(statusCode, (int)response.StatusCode); }
private static async Task ReadBodyFromFile(HttpMockSetting setting) { if (!string.IsNullOrEmpty(setting.Response.BodyFromFilePath) && string.IsNullOrEmpty(setting.Response.Body)) { if (File.Exists(setting.Response.BodyFromFilePath)) { using var reader = File.OpenText(setting.Response.BodyFromFilePath); var bodyFromFile = await reader.ReadToEndAsync(); setting.Response.Body = bodyFromFile; } else { throw new Exception($"Path in BodyFromFilePath not found: {setting.Response.BodyFromFilePath}"); } } }
public async Task CanReplaceBodySingleFieldFromSettings() { var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Request = new Request { Methods = new System.Collections.Generic.List <string> { "GET" }, PathStartsWith = "/api/replace/" }, Response = new Response { Status = 200, Body = "{ \"replace\" : \"123\" }", //TODO: noted bug, replacement does not work correctly for numbers in json Replace = new Replace { BodyReplacements = new System.Collections.Generic.Dictionary <string, string> { { "replace", "modified" } } } } }; opts.Add(setting); })); }); }).CreateClient(); var response = await client.GetAsync($"/api/replace/"); Assert.Equal($"{{ \"replace\":\"modified\" }}", await response.Content.ReadAsStringAsync()); Assert.Equal(200, (int)response.StatusCode); }
public async Task CanMockResponseUsingPathRegex_Matches(string url) { // Arrange var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Request = new Request { Methods = new System.Collections.Generic.List <string> { "GET" }, PathMatchesRegex = ".+\\d{4}.+" }, Response = new Response { Status = 200, Body = "regex_is_working" } }; opts.Add(setting); })); }); }).CreateClient(); // Act var response = await client.GetAsync(url); // Assert response.EnsureSuccessStatusCode(); // Status Code 200-299 var message = await response.Content.ReadAsStringAsync(); Assert.Contains("regex_is_working", message); }
public async Task CanMockGetOrPostResponseUsingExactPath() { var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Request = new Request { Methods = new System.Collections.Generic.List <string> { "GET", "POST" }, Path = "/hello/world" }, Response = new Response { Status = 200, Body = "hello" } }; opts.Add(setting); })); }); }).CreateClient(); var responseGet = await client.GetAsync("hello/world"); Assert.Contains("hello", await responseGet.Content.ReadAsStringAsync()); Assert.Equal(200, (int)responseGet.StatusCode); var responsePost = await client.PostAsync("hello/world", new StringContent("test")); Assert.Contains("hello", await responsePost.Content.ReadAsStringAsync()); Assert.Equal(200, (int)responsePost.StatusCode); }
public async Task KestralMock_works_with_Refit() { var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Request = new Request { Methods = new System.Collections.Generic.List <string> { "GET" }, Path = "/hello/world" }, Response = new Response { Status = 200, Body = "{ \"hello\": \"world\" }" } }; opts.Add(setting); })); }); }).CreateClient(); var testApi = RestService.For <IKestralMockTestApi>(client); var helloWorld = await testApi.GetHelloWorldWorld(); Assert.Contains("world", helloWorld.Hello); }
public async Task CanMockResponseUsingPathRegex_NoMatch() { // Arrange var client = _factory.WithWebHostBuilder(b => { b.ConfigureTestServices(services => { services.Configure((Action <MockConfiguration>)(opts => { opts.Clear(); var setting = new HttpMockSetting { Request = new Request { Methods = new System.Collections.Generic.List <string> { "GET" }, PathMatchesRegex = ".+\\d{4}.+" }, Response = new Response { Status = 200, Body = "regex_is_working" } }; opts.Add(setting); })); }); }).CreateClient(); // Act var response = await client.GetAsync("/test/abcd/xyz"); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }