async public Task <NetmockeryTestCaseResult> ExecuteAgainstHttpClientAsync(HttpClient client, string requestUrl) { var retval = new NetmockeryTestCaseResult { TestCase = this }; if (QueryString != null) { requestUrl += QueryString; } HttpResponseMessage responseMessage; if (RequestBody != null) { responseMessage = await client.PostAsync(requestUrl, new ByteArrayContent(Encoding.UTF8.GetBytes(RequestBody))); } else { responseMessage = await client.GetAsync(requestUrl); } var body = await responseMessage.Content.ReadAsStringAsync(); string message; var requestMatcher = ""; if (responseMessage.Headers.Contains("X-Netmockery-RequestMatcher")) { requestMatcher = responseMessage.Headers.GetValues("X-Netmockery-RequestMatcher").ElementAt(0); } var responseCreator = ""; if (responseMessage.Headers.Contains("X-Netmockery-ResponseCreator")) { responseCreator = responseMessage.Headers.GetValues("X-Netmockery-ResponseCreator").ElementAt(0); } var contentType = ""; var charset = ""; if (responseMessage.Content.Headers.ContentType != null) { contentType = responseMessage.Content.Headers.ContentType.MediaType; charset = responseMessage.Content.Headers.ContentType.CharSet; } if (Evaluate(requestMatcher, responseCreator, body, contentType, charset, out message)) { retval.SetSuccess(); } else { retval.SetFailure(message); } return(retval); }
async public Task <NetmockeryTestCaseResult> ExecuteAsync(EndpointCollection endpointCollection, bool handleErrors = true, DateTime?now = null) { Debug.Assert(endpointCollection != null); var testResult = new NetmockeryTestCaseResult { TestCase = this }; try { var endpoint = endpointCollection.Resolve(RequestPath); if (endpoint == null) { return(testResult.SetFailure(ERROR_NOMATCHING_ENDPOINT)); } testResult.EndpointName = endpoint.Name; var matcher_and_creator = endpoint.Resolve(new PathString(RequestPath), new QueryString(QueryString), RequestBody ?? "", null); if (matcher_and_creator == null) { return(testResult.SetFailure(ERROR_ENDPOINT_HAS_NO_MATCH)); } if (!HasExpectations) { return(testResult.SetFailure("Test case has no expectations")); } var responseCreator = matcher_and_creator.ResponseCreator; string responseBody = null; string charset = ""; string contenttype = ""; if (NeedsResponseBody) { var simpleResponseCreator = responseCreator as SimpleResponseCreator; if (simpleResponseCreator == null) { return(testResult.SetFailure($"Response creator {responseCreator.ToString()} not supported by test framework")); } var requestInfo = new RequestInfo { EndpointDirectory = endpoint.Directory, Headers = null, RequestPath = RequestPath, QueryString = QueryString, RequestBody = RequestBody }; if (now != null) { requestInfo.SetStaticNow(now.Value); } responseBody = simpleResponseCreator.GetBodyAndExecuteReplacements(requestInfo); contenttype = simpleResponseCreator.ContentType ?? ""; charset = simpleResponseCreator.Encoding.WebName; } string message; if (Evaluate(matcher_and_creator.RequestMatcher.ToString(), matcher_and_creator.ResponseCreator.ToString(), responseBody, contenttype, charset, out message)) { return(testResult.SetSuccess()); } else { return(testResult.SetFailure(message)); } } catch (Exception exception) { if (!handleErrors) { throw; } return(testResult.SetException(exception)); } }