public async Task Summarize_Maps_Fields_Properly() { //arrange var request = new HttpRequestMessage(HttpMethod.Post, "https://www.whatever.com/someendpoint"); request.Headers.Add("User-Agent", "MyApp"); string requestBody = @"{""user"":""Alan"", ""trickyField"":""--!?@Divider:"", ""trickyField2"":""HTTP/1.1 200 OK""}"; request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); var response = new HttpResponseMessage(HttpStatusCode.OK); response.Headers.Add("Server", "Kestrel"); string responseBody = @"{""value"":""whatever"", ""trickyField"":""--!?@Divider:"", ""trickyField2"":""HTTP/1.1 200 OK""}"; response.Content = new StringContent(responseBody, Encoding.UTF8, "application/json"); // act var result = await RecordingFormatter.Summarize(request, response, TimeSpan.FromMilliseconds(1000)); // asserts result.Should().NotBeNull(); result.Metadata.LocalMachine = System.Environment.MachineName; result.Metadata.RecordedFrom.Should().NotBeNullOrEmpty(); result.Metadata.RecordedFrom.Should().EndWith(@"(No httpcontext available)"); result.Metadata.ToolUrl.Should().Be(Constants.Website); result.Metadata.ToolNameAndVersion.Should().StartWith("SystemTestingTools "); result.Metadata.User.Should().Contain(System.Environment.UserName); result.Metadata.Timezone.Should().NotBeNullOrWhiteSpace(); result.Metadata.DateTime.Should().BeAfter(System.DateTime.Now.AddSeconds(-1)); result.Metadata.DateTime.Should().BeBefore(System.DateTime.Now.AddSeconds(1)); result.Metadata.latencyMiliseconds.Should().Be(1000); result.Request.Method.Should().Be(HttpMethod.Post); result.Request.Url.Should().Be("https://www.whatever.com/someendpoint"); result.Request.Body.Should().Be(requestBody); result.Request.Headers.Count.Should().Be(2); result.Request.Headers["User-Agent"].Should().Be("MyApp"); result.Request.Headers["Content-Type"].Should().Be("application/json; charset=utf-8"); result.Response.HttpVersion.Should().Be(new System.Version(1, 1)); result.Response.Status.Should().Be(HttpStatusCode.OK); result.Response.Body.Should().Be(responseBody); result.Response.Headers.Count.Should().Be(2); result.Response.Headers["Server"].Should().Be("Kestrel"); result.Response.Headers["Content-Type"].Should().Be("application/json; charset=utf-8"); }
/// <summary> /// Save the request and response to a file; this same file can be used later to be returned as a response /// it will not save or throw an error if there is no response (an exception occurred) /// </summary> /// <param name="relativeFolder">Relative folder inside the base folder where your stubs will be saved</param> /// <param name="fileName">stub filename, should not contain the extension, if none passed the HttpStatus will be the name</param> /// <param name="howManyFilesToKeep">if 0, it will keep an infinite number of files, and add a number at the end, example: FILENAME_0001, FILENAME_0002 /// if 1, it will keep just one, and overwrite it everytime /// if >1, it will create files with a number at the end, but once the limit is reached, it will stop creating files, no overwritting</param> /// <returns>a task to be awaited</returns> public async Task SaveAsRecording(FolderRelativePath relativeFolder = null, FileName fileName = null, int howManyFilesToKeep = 0) { if (Response == null) { return; // nothing to save if we have no response (an exception occurred) } var requestResponse = await RecordingFormatter.Summarize(Request, Response, Duration); var finalFileName = Global.GlobalRecordingManager.Save(requestResponse, relativeFolder, fileName, howManyFilesToKeep); var recording = new Recording() { File = finalFileName, DateTime = DateTime.Now, Request = Request, Response = Response }; RecordingCollection.Recordings.Add(recording); }
/// <summary> /// Re-send the request of the recording, using exactly the same URL and headers, and update the relevant file /// </summary> /// <param name="recordings">the list of recordings whose request will be re-sent</param> /// <param name="client">the httpClient to be used for the calls, if none passed; a new one will be created. Pass one if you need non default configurations such as proxy or timeout</param> public static async Task ReSendRequestAndUpdateFile(this IEnumerable <Recording> recordings, HttpClient client = null) { if (!recordings.Any()) { return; } var recordingManager = new RecordingManager(); foreach (var recording in recordings) { var watch = Stopwatch.StartNew(); recording.Response = await recording.ReSendRequest(client); watch.Stop(); recording.DateTime = DateTime.Now; var requestResponse = await RecordingFormatter.Summarize(recording.Request, recording.Response, TimeSpan.FromMilliseconds(watch.ElapsedMilliseconds)); recordingManager.SaveToFile(recording.FileFullPath, requestResponse); } }
public List <Recording> GetRecordings(FolderAbsolutePath folder) { var list = new List <Recording>(); foreach (var fullFilePath in _fileSystem.GetTextFileNames(folder)) { var content = _fileSystem.ReadContent(fullFilePath); if (!RecordingFormatter.IsValid(content)) { continue; } var recording = RecordingFormatter.Read(content); if (recording == null) { continue; } recording.FileFullPath = fullFilePath; recording.File = StandardizeFileNameForDisplay(folder, fullFilePath); list.Add(recording); } return(list); }
public void SaveToFile(string fullFileName, RequestResponse log) { _fileSystem.CreateFile(fullFileName, RecordingFormatter.Format(log)); }