예제 #1
0
        public async Task PayloadSentWithProperUserAgent()
        {
            var isRequestFinished = new TaskCompletionSource <object>();

            HttpHeaderValueCollection <ProductInfoHeaderValue> userAgentHeader = null;
            var handler = new MockHttpMessageHandler((r, c) =>
            {
                userAgentHeader = r.Headers.UserAgent;
                isRequestFinished.SetResult(null);
                return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)));
            });

            var logger        = new NoopLogger();
            var service       = Service.GetDefaultService(new TestAgentConfigurationReader(logger), logger);
            var payloadSender = new PayloadSenderV2(logger, new TestAgentConfigurationReader(logger),
                                                    service, new Api.System(), handler);

            using (var agent = new ApmAgent(new TestAgentComponents(payloadSender: payloadSender)))
            {
                agent.PayloadSender.QueueTransaction(new Transaction(agent, "TestName", "TestType"));
            }

            await isRequestFinished.Task;

            userAgentHeader
            .Should()
            .NotBeEmpty()
            .And.HaveCount(3);

            userAgentHeader.First().Product.Name.Should().Be($"elasticapm-{Consts.AgentName}");
            userAgentHeader.First().Product.Version.Should().NotBeEmpty();

            userAgentHeader.Skip(1).First().Product.Name.Should().Be("System.Net.Http");
            userAgentHeader.Skip(1).First().Product.Version.Should().NotBeEmpty();

            userAgentHeader.Skip(2).First().Product.Name.Should().NotBeEmpty();
            userAgentHeader.Skip(2).First().Product.Version.Should().NotBeEmpty();
        }
예제 #2
0
        public void Get_SetsUpTheRequestCorrectly_AndDeserialisesTheResult()
        {
            // Arrange
            string url    = string.Empty;
            string method = string.Empty;
            HttpHeaderValueCollection <MediaTypeWithQualityHeaderValue> acceptHeaders = null;
            AuthenticationHeaderValue authentication = null;

            var jsonResult = JsonConvert.SerializeObject(new TestObject()
            {
                Name = "to", Value = 2
            });
            var httpClientHandler = new HttpClientHandlerFakeWithFunc()
            {
                SendAsyncFunc = (r, c) =>
                {
                    url            = r.RequestUri.ToString();
                    method         = r.Method.ToString();
                    acceptHeaders  = r.Headers.Accept;
                    authentication = r.Headers.Authorization;
                    return(Task.FromResult(new HttpResponseMessage(HttpStatusCode.OK)
                    {
                        Content = new StringContent(jsonResult)
                    }));
                }
            };
            Result <TestObject> result;

            // Act
            using (var restClient = new RestClient("https://buildserver:8154", "username", "password", false, httpClientHandler))
            {
                result = restClient.Get <TestObject>("/go/api/dashboard", "application/vnd.go.cd.v1+json");
            }

            //Assert
            Assert.That(result.IsValid, Is.True);
            Assert.That(url, Is.EqualTo("https://buildserver:8154/go/api/dashboard"));
            Assert.That(method, Is.EqualTo("GET"));
            Assert.That(acceptHeaders.Count, Is.EqualTo(1));
            Assert.That(acceptHeaders.First().ToString(), Is.EqualTo("application/vnd.go.cd.v1+json"));
            Assert.That(authentication.Scheme, Is.EqualTo("Basic"));
            Assert.That(authentication.Parameter, Is.EqualTo(Convert.ToBase64String(Encoding.ASCII.GetBytes($"username:password"))));
            Assert.That(result.Data.Name, Is.EqualTo("to"));
            Assert.That(result.Data.Value, Is.EqualTo(2));
        }
        protected HttpResponseMessage ReturnLatestOrNotModified <T>(DateTime?lastModified, T message)
        {
            //Content can vary if modified (diff last mod date), diff media type, diff request style, e.g. root/bare/etc.
            string etagString = "\"" +
                                lastModified +
                                String.Join(",", Request.Headers.Accept).Replace("\"", "-") +
                                Request.GetUrlHelper().Request.RequestUri.Query + "\"";

            HttpHeaderValueCollection <EntityTagHeaderValue> etags = Request.Headers.IfNoneMatch;
            bool etagsDiffer = false;

            if (etags.Count == 1)
            {
                if (etagString != etags.First().Tag)
                {
                    etagsDiffer = true;
                }
            }
            else if (etags.Count > 1)
            {
                throw new InvalidOperationException("Found more than 1 etag. This API doesn't know what to do with that.");
            }

            bool clientIsStale = ClientIsStale(lastModified) || etagsDiffer;

            if (!clientIsStale)
            {
                return(new HttpResponseMessage(HttpStatusCode.NotModified));
            }

            HttpResponseMessage rm = Request.CreateResponse(HttpStatusCode.OK, message);

            rm.Headers.CacheControl = new CacheControlHeaderValue
            {
                NoCache = false,
                Public  = false,
                Private = true,
                MaxAge  = new TimeSpan(24, 0, 0)
            };
            rm.Headers.ETag = new EntityTagHeaderValue(etagString);
            rm.Content.Headers.LastModified = (lastModified ?? DateTime.MinValue);
            return(rm);
        }