Exemple #1
0
        public void TestStopRecordingWithVariables()
        {
            var tmpPath          = Path.GetTempPath();
            var startHttpContext = new DefaultHttpContext();
            var pathToRecording  = "recordings/oauth_request_new.json";
            var recordingHandler = new RecordingHandler(tmpPath);
            var dict             = new Dictionary <string, string> {
                { "key1", "valueabc123" },
                { "key2", "value123abc" }
            };
            var endHttpContext = new DefaultHttpContext();

            recordingHandler.StartRecording(pathToRecording, startHttpContext.Response);
            var sessionId = startHttpContext.Response.Headers["x-recording-id"].ToString();

            recordingHandler.StopRecording(sessionId, variables: new SortedDictionary <string, string>(dict));
            var storedVariables = TestHelpers.LoadRecordSession(Path.Combine(tmpPath, pathToRecording)).Session.Variables;

            Assert.Equal(dict.Count, storedVariables.Count);

            foreach (var kvp in dict)
            {
                Assert.Equal(kvp.Value, storedVariables[kvp.Key]);
            }
        }
Exemple #2
0
        public void TestResetTargetsSessionOnly()
        {
            RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());
            var httpContext = new DefaultHttpContext();

            testRecordingHandler.StartRecording("recordingings/cool.json", httpContext.Response);
            var recordingId = httpContext.Response.Headers["x-recording-id"].ToString();

            testRecordingHandler.Sanitizers.Clear();
            testRecordingHandler.Sanitizers.Add(new BodyRegexSanitizer("sanitized", ".*"));
            testRecordingHandler.Transforms.Clear();
            testRecordingHandler.AddSanitizerToRecording(recordingId, new GeneralRegexSanitizer("sanitized", ".*"));
            testRecordingHandler.SetDefaultExtensions(recordingId);
            var session = testRecordingHandler.RecordingSessions.First().Value;

            // check that the individual session had reset sanitizers
            Assert.Empty(session.AdditionalSanitizers);

            // stop the recording to clear out the session cache
            testRecordingHandler.StopRecording(recordingId);

            // then verify that the session level is NOT reset.
            Assert.Single(testRecordingHandler.Sanitizers);
            Assert.IsType <BodyRegexSanitizer>(testRecordingHandler.Sanitizers.First());
        }
Exemple #3
0
        public void TestStopRecordingNullVariables()
        {
            var tmpPath          = Path.GetTempPath();
            var httpContext      = new DefaultHttpContext();
            var pathToRecording  = "recordings/oauth_request_new.json";
            var recordingHandler = new RecordingHandler(tmpPath);

            recordingHandler.StartRecording(pathToRecording, httpContext.Response);
            var sessionId = httpContext.Response.Headers["x-recording-id"].ToString();

            recordingHandler.StopRecording(sessionId, variables: null);

            var storedVariables = TestHelpers.LoadRecordSession(Path.Combine(tmpPath, pathToRecording)).Session.Variables;

            Assert.Empty(storedVariables);
        }
Exemple #4
0
        public async Task TestCanSkipRecordingEntireRequestResponse()
        {
            var currentPath     = Directory.GetCurrentDirectory();
            var httpContext     = new DefaultHttpContext();
            var pathToRecording = "recordings/skip_entry";

            var mockClient       = new HttpClient(new MockHttpHandler());
            var recordingHandler = new RecordingHandler(currentPath)
            {
                RedirectableClient = mockClient,
                RedirectlessClient = mockClient
            };


            var fullPathToRecording = Path.Combine(currentPath, pathToRecording) + ".json";

            recordingHandler.StartRecording(pathToRecording, httpContext.Response);
            var sessionId = httpContext.Response.Headers["x-recording-id"].ToString();

            CreateRecordModeRequest(httpContext, "request-response");

            await recordingHandler.HandleRecordRequestAsync(sessionId, httpContext.Request, httpContext.Response);

            httpContext = new DefaultHttpContext();
            // send a second request that SHOULD be recorded
            CreateRecordModeRequest(httpContext);
            httpContext.Request.Headers.Remove("x-recording-skip");
            httpContext.Request.Body = TestHelpers.GenerateStreamRequestBody("{ \"key\": \"value\" }");
            await recordingHandler.HandleRecordRequestAsync(sessionId, httpContext.Request, httpContext.Response);

            recordingHandler.StopRecording(sessionId);

            try
            {
                using var fileStream = File.Open(fullPathToRecording, FileMode.Open);
                using var doc        = JsonDocument.Parse(fileStream);
                var record = RecordSession.Deserialize(doc.RootElement);
                Assert.Single(record.Entries);
                var entry = record.Entries.First();
                Assert.Equal("value", JsonDocument.Parse(entry.Request.Body).RootElement.GetProperty("key").GetString());
                Assert.Equal(MockHttpHandler.DefaultResponse, Encoding.UTF8.GetString(entry.Response.Body));
            }
            finally
            {
                File.Delete(fullPathToRecording);
            }
        }
Exemple #5
0
        public void TestWriteAbsoluteRecording()
        {
            var tmpPath          = Path.GetTempPath();
            var currentPath      = Directory.GetCurrentDirectory();
            var httpContext      = new DefaultHttpContext();
            var pathToRecording  = Path.Combine(currentPath, "recordings/oauth_request_new.json");
            var recordingHandler = new RecordingHandler(tmpPath);

            recordingHandler.StartRecording(pathToRecording, httpContext.Response);
            var sessionId = httpContext.Response.Headers["x-recording-id"].ToString();

            recordingHandler.StopRecording(sessionId);

            try
            {
                Assert.True(File.Exists(pathToRecording));
            }
            finally
            {
                File.Delete(pathToRecording);
            }
        }
Exemple #6
0
        public async Task TestCanSkipRecordingRequestBody()
        {
            var currentPath      = Directory.GetCurrentDirectory();
            var httpContext      = new DefaultHttpContext();
            var pathToRecording  = "recordings/skip_body";
            var mockClient       = new HttpClient(new MockHttpHandler());
            var recordingHandler = new RecordingHandler(currentPath)
            {
                RedirectableClient = mockClient,
                RedirectlessClient = mockClient
            };
            var fullPathToRecording = Path.Combine(currentPath, pathToRecording) + ".json";

            recordingHandler.StartRecording(pathToRecording, httpContext.Response);
            var sessionId = httpContext.Response.Headers["x-recording-id"].ToString();

            CreateRecordModeRequest(httpContext, "request-body");

            await recordingHandler.HandleRecordRequestAsync(sessionId, httpContext.Request, httpContext.Response);

            recordingHandler.StopRecording(sessionId);

            try
            {
                using var fileStream = File.Open(fullPathToRecording, FileMode.Open);
                using var doc        = JsonDocument.Parse(fileStream);
                var record = RecordSession.Deserialize(doc.RootElement);
                var entry  = record.Entries.First();
                Assert.Null(entry.Request.Body);
                Assert.Equal(MockHttpHandler.DefaultResponse, Encoding.UTF8.GetString(entry.Response.Body));
            }
            finally
            {
                File.Delete(fullPathToRecording);
            }
        }