public void Headers_Invalid()
        {
            HttpResponseMessage message = new HttpResponseMessage();
            HttpResponseHeaders headers = message.Headers;

            try {
                headers.Add("age", "");
                Assert.Fail("#1");
            } catch (FormatException) {
            }

            try {
                headers.Add(null, "");
                Assert.Fail("#2");
            } catch (ArgumentException) {
            }

            try {
                headers.Add("mm", null as IEnumerable <string>);
                Assert.Fail("#2b");
            } catch (ArgumentNullException) {
            }

            try {
                headers.Add("Allow", "audio");
                Assert.Fail("#2c");
            } catch (InvalidOperationException) {
            }

            Assert.IsFalse(headers.TryAddWithoutValidation("Allow", ""), "#3");

            Assert.IsFalse(headers.TryAddWithoutValidation(null, ""), "#4");

            try {
                headers.Contains(null);
                Assert.Fail("#5");
            } catch (ArgumentException) {
            }

            try {
                headers.GetValues(null);
                Assert.Fail("#6a");
            } catch (ArgumentException) {
            }

            try {
                headers.GetValues("bbbb");
                Assert.Fail("#6b");
            } catch (InvalidOperationException) {
            }

            try {
                headers.Add("location", new[] { "example.com", "example.org" });
                Assert.Fail("#7a");
            } catch (FormatException) {
            }

            headers.TryAddWithoutValidation("location", "*****@*****.**");
            try {
                headers.Add("location", "w3.org");
                Assert.Fail("#7b");
            } catch (FormatException) {
            }
        }
Example #2
0
        public void Test()
        {
            var catResponseHeader = _responseHeaders.GetValues(@"X-NewRelic-App-Data")?.FirstOrDefault();

            Assert.NotNull(catResponseHeader);

            var catResponseData = HeaderEncoder.DecodeAndDeserialize <CrossApplicationResponseData>(catResponseHeader, HeaderEncoder.IntegrationTestEncodingKey);

            var metrics = _fixture.AgentLog.GetMetrics().ToList();
            var calleeTransactionEvent = _fixture.AgentLog.TryGetTransactionEvent("WebTransaction/MVC/DefaultController/Index");

            Assert.NotNull(calleeTransactionEvent);

            var callerTransactionTrace = _fixture.AgentLog.TryGetTransactionSample("WebTransaction/MVC/DefaultController/ChainedHttpClient");

            Assert.NotNull(callerTransactionTrace);

            var crossProcessId = _fixture.AgentLog.GetCrossProcessId();

            // Note: we are checking the metrics that are generated by the *Caller* as a result of receiving a CAT response.
            var expectedMetrics = new List <Assertions.ExpectedMetric>
            {
                new Assertions.ExpectedMetric {
                    metricName = @"External/all", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"External/allWeb", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = $@"External/{_fixture.RemoteApplication.DestinationServerName}/all", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = $@"ExternalApp/{_fixture.RemoteApplication.DestinationServerName}/{crossProcessId}/all", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = $@"ExternalTransaction/{_fixture.RemoteApplication.DestinationServerName}/{crossProcessId}/WebTransaction/MVC/DefaultController/Index", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = $@"ExternalTransaction/{_fixture.RemoteApplication.DestinationServerName}/{crossProcessId}/WebTransaction/MVC/DefaultController/Index", metricScope = @"WebTransaction/MVC/DefaultController/ChainedHttpClient", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = $@"External/{_fixture.RemoteApplication.DestinationServerName}/Stream/GET", callCount = 1
                },
                new Assertions.ExpectedMetric {
                    metricName = @"ClientApplication/[^/]+/all", IsRegexName = true
                }
            };
            var unexpectedMetrics = new List <Assertions.ExpectedMetric>
            {
                // This scoped metric should be superceded by the ExternalTransaction metric above
                new Assertions.ExpectedMetric {
                    metricName = $@"External/{_fixture.RemoteApplication.DestinationServerName}/Stream/GET", metricScope = @"WebTransaction/MVC/DefaultController/ChainedHttpClient"
                }
            };
            // Note: we are checking the attributes attached to the *Callee's* transaction, not the caller's transaction. The attributes attached to the caller's transaction are already fully vetted in the CatInbound tests.
            var expectedTransactionEventIntrinsicAttributes1 = new List <string>
            {
                "nr.guid",
                "nr.pathHash",
                "nr.referringPathHash",
                "nr.referringTransactionGuid"
            };
            var expectedTransactionEventIntrinsicAttributes2 = new Dictionary <string, string>
            {
                // This value comes from what we send to the application (see parameter passed to GetWithCatHeader above)
                { "nr.tripId", "tripId" }
            };
            var expectedCallerTraceSegmentParameters = new List <Assertions.ExpectedSegmentParameter>
            {
                new Assertions.ExpectedSegmentParameter {
                    segmentName = "ExternalTransaction/[^/]+/[^/]+/WebTransaction/MVC/DefaultController/Index", IsRegexSegmentName = true, parameterName = "transaction_guid"
                }
            };

            NrAssert.Multiple(
                () => Assert.Equal(crossProcessId, catResponseData.CrossProcessId),
                () => Assert.Equal("WebTransaction/MVC/DefaultController/ChainedHttpClient", catResponseData.TransactionName),
                () => Assert.True(catResponseData.QueueTimeInSeconds >= 0),
                () => Assert.True(catResponseData.ResponseTimeInSeconds >= 0),
                () => Assert.Equal(-1, catResponseData.ContentLength),
                () => Assert.NotNull(catResponseData.TransactionGuid),
                () => Assert.False(catResponseData.Unused),

                () => Assertions.MetricsExist(expectedMetrics, metrics),
                () => Assertions.MetricsDoNotExist(unexpectedMetrics, metrics),

                // Note: It is difficult (perhaps impossible) to ensure that a transaction trace is generate for the chained request. This is because only one transaction trace is collected per harvest, and the chained requests will both be eligible.

                // calleeTransactionEvent attributes
                () => Assertions.TransactionEventHasAttributes(expectedTransactionEventIntrinsicAttributes1, TransactionEventAttributeType.Intrinsic, calleeTransactionEvent),
                () => Assertions.TransactionEventHasAttributes(expectedTransactionEventIntrinsicAttributes2, TransactionEventAttributeType.Intrinsic, calleeTransactionEvent),

                // callerTransactionTrace segments
                () => Assertions.TransactionTraceSegmentParametersExist(expectedCallerTraceSegmentParameters, callerTransactionTrace)
                );
        }
 public void Location_UseAddMethodWithInvalidValue_InvalidValueRecognized()
 {
     headers.TryAddWithoutValidation("Location", " http://example.com http://other");
     Assert.Null(headers.GetParsedValues("Location"));
     Assert.Equal(1, headers.GetValues("Location").Count());
     Assert.Equal(" http://example.com http://other", headers.GetValues("Location").First());
 }
Example #4
0
 public void SetHeaders(HttpResponseHeaders headers)
 {
     RateLimitLimit     = int.Parse(headers.GetValues("X-Ratelimit-Limit").First());
     RateLimitRemaining = int.Parse(headers.GetValues("X-Ratelimit-Remaining").First());
     RateLimitReset     = int.Parse(headers.GetValues("X-Ratelimit-Reset").First());
 }