public static MockRouteData GeneratePassthroughRecord(HttpContext context) { MockRouteData route = new MockRouteData { Path = context.Request.Path, Method = context.Request.Method, Headers = context.Request.Headers.Select(kvp => new NamedParameter { Key = kvp.Key, Value = kvp.Value.ToString() }).ToArray(), Parameters = context.Request.Query.Select(kvp => new NamedParameter { Key = kvp.Key, Value = kvp.Value.ToString() }).ToArray() }; return(route); }
public async Task Invoke(HttpContext context) { Stream originalBody = context.Response.Body; try { using (var memStream = new MemoryStream()) { context.Response.Body = memStream; await next(context); memStream.Position = 0; string responseBody = new StreamReader(memStream).ReadToEnd(); // Not very optimal, but does the trick for now. var routeData = MockRouteData.GeneratePassthroughRecord(context); routeData.Code = (HttpStatusCode)context.Response.StatusCode; routeData.Response = responseBody; if (!server.PassthroughRecords.Add(routeData)) { // Do something here } if (server.BuildMode) { MockServerBroker.Instance.PromotePassthroughRouteToPermanent(server, routeData); MockServerBroker.Instance.SaveBuildServer(); } memStream.Position = 0; await memStream.CopyToAsync(originalBody); } } finally { context.Response.Body = originalBody; } }