public void RecordMatcherThrowsExceptionsWithDetails()
        {
            var matcher = new RecordMatcher();

            var requestEntry = new RecordEntry()
            {
                RequestUri    = "http://localhost/",
                RequestMethod = RequestMethod.Head,
                Request       =
                {
                    Headers =
                    {
                        { "Content-Length",    new[] { "41"           } },
                        { "Some-Header",       new[] { "Random value" } },
                        { "Some-Other-Header", new[] { "V"            } }
                    },
                    Body    = Encoding.UTF8.GetBytes("This is request body, it's nice and long.")
                }
            };

            RecordEntry[] entries = new[]
            {
                new RecordEntry()
                {
                    RequestUri    = "http://remote-host",
                    RequestMethod = RequestMethod.Put,
                    Request       =
                    {
                        Headers =
                        {
                            { "Content-Length", new[] { "41"               } },
                            { "Some-Header",    new[] { "Non-Random value" } },
                            { "Extra-Header",   new[] { "Extra-Value"      } }
                        },
                        Body    = Encoding.UTF8.GetBytes("This is request body, it's nice and long but it also doesn't match.")
                    }
                }
            };

            TestRecordingMismatchException exception = Assert.Throws <TestRecordingMismatchException>(() => matcher.FindMatch(requestEntry, entries));

            Assert.AreEqual(
                "Unable to find a record for the request HEAD http://localhost/" + Environment.NewLine +
                "Method doesn't match, request <HEAD> record <PUT>" + Environment.NewLine +
                "Uri doesn't match:" + Environment.NewLine +
                "    request <http://localhost/>" + Environment.NewLine +
                "    record  <http://remote-host>" + Environment.NewLine +
                "Header differences:" + Environment.NewLine +
                "    <Some-Header> values differ, request <Random value>, record <Non-Random value>" + Environment.NewLine +
                "    <Some-Other-Header> is absent in record, value <V>" + Environment.NewLine +
                "    <Extra-Header> is absent in request, value <Extra-Value>" + Environment.NewLine +
                "Body differences:" + Environment.NewLine +
                "Request and response bodies do not match at index 40:" + Environment.NewLine +
                "     request: \"e and long.\"" + Environment.NewLine +
                "     record:  \"e and long but it also doesn't\"" + Environment.NewLine,
                exception.Message);
        }
예제 #2
0
        public void RecordMatcheRequiresPresenceOfIgnoredHeaders()
        {
            var matcher = new RecordMatcher();

            var mockRequest = new RecordEntry()
            {
                RequestUri    = "http://localhost",
                RequestMethod = RequestMethod.Put,
                Request       =
                {
                    // Request-Id and TraceParent are ignored until we can
                    // re-record all old tests.
                    Headers =
                    {
                        { "Request-Id", new[] { "Some Random value"             } },
                        { "Date",       new[] { "Fri, 06 Nov 2020 02:42:26 GMT" } },
                        { "x-ms-date",  new[] { "Fri, 06 Nov 2020 02:42:26 GMT" } },
                    }
                }
            };

            RecordEntry[] entries = new[]
            {
                new RecordEntry()
                {
                    RequestUri    = "http://localhost",
                    RequestMethod = RequestMethod.Put,
                    Request       =
                    {
                        Headers =
                        {
                            { "x-ms-client-request-id", new[] { "some random request id"  } },
                            { "User-Agent",             new[] { "some random sdk"         } },
                            { "traceparent",            new[] { "some random traceparent" } }
                        }
                    }
                }
            };

            TestRecordingMismatchException exception = Assert.Throws <TestRecordingMismatchException>(() => matcher.FindMatch(mockRequest, entries));

            Assert.Equal(
                "Unable to find a record for the request PUT http://localhost" + Environment.NewLine +
                "Header differences:" + Environment.NewLine +
                "    <Date> is absent in record, value <Fri, 06 Nov 2020 02:42:26 GMT>" + Environment.NewLine +
                "    <x-ms-date> is absent in record, value <Fri, 06 Nov 2020 02:42:26 GMT>" + Environment.NewLine +
                "    <User-Agent> is absent in request, value <some random sdk>" + Environment.NewLine +
                "    <x-ms-client-request-id> is absent in request, value <some random request id>" + Environment.NewLine +
                "Body differences:" + Environment.NewLine,
                exception.Message);
        }
        public void RecordMatcherThrowsExceptionsWhenNoRecordsLeft()
        {
            var matcher = new RecordMatcher();

            var mockRequest = new RecordEntry()
            {
                RequestUri    = "http://localhost/",
                RequestMethod = RequestMethod.Head
            };

            RecordEntry[] entries = { };

            TestRecordingMismatchException exception = Assert.Throws <TestRecordingMismatchException>(() => matcher.FindMatch(mockRequest, entries));

            Assert.AreEqual(
                "Unable to find a record for the request HEAD http://localhost/" + Environment.NewLine +
                "No records to match." + Environment.NewLine,
                exception.Message);
        }