public async Task HandleRequest(HttpContext context, string requestBody, byte[] requestBodyBytes) { var responseRegistryItem = new ResponseRegistryItem { Timestamp = DateTime.Now, RequestBody = requestBody, RequestPath = context.Request.Path.ToString(), QueryString = context.Request.QueryString.ToString() }; try { await HandleRequestInner(responseRegistryItem, context, requestBody, requestBodyBytes); } catch (Exception e) { Debug.WriteLine(e); responseRegistryItem.Error = e.ToString(); } finally { responseRegistryItem.WriteToConsole(); _responseRegistry.Add(responseRegistryItem); } }
public void Add(ResponseRegistryItem responseRegistryItem) { Debug.Assert(responseRegistryItem.Id == 0); responseRegistryItem.Id = ++_nextId; if (_items.Count >= Capacity) { _items.Dequeue(); } _items.Enqueue(responseRegistryItem); }
public ResponseRegistryItem Add(ResponseRegistryItem responseRegistryItem) { Debug.Assert(responseRegistryItem.Id == 0); lock (_lock) { responseRegistryItem.Id = ++_nextId; if (_items.Count >= Capacity) { _items.Dequeue(); } _items.Enqueue(responseRegistryItem); } return(responseRegistryItem); }
public async Task HandleRequestInner(ResponseRegistryItem responseRegistryItem, HttpContext context, string requestBody, byte[] requestBodyBytes) { Debug.Assert(_endpointCollectionProvider != null); Debug.Assert(responseRegistryItem != null); var endpointCollection = _endpointCollectionProvider.EndpointCollection; var endpoint = endpointCollection.Resolve(context.Request.Path.ToString()); responseRegistryItem.Endpoint = endpoint; if (endpoint != null) { var matcher_and_creator = endpoint.Resolve(context.Request.Method, context.Request.Path, context.Request.QueryString, requestBody, context.Request.Headers); if (matcher_and_creator != null) { if (endpoint.RecordRequests) { _responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem); } var responseCreator = matcher_and_creator.ResponseCreator; responseRegistryItem.RequestMatcher = matcher_and_creator.RequestMatcher; responseRegistryItem.ResponseCreator = responseCreator; responseRegistryItem.SingleMatch = matcher_and_creator.SingleMatch; if (responseCreator.Delay > 0) { await Task.Delay(TimeSpan.FromSeconds(responseCreator.Delay)); } if (TestMode) { context.Response.Headers["X-Netmockery-RequestMatcher"] = matcher_and_creator.RequestMatcher.ToString(); context.Response.Headers["X-Netmockery-ResponseCreator"] = matcher_and_creator.ResponseCreator.ToString(); } var responseBytes = await responseCreator.CreateResponseAsync(new HttpRequestWrapper(context.Request), requestBodyBytes, new HttpResponseWrapper(context.Response), endpoint); responseRegistryItem.ResponseBody = Encoding.UTF8.GetString(responseBytes); } else { _responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem); responseRegistryItem.Error = "Endpoint has no match for request"; } } else { _responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem); responseRegistryItem.Error = "No endpoint matches request path"; } }
public async Task HandleRequest(HttpContext context, string requestBody, byte[] requestBodyBytes) { Debug.Assert(context != null); var responseRegistryItem = new ResponseRegistryItem { Timestamp = DateTime.Now, RequestBody = requestBody, Method = context.Request.Method, RequestPath = context.Request.Path.ToString(), QueryString = context.Request.QueryString.ToString() }; Debug.Assert(responseRegistryItem.Id == 0); try { await HandleRequestInner(responseRegistryItem, context, requestBody, requestBodyBytes); } catch (Exception e) { Debug.WriteLine(e); responseRegistryItem.Error = e.ToString(); if (!responseRegistryItem.HasBeenAddedToRegistry) { _responseRegistry.AddAndWriteIncomingInfoToConsole(responseRegistryItem); } context.Response.StatusCode = 500; await context.Response.WriteAsync("*********************\n"); await context.Response.WriteAsync($"NETMOCKERY EXCEPTION:\n\n{e}"); } finally { if (responseRegistryItem.HasBeenAddedToRegistry) { responseRegistryItem.WriteResolvedInfoToConsole(); } } }
public void AddAndWriteIncomingInfoToConsole(ResponseRegistryItem responseRegistryItem) { Add(responseRegistryItem); responseRegistryItem.WriteIncomingInfoToConsole(); }