public static RecordingHandler LoadRecordSessionIntoInMemoryStore(string path) { using var stream = System.IO.File.OpenRead(path); using var doc = JsonDocument.Parse(stream); var guid = Guid.NewGuid().ToString(); var session = new ModifiableRecordSession(RecordSession.Deserialize(doc.RootElement)); RecordingHandler handler = new RecordingHandler(Directory.GetCurrentDirectory()); handler.InMemorySessions.TryAdd(guid, session); return(handler); }
public async Task StartPlaybackAsync(string sessionId, HttpResponse outgoingResponse, RecordingType mode = RecordingType.FilePersisted) { var id = Guid.NewGuid().ToString(); ModifiableRecordSession session; if (mode == RecordingType.InMemory) { if (!InMemorySessions.TryGetValue(sessionId, out session)) { throw new HttpException(HttpStatusCode.BadRequest, $"There is no in-memory session with id {sessionId} available for playback retrieval."); } session.SourceRecordingId = sessionId; } else { var path = GetRecordingPath(sessionId); if (!File.Exists(path)) { throw new TestRecordingMismatchException($"Recording file path {path} does not exist."); } using var stream = System.IO.File.OpenRead(path); using var doc = await JsonDocument.ParseAsync(stream).ConfigureAwait(false); session = new ModifiableRecordSession(RecordSession.Deserialize(doc.RootElement)) { Path = path }; } if (!PlaybackSessions.TryAdd(id, session)) { throw new HttpException(HttpStatusCode.InternalServerError, $"Unexpectedly failed to add new playback session under id {id}."); } outgoingResponse.Headers.Add("x-recording-id", id); var json = JsonSerializer.Serialize(session.Session.Variables); outgoingResponse.Headers.Add("Content-Type", "application/json"); // Write to the response await outgoingResponse.WriteAsync(json); }
public void StartRecording(string sessionId, HttpResponse outgoingResponse) { var id = Guid.NewGuid().ToString(); var session = new ModifiableRecordSession(new RecordSession()) { Path = sessionId ?? String.Empty, Client = null }; if (!RecordingSessions.TryAdd(id, session)) { throw new HttpException(HttpStatusCode.InternalServerError, $"Unexpectedly failed to add new recording session under id {id}."); } outgoingResponse.Headers.Add("x-recording-id", id); }