Exemple #1
0
        public void Save_TwoRecord_Success()
        {
            var record = new CassetteRecord(
                new CassetteRecordRequest(
                    HttpMethod.Get.Method,
                    new Uri("http://*****:*****@"{""a"": 1, ""b"": 2}"));
            var yamlCassetteStorage = new YamlCassetteStorage();
            var stringWriter        = new StringWriter();

            yamlCassetteStorage.Save(stringWriter, new[] { record, record });

            var yamlStream = new YamlStream();

            yamlStream.Load(new StringReader(stringWriter.GetStringBuilder().ToString()));

            Assert.That(yamlStream.Documents, Has.Count.EqualTo(2));

            AssertDocument(yamlStream.Documents[0], record);
            AssertDocument(yamlStream.Documents[1], record);
        protected virtual void BeforeTest(ITest test, YamlCassetteStorage cassetteStorage, string cassettePath)
        {
            var cassette           = cassetteStorage.LoadCassette(cassettePath);
            var httpMessageHandler = CreateHttpMessageHandler(cassette);

            var withVcr = GetWithVcr(test);

            withVcr.Cassette = cassette;
            withVcr.HttpMessageHandlerFunc = httpMessageHandler;
        }
        protected virtual void AfterTest(ITest test, YamlCassetteStorage cassetteStorage, string cassettePath)
        {
            var withVcr = GetWithVcr(test);

            foreach (var record in withVcr.Cassette.Records)
            {
                FilterCassetteRecord(record);
            }

            cassetteStorage.SaveCassette(cassettePath, withVcr.Cassette);
        }
Exemple #4
0
        public void Save_SingleBytesRecord_Success([Values(10, 57 - 1, 57, 57 + 1, 57 * 2 - 1, 57 * 2, 57 * 2 + 1, 200)] int bodyLength)
        {
            var responseBody = Enumerable.Range(0, bodyLength).Select(i => (byte)i).ToArray();
            var record       = new CassetteRecord(
                new CassetteRecordRequest(
                    HttpMethod.Get.Method,
                    new Uri("http://localhost:8080/test"),
                    new NameValueCollection
            {
                { "Content-Type", "text" },
                { "Cookie", "value=1" },
                { "Cookie", "value=2" },
            }),
                new CassetteRecordResponse(
                    new Version(1, 1),
                    200,
                    "OK",
                    new NameValueCollection
            {
                { "Content-Type", "octet/stream" }
            },
                    responseBody));
            var yamlCassetteStorage = new YamlCassetteStorage();
            var stringWriter        = new StringWriter();

            yamlCassetteStorage.Save(stringWriter, new[] { record });

            var yamlStream = new YamlStream();

            yamlStream.Load(new StringReader(stringWriter.GetStringBuilder().ToString()));

            Assert.That(yamlStream.Documents, Has.Count.EqualTo(1));

            var document = yamlStream.Documents[0];

            Assert.That(document.RootNode, Is.TypeOf <YamlMappingNode>());
            var node = (YamlMappingNode)document.RootNode;

            Assert.That(node.Children, Has.Count.EqualTo(2));
            Assert.That(node.Children[new YamlScalarNode("Request")], Is.TypeOf <YamlMappingNode>());

            var requestNode = (YamlMappingNode)node.Children[new YamlScalarNode("Request")];

            Assert.That(requestNode.Children[new YamlScalarNode("Method")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Request.Method));
            Assert.That(requestNode.Children[new YamlScalarNode("Uri")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Request.Uri.ToString()));

            Assert.That(requestNode.Children[new YamlScalarNode("Headers")], Is.TypeOf <YamlSequenceNode>());
            var requestHeaders = (YamlSequenceNode)requestNode.Children[new YamlScalarNode("Headers")];

            Assert.That(requestHeaders.Children, Has.Count.EqualTo(2));
            Assert.That(requestHeaders[0], Is.TypeOf <YamlMappingNode>());
            Assert.That(((YamlMappingNode)requestHeaders[0]).Children[new YamlScalarNode("Content-Type")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo("text"));
            Assert.That(requestHeaders[1], Is.TypeOf <YamlMappingNode>());
            Assert.That(((YamlMappingNode)requestHeaders[1]).Children[new YamlScalarNode("Cookie")], Is.TypeOf <YamlSequenceNode>());
            Assert.That(((YamlSequenceNode)((YamlMappingNode)requestHeaders[1]).Children[new YamlScalarNode("Cookie")])[0], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo("value=1"));
            Assert.That(((YamlSequenceNode)((YamlMappingNode)requestHeaders[1]).Children[new YamlScalarNode("Cookie")])[1], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo("value=2"));

            Assert.That(node.Children[new YamlScalarNode("Response")], Is.TypeOf <YamlMappingNode>());

            var responseNode = (YamlMappingNode)node.Children[new YamlScalarNode("Response")];

            Assert.That(responseNode.Children[new YamlScalarNode("Version")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Response.Version.ToString()));
            Assert.That(responseNode.Children[new YamlScalarNode("StatusCode")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Response.StatusCode.ToString()));
            Assert.That(responseNode.Children[new YamlScalarNode("StatusMessage")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Response.StatusMessage));
            Assert.That(responseNode.Children[new YamlScalarNode("Body")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(Convert.ToBase64String(responseBody, Base64FormattingOptions.InsertLineBreaks).Replace("\r\n", "\n")));

            var responseHeaders = (YamlSequenceNode)responseNode.Children[new YamlScalarNode("Headers")];

            Assert.That(responseHeaders.Children, Has.Count.EqualTo(1));
            Assert.That(responseHeaders[0], Is.TypeOf <YamlMappingNode>());
            Assert.That(((YamlMappingNode)responseHeaders[0]).Children[new YamlScalarNode("Content-Type")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo("octet/stream"));
        }
Exemple #5
0
        public void Save_SingleRecord_Success()
        {
            var responseBody = @"{""a"": 1, ""b"": 2}";
            var record       = new CassetteRecord(
                new CassetteRecordRequest(
                    HttpMethod.Get.Method,
                    new Uri("http://localhost:8080/test"),
                    new NameValueCollection
            {
                { "Content-Type", "text" },
                { "Cookie", "value=1" },
                { "Cookie", "value=2" },
            }),
                new CassetteRecordResponse(
                    new Version(1, 1),
                    200,
                    "OK",
                    new NameValueCollection
            {
                { "Content-Type", "application/json" }
            },
                    responseBody));
            var yamlCassetteStorage = new YamlCassetteStorage();
            var stringWriter        = new StringWriter();

            yamlCassetteStorage.Save(stringWriter, new[] { record });

            var yamlStream = new YamlStream();

            yamlStream.Load(new StringReader(stringWriter.GetStringBuilder().ToString()));

            Assert.That(yamlStream.Documents, Has.Count.EqualTo(1));

            var document = yamlStream.Documents[0];

            Assert.That(document.RootNode, Is.TypeOf <YamlMappingNode>());
            var node = (YamlMappingNode)document.RootNode;

            Assert.That(node.Children, Has.Count.EqualTo(2));
            Assert.That(node.Children[new YamlScalarNode("Request")], Is.TypeOf <YamlMappingNode>());

            var requestNode = (YamlMappingNode)node.Children[new YamlScalarNode("Request")];

            Assert.That(requestNode.Children[new YamlScalarNode("Method")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Request.Method));
            Assert.That(requestNode.Children[new YamlScalarNode("Uri")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Request.Uri.ToString()));

            Assert.That(requestNode.Children[new YamlScalarNode("Headers")], Is.TypeOf <YamlSequenceNode>());
            var requestHeaders = (YamlSequenceNode)requestNode.Children[new YamlScalarNode("Headers")];

            Assert.That(requestHeaders.Children, Has.Count.EqualTo(2));
            Assert.That(requestHeaders[0], Is.TypeOf <YamlMappingNode>());
            Assert.That(((YamlMappingNode)requestHeaders[0]).Children[new YamlScalarNode("Content-Type")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo("text"));
            Assert.That(requestHeaders[1], Is.TypeOf <YamlMappingNode>());
            Assert.That(((YamlMappingNode)requestHeaders[1]).Children[new YamlScalarNode("Cookie")], Is.TypeOf <YamlSequenceNode>());
            Assert.That(((YamlSequenceNode)((YamlMappingNode)requestHeaders[1]).Children[new YamlScalarNode("Cookie")])[0], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo("value=1"));
            Assert.That(((YamlSequenceNode)((YamlMappingNode)requestHeaders[1]).Children[new YamlScalarNode("Cookie")])[1], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo("value=2"));

            Assert.That(node.Children[new YamlScalarNode("Response")], Is.TypeOf <YamlMappingNode>());

            var responseNode = (YamlMappingNode)node.Children[new YamlScalarNode("Response")];

            Assert.That(responseNode.Children[new YamlScalarNode("Version")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Response.Version.ToString()));
            Assert.That(responseNode.Children[new YamlScalarNode("StatusCode")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Response.StatusCode.ToString()));
            Assert.That(responseNode.Children[new YamlScalarNode("StatusMessage")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(record.Response.StatusMessage));
            Assert.That(responseNode.Children[new YamlScalarNode("Body")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo(responseBody));

            var responseHeaders = (YamlSequenceNode)responseNode.Children[new YamlScalarNode("Headers")];

            Assert.That(responseHeaders.Children, Has.Count.EqualTo(1));
            Assert.That(responseHeaders[0], Is.TypeOf <YamlMappingNode>());
            Assert.That(((YamlMappingNode)responseHeaders[0]).Children[new YamlScalarNode("Content-Type")], Is.TypeOf <YamlScalarNode>().And.Property(nameof(YamlScalarNode.Value)).EqualTo("application/json"));
        }