예제 #1
0
        public async Task Roundtrip()
        {
            var data = new byte[] { 255, 0 };

            var input = new HttpRequestMessage();

            input.RequestUri = new Uri("https://example.com:8080/");
            input.Method     = HttpMethod.Patch;
            input.Headers.Add("User-Agent", "Foo/1.0");
            input.Content = new ByteArrayContent(data);
            input.Content.Headers.Add("Content-Type", "application/x-test");

            HttpRequestMessage output;

            using (var stream = new MemoryStream())
            {
                await RequestSerialization.Serialize(input, stream);

                stream.Seek(0, SeekOrigin.Begin);
                output = await RequestSerialization.Deserialize(stream);
            }

            var outputData = await output.Content.ReadAsByteArrayAsync();

            Assert.AreEqual("https://example.com:8080/",
                            output.RequestUri.AbsoluteUri);
            Assert.AreEqual(HttpMethod.Patch, output.Method);
            Assert.AreEqual("Foo/1.0",
                            output.Headers.UserAgent.FirstOrDefault()?.ToString());
            Assert.AreEqual("application/x-test",
                            output.Content.Headers.ContentType.MediaType);
            CollectionAssert.AreEqual(data, outputData);
        }
예제 #2
0
        public async Task Roundtrip_NoContent()
        {
            var input = new HttpRequestMessage();

            input.RequestUri = new Uri("https://example.com:8080/");
            input.Method     = HttpMethod.Patch;

            HttpRequestMessage output;

            using (var stream = new MemoryStream())
            {
                await RequestSerialization.Serialize(input, stream);

                stream.Seek(0, SeekOrigin.Begin);
                output = await RequestSerialization.Deserialize(stream);
            }

            Assert.AreEqual("https://example.com:8080/",
                            output.RequestUri.AbsoluteUri);
            Assert.IsNull(output.Content);
        }
예제 #3
0
        public async Task ProcessMessage(
            [QueueTrigger("requests-v1")] string message,
            ILogger logger)
        {
            var blobClient = _storageProvider.Get(null).CreateCloudBlobClient();
            var blob       = new CloudBlob(new Uri(message), blobClient);

            HttpRequestMessage request;

            using (var stream = await blob.OpenReadAsync())
                request = await RequestSerialization.Deserialize(stream);

            HttpResponseMessage response;

            using (var client = _httpClientFactory.CreateClient())
                response = await client.SendAsync(request);

            logger.LogInformation(
                (int)response.StatusCode + " " +
                request.Method + " " +
                request.RequestUri);

            if (!response.IsSuccessStatusCode)
            {
                throw new ApplicationException(
                          request.Method + " " +
                          request.RequestUri + " failed with status code " +
                          (int)response.StatusCode);
            }

            try
            {
                await blob.DeleteAsync();
            }
            catch (Exception e)
            {
                logger.LogWarning(e, "Failed to delete blob");
            }
        }