예제 #1
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);
            }
        }
예제 #2
0
        public async Task TestInvalidRecordModeThrows(params string[] values)
        {
            var currentPath      = Directory.GetCurrentDirectory();
            var httpContext      = new DefaultHttpContext();
            var pathToRecording  = "recordings/invalid_record_mode";
            var mockClient       = new HttpClient(new MockHttpHandler());
            var recordingHandler = new RecordingHandler(currentPath)
            {
                RedirectableClient = mockClient,
                RedirectlessClient = mockClient
            };

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

            CreateRecordModeRequest(httpContext, new StringValues(values));

            HttpException resultingException = await Assert.ThrowsAsync <HttpException>(
                async() => await recordingHandler.HandleRecordRequestAsync(sessionId, httpContext.Request, httpContext.Response)
                );

            Assert.Equal(HttpStatusCode.BadRequest, resultingException.StatusCode);
        }
예제 #3
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);
            }
        }