public TelemetryItem <RequestData> ValidateRequestWithHeaders(InProcessServer server, string requestPath, Dictionary <string, string> requestHeaders, RequestTelemetry expected, bool expectRequestContextInResponse = true)
        {
            // Subtract 50 milliseconds to hack around strange behavior on build server where the RequestTelemetry.Timestamp is somehow sometimes earlier than now by a few milliseconds.
            expected.Timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromMilliseconds(50));
            Stopwatch timer = Stopwatch.StartNew();

            var response = this.ExecuteRequest(server.BaseHost + requestPath, requestHeaders);

            var actual = server.Listener.ReceiveItemsOfType <TelemetryItem <RequestData> >(1, TestListenerTimeoutInMs);

            timer.Stop();

            this.DebugTelemetryItems(actual);
            this.output.WriteLine("Response headers: " + string.Join(",", response.Headers.Select(kvp => $"{kvp.Key} = {kvp.Value.First()}")));

            var item = actual.OfType <TelemetryItem <RequestData> >().FirstOrDefault();

            Assert.NotNull(item);
            var data = ((TelemetryItem <RequestData>)item).data.baseData;

            Assert.Equal(expected.ResponseCode, data.responseCode);
            Assert.Equal(expected.Name, data.name);
            Assert.Equal(expected.Success, data.success);
            Assert.Equal(expected.Url, new Uri(data.url));
            Assert.Equal(expectRequestContextInResponse, response.Headers.Contains("Request-Context"));
            if (expectRequestContextInResponse)
            {
                Assert.True(response.Headers.TryGetValues("Request-Context", out var appIds));
                Assert.Equal($"appId={InProcessServer.AppId}", appIds.Single());
            }

            output.WriteLine("actual.Duration: " + data.duration);
            output.WriteLine("timer.Elapsed: " + timer.Elapsed);
            Assert.True(TimeSpan.Parse(data.duration) < timer.Elapsed.Add(TimeSpan.FromMilliseconds(20)), "duration");

            return(item);
        }
        public (TelemetryItem <RequestData>, TelemetryItem <RemoteDependencyData>) ValidateBasicDependency(InProcessServer server, string requestPath, DependencyTelemetry expected)
        {
            var response = this.ExecuteRequest(server.BaseHost + requestPath);

            var actual = server.Listener.ReceiveItems(TestListenerTimeoutInMs);

            this.DebugTelemetryItems(actual);

            var dependencyTelemetry = actual.OfType <TelemetryItem <RemoteDependencyData> >().FirstOrDefault();

            Assert.NotNull(dependencyTelemetry);
            var dependencyData = ((TelemetryItem <RemoteDependencyData>)dependencyTelemetry).data.baseData;

            Assert.Equal(expected.Data, dependencyData.data);
            Assert.Equal(expected.ResultCode, dependencyData.resultCode);
            Assert.Equal(expected.Success, dependencyData.success);

            var requestTelemetry = actual.OfType <TelemetryItem <RequestData> >().FirstOrDefault();

            Assert.NotNull(requestTelemetry);

            Assert.Equal(requestTelemetry.tags["ai.operation.id"], dependencyTelemetry.tags["ai.operation.id"]);
            Assert.Contains(dependencyTelemetry.data.baseData.id, requestTelemetry.tags["ai.operation.parentId"]);

            return(requestTelemetry, dependencyTelemetry);
        }
 public TelemetryItem <RequestData> ValidateBasicRequest(InProcessServer server, string requestPath, RequestTelemetry expected, bool expectRequestContextInResponse = true)
 {
     return(ValidateRequestWithHeaders(server, requestPath, null, expected, expectRequestContextInResponse));
 }