コード例 #1
0
        private async void ValidateeSimpleHttpTriggerSentAsDefaultInvocationRequest(HttpRequestMessage httpRequestMessage)
        {
            Assert.Contains($"{HttpWorkerConstants.UserAgentHeaderValue}/{ScriptHost.Version}", httpRequestMessage.Headers.UserAgent.ToString());
            Assert.Equal(_testInvocationId.ToString(), httpRequestMessage.Headers.GetValues(HttpWorkerConstants.InvocationIdHeaderName).Single());
            Assert.Equal(ScriptHost.Version, httpRequestMessage.Headers.GetValues(HttpWorkerConstants.HostVersionHeaderName).Single());
            Assert.Equal(httpRequestMessage.RequestUri.ToString(), $"http://127.0.0.1:{_defaultPort}/{TestFunctionName}");

            HttpScriptInvocationContext httpScriptInvocationContext = await httpRequestMessage.Content.ReadAsAsync <HttpScriptInvocationContext>();

            // Verify Metadata
            var expectedMetadata = HttpWorkerTestUtilities.GetScriptInvocationBindingData();

            Assert.Equal(expectedMetadata.Count(), httpScriptInvocationContext.Metadata.Count());
            foreach (var key in expectedMetadata.Keys)
            {
                Assert.Equal(JsonConvert.SerializeObject(expectedMetadata[key]), httpScriptInvocationContext.Metadata[key]);
            }

            // Verify Data
            Assert.True(httpScriptInvocationContext.Data.Keys.Contains("testInputReq"));
            JObject resultHttpReq       = JObject.FromObject(httpScriptInvocationContext.Data["testInputReq"]);
            JObject expectedHttpRequest = await HttpWorkerTestUtilities.GetTestHttpRequest().GetRequestAsJObject();

            Assert.True(JToken.DeepEquals(expectedHttpRequest, resultHttpReq));
        }
        private async void ValidateDefaultInvocationRequest(HttpRequestMessage httpRequestMessage)
        {
            Assert.Contains($"{HttpWorkerConstants.UserAgentHeaderValue}/{ScriptHost.Version}", httpRequestMessage.Headers.UserAgent.ToString());
            Assert.Equal(_testInvocationId.ToString(), httpRequestMessage.Headers.GetValues(HttpWorkerConstants.InvocationIdHeaderName).Single());
            Assert.Equal(ScriptHost.Version, httpRequestMessage.Headers.GetValues(HttpWorkerConstants.HostVersionHeaderName).Single());
            Assert.Equal(httpRequestMessage.RequestUri.ToString(), $"http://127.0.0.1:{_defaultPort}/{TestFunctionName}");

            HttpScriptInvocationContext httpScriptInvocationContext = await httpRequestMessage.Content.ReadAsAsync <HttpScriptInvocationContext>();

            // Verify Metadata
            var expectedMetadata = HttpWorkerTestUtilities.GetScriptInvocationBindingData();

            Assert.Equal(expectedMetadata.Count(), httpScriptInvocationContext.Metadata.Count());
            foreach (var key in expectedMetadata.Keys)
            {
                Assert.Equal(JsonConvert.SerializeObject(expectedMetadata[key]), httpScriptInvocationContext.Metadata[key]);
            }

            // Verify Data
            var expectedData = HttpWorkerTestUtilities.GetScriptInvocationInputs();

            Assert.Equal(expectedData.Count(), httpScriptInvocationContext.Data.Count());
            foreach (var item in expectedData)
            {
                Assert.True(httpScriptInvocationContext.Data.Keys.Contains(item.name));
                Assert.Equal(JsonConvert.SerializeObject(item.val), httpScriptInvocationContext.Data[item.name]);
            }
        }
        public async Task ToHttpRequestTest()
        {
            HttpScriptInvocationContext testContext = new HttpScriptInvocationContext
            {
                Data     = new Dictionary <string, object>(),
                Metadata = new Dictionary <string, object>(),
            };

            testContext.Data.Add("randomDataKey", "randomDataValue");
            testContext.Metadata.Add("randomMetaDataKey", "randomMetaDataValue");

            string             expectedUri = "http://randomhost";
            HttpRequestMessage result      = testContext.ToHttpRequestMessage(expectedUri);

            string resultContent = await result.Content.ReadAsStringAsync();

            HttpScriptInvocationContext expected = JsonConvert.DeserializeObject <HttpScriptInvocationContext>(resultContent);

            Assert.Equal(result.RequestUri.ToString(), new Uri(expectedUri).ToString());
            Assert.Equal(result.Method, HttpMethod.Post);
            foreach (string key in testContext.Data.Keys)
            {
                Assert.Equal(testContext.Data[key], expected.Data[key]);
            }
            foreach (string key in testContext.Metadata.Keys)
            {
                Assert.Equal(testContext.Metadata[key], expected.Metadata[key]);
            }
        }
        public void TestSetRetryContext_Retry()
        {
            ScriptInvocationContext context = new ScriptInvocationContext()
            {
                ExecutionContext = new ExecutionContext()
                {
                    RetryContext = new Host.RetryContext()
                    {
                        RetryCount    = 1,
                        MaxRetryCount = 2,
                        Exception     = new Exception("test")
                    }
                }
            };
            HttpScriptInvocationContext testContext = new HttpScriptInvocationContext()
            {
                Data     = new Dictionary <string, object>(),
                Metadata = new Dictionary <string, object>()
            };

            ScriptInvocationContextExtensions.SetRetryContext(context, testContext);
            var retryContext = (RetryContext)testContext.Metadata["RetryContext"];

            Assert.NotNull(retryContext);
            Assert.Equal(retryContext.RetryCount, context.ExecutionContext.RetryContext.RetryCount);
            Assert.Equal(retryContext.MaxRetryCount, context.ExecutionContext.RetryContext.MaxRetryCount);
            Assert.NotNull(retryContext.Exception);
        }
コード例 #5
0
        public static HttpRequestMessage ToHttpRequestMessage(this HttpScriptInvocationContext context, string requestUri)
        {
            HttpRequestMessage httpRequest = new HttpRequestMessage
            {
                RequestUri = new Uri(requestUri),
                Method     = HttpMethod.Post,
                Content    = new ObjectContent <HttpScriptInvocationContext>(context, new JsonMediaTypeFormatter())
            };

            return(httpRequest);
        }
        public void TestSetRetryContext_NoRetry()
        {
            ScriptInvocationContext scriptInvocationContext = new ScriptInvocationContext()
            {
                ExecutionContext = new ExecutionContext()
            };

            HttpScriptInvocationContext testContext = new HttpScriptInvocationContext()
            {
                Data     = new Dictionary <string, object>(),
                Metadata = new Dictionary <string, object>()
            };

            ScriptInvocationContextExtensions.SetRetryContext(scriptInvocationContext, testContext);

            Assert.Empty(testContext.Metadata);
        }
        public static async Task <HttpScriptInvocationContext> ToHttpScriptInvocationContext(this ScriptInvocationContext scriptInvocationContext)
        {
            HttpScriptInvocationContext httpScriptInvocationContext = new HttpScriptInvocationContext();

            // populate metadata
            foreach (var bindingDataPair in scriptInvocationContext.BindingData)
            {
                if (bindingDataPair.Value != null)
                {
                    if (bindingDataPair.Value is HttpRequest)
                    {
                        // no metadata for httpTrigger
                        continue;
                    }
                    if (bindingDataPair.Key.EndsWith("trigger", StringComparison.OrdinalIgnoreCase))
                    {
                        // Data will include value of the trigger. Do not duplicate
                        continue;
                    }
                    httpScriptInvocationContext.Metadata[bindingDataPair.Key] = GetHttpScriptInvocationContextValue(bindingDataPair.Value);
                }
            }

            // populate input bindings
            foreach (var input in scriptInvocationContext.Inputs)
            {
                if (input.val is HttpRequest httpRequest)
                {
                    httpScriptInvocationContext.Data[input.name] = await httpRequest.GetRequestAsJObject();

                    continue;
                }
                httpScriptInvocationContext.Data[input.name] = GetHttpScriptInvocationContextValue(input.val, input.type);
            }

            SetRetryContext(scriptInvocationContext, httpScriptInvocationContext);
            return(httpScriptInvocationContext);
        }
 internal static void SetRetryContext(ScriptInvocationContext scriptInvocationContext, HttpScriptInvocationContext httpScriptInvocationContext)
 {
     if (scriptInvocationContext.ExecutionContext.RetryContext != null)
     {
         var retryContext = scriptInvocationContext.ExecutionContext.RetryContext;
         httpScriptInvocationContext.Metadata["RetryContext"] = new RetryContext()
         {
             MaxRetryCount = retryContext.MaxRetryCount,
             RetryCount    = retryContext.RetryCount,
             Exception     = retryContext.Exception,
         };
     }
 }