public async Task CanApplyHeaderTransform()
        {
            var headerTransform = new HeaderTransform(
                "someNewHeader",
                "value");
            RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());

            testRecordingHandler.Transforms.Clear();
            testRecordingHandler.Transforms.Add(headerTransform);

            var playbackContext  = new DefaultHttpContext();
            var targetFile       = "Test.RecordEntries/response_with_header_to_transform.json";
            var transformedEntry = TestHelpers.LoadRecordSession(targetFile).Session.Entries[0];

            // start playback
            playbackContext.Request.Headers["x-recording-file"] = targetFile;
            var controller = new Playback(testRecordingHandler)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = playbackContext
                }
            };
            await controller.Start();

            var recordingId = playbackContext.Response.Headers["x-recording-id"].ToString();

            // transform should apply
            HttpRequest  request  = TestHelpers.CreateRequestFromEntry(transformedEntry);
            HttpResponse response = new DefaultHttpContext().Response;
            await testRecordingHandler.HandlePlaybackRequest(recordingId, request, response);

            Assert.Equal("value", response.Headers["someNewHeader"]);
        }
示例#2
0
        public async Task CanApplyHeaderTransformWithCondition()
        {
            var headerTransform = new HeaderTransform(
                "Location",
                "http://localhost",
                condition: new ApplyCondition
            {
                ResponseHeader = new HeaderCondition
                {
                    Key        = "Location",
                    ValueRegex = @".*/Tables\(.*"
                }
            });
            RecordingHandler testRecordingHandler = new RecordingHandler(Directory.GetCurrentDirectory());

            testRecordingHandler.Transforms.Clear();
            testRecordingHandler.Transforms.Add(headerTransform);

            var playbackContext    = new DefaultHttpContext();
            var targetFile         = "Test.RecordEntries/response_with_header_to_transform.json";
            var transformedEntry   = TestHelpers.LoadRecordSession(targetFile).Session.Entries[0];
            var untransformedEntry = TestHelpers.LoadRecordSession(targetFile).Session.Entries[1];

            // start playback
            var body = "{\"x-recording-file\":\"" + targetFile + "\"}";

            playbackContext.Request.Body          = TestHelpers.GenerateStreamRequestBody(body);
            playbackContext.Request.ContentLength = body.Length;

            var controller = new Playback(testRecordingHandler)
            {
                ControllerContext = new ControllerContext()
                {
                    HttpContext = playbackContext
                }
            };
            await controller.Start();

            var recordingId = playbackContext.Response.Headers["x-recording-id"].ToString();

            // transform should apply only to first request
            HttpRequest  request  = TestHelpers.CreateRequestFromEntry(transformedEntry);
            HttpResponse response = new DefaultHttpContext().Response;
            await testRecordingHandler.HandlePlaybackRequest(recordingId, request, response);

            Assert.Equal("http://localhost", response.Headers["Location"]);

            // this one should keep the original Location value
            request  = TestHelpers.CreateRequestFromEntry(untransformedEntry);
            response = new DefaultHttpContext().Response;
            await testRecordingHandler.HandlePlaybackRequest(recordingId, request, response);

            var originalLocation = untransformedEntry.Response.Headers["Location"];

            Assert.Equal(originalLocation, response.Headers["Location"]);
        }