public async Task ToRpc_Collection_Byte_With_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);
            MapField <string, string> addedCapabilities = new MapField <string, string>
            {
                { RpcWorkerConstants.TypedDataCollection, RpcWorkerConstants.TypedDataCollection }
            };

            capabilities.UpdateCapabilities(addedCapabilities);

            byte[][] arrBytes = new byte[2][];
            arrBytes[0] = new byte[] { 22 };
            arrBytes[1] = new byte[] { 11 };

            TypedData returned_typedata = await arrBytes.ToRpc(logger, capabilities);

            TypedData typedData = new TypedData();

            CollectionBytes collectionBytes = new CollectionBytes();

            foreach (byte[] element in arrBytes)
            {
                if (element != null)
                {
                    collectionBytes.Bytes.Add(ByteString.CopyFrom(element));
                }
            }
            typedData.CollectionBytes = collectionBytes;

            Assert.Equal(typedData.CollectionBytes, returned_typedata.CollectionBytes);
            Assert.Equal(typedData.CollectionBytes.Bytes[0], returned_typedata.CollectionBytes.Bytes[0]);
        }
        public async Task ToRpc_Collection_Double_With_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);
            MapField <string, string> addedCapabilities = new MapField <string, string>
            {
                { RpcWorkerConstants.TypedDataCollection, RpcWorkerConstants.TypedDataCollection }
            };

            capabilities.UpdateCapabilities(addedCapabilities);
            double[]  arrDouble         = { 1.1, 2.2 };
            TypedData returned_typedata = await arrDouble.ToRpc(logger, capabilities);

            TypedData typedData = new TypedData();

            CollectionDouble collectionDouble = new CollectionDouble();

            foreach (double element in arrDouble)
            {
                collectionDouble.Double.Add(element);
            }
            typedData.CollectionDouble = collectionDouble;

            Assert.Equal(typedData.CollectionDouble, returned_typedata.CollectionDouble);
            Assert.Equal(typedData.CollectionDouble.Double[0], returned_typedata.CollectionDouble.Double[0]);
        }
        public async Task HttpObjects_Headers(bool ignoreEmptyValues, string[] headerKeys, string[] headerValues, string[] expectedKeys, string[] expectedValues)
        {
            var logger = MockNullLoggerFactory.CreateLogger();
            // Capability must be enabled
            var capabilities = new Capabilities(logger);

            if (ignoreEmptyValues)
            {
                capabilities.UpdateCapabilities(new MapField <string, string>
                {
                    { RpcWorkerConstants.IgnoreEmptyValuedRpcHttpHeaders, "true" }
                });
            }

            var headerDictionary = new HeaderDictionary();

            for (int i = 0; i < headerValues.Length; i++)
            {
                headerDictionary.Add(headerKeys[i], headerValues[i]);
            }

            HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", $"http://localhost/api/httptrigger-scenarios", headerDictionary);

            var rpcRequestObject = await request.ToRpc(logger, capabilities);

            // Same key and value strings for each pair
            for (int i = 0; i < expectedKeys.Length; i++)
            {
                Assert.True(rpcRequestObject.Http.Headers.ContainsKey(expectedKeys[i]));
                Assert.Equal(expectedValues[i], rpcRequestObject.Http.Headers.GetValueOrDefault(expectedKeys[i]));
            }
        }
        public async Task ToRpc_Collection_String_With_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);
            MapField <string, string> addedCapabilities = new MapField <string, string>
            {
                { RpcWorkerConstants.TypedDataCollection, RpcWorkerConstants.TypedDataCollection }
            };

            capabilities.UpdateCapabilities(addedCapabilities);
            string[]  arrString         = { "element1", "element_2" };
            TypedData returned_typedata = await arrString.ToRpc(logger, capabilities);

            TypedData        typedData        = new TypedData();
            CollectionString collectionString = new CollectionString();

            foreach (string element in arrString)
            {
                if (!string.IsNullOrEmpty(element))
                {
                    collectionString.String.Add(element);
                }
            }
            typedData.CollectionString = collectionString;

            Assert.Equal(typedData.CollectionString, returned_typedata.CollectionString);
            Assert.Equal(typedData.CollectionString.String[0], returned_typedata.CollectionString.String[0]);
        }
        public async Task ToRpc_Collection_Long_With_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);
            MapField <string, string> addedCapabilities = new MapField <string, string>
            {
                { RpcWorkerConstants.TypedDataCollection, RpcWorkerConstants.TypedDataCollection }
            };

            capabilities.UpdateCapabilities(addedCapabilities);
            long[]    arrLong           = { 1L, 2L };
            TypedData returned_typedata = await arrLong.ToRpc(logger, capabilities);

            TypedData        typedData      = new TypedData();
            CollectionSInt64 collectionLong = new CollectionSInt64();

            foreach (long element in arrLong)
            {
                collectionLong.Sint64.Add(element);
            }
            typedData.CollectionSint64 = collectionLong;

            Assert.Equal(typedData.CollectionSint64, returned_typedata.CollectionSint64);
            Assert.Equal(typedData.CollectionSint64.Sint64[0], returned_typedata.CollectionSint64.Sint64[0]);
        }
        public async Task ToRpc_Collection_Double_Without_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            double[]  arrDouble         = { 1.1, 2.2 };
            TypedData returned_typedata = await arrDouble.ToRpc(logger, capabilities);

            TypedData typedData = new TypedData();

            typedData.Json = JsonConvert.SerializeObject(arrDouble);

            Assert.Equal(typedData.Json, returned_typedata.Json);
        }
        public async Task ToRpc_Collection_String_Without_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            string[]  arrString         = { "element1", "element_2" };
            TypedData returned_typedata = await arrString.ToRpc(logger, capabilities);

            TypedData typedData = new TypedData();

            typedData.Json = JsonConvert.SerializeObject(arrString);

            Assert.Equal(typedData.Json, returned_typedata.Json);
        }
        public void ToRpc_Collection_Long_Without_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            long[]    arrLong           = { 1L, 2L };
            TypedData returned_typedata = arrLong.ToRpc(logger, capabilities);

            TypedData typedData = new TypedData();

            typedData.Json = JsonConvert.SerializeObject(arrLong);

            Assert.Equal(typedData.Json, returned_typedata.Json);
        }
        public async Task ToRpc_Bytes_Without_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            byte[] arrByte = Encoding.Default.GetBytes("HellowWorld");

            TypedData returned_typedata = await arrByte.ToRpc(logger, capabilities);

            TypedData typedData = new TypedData();

            typedData.Bytes = ByteString.CopyFrom(arrByte);

            Assert.Equal(typedData.Bytes, returned_typedata.Bytes);
        }
Пример #10
0
        public void HttpObjects_ClaimsPrincipal()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", $"http://localhost/apihttptrigger-scenarios");

            var claimsIdentity1 = MockEasyAuth("facebook", "Connor McMahon", "10241897674253170");
            var claimsIdentity2 = new ClaimsIdentity(new List <Claim>
            {
                new Claim("http://schemas.microsoft.com/2017/07/functions/claims/authlevel", "Function")
            }, "WebJobsAuthLevel");
            var claimsIdentity3  = new ClaimsIdentity();
            var claimsIdentities = new List <ClaimsIdentity> {
                claimsIdentity1, claimsIdentity2, claimsIdentity3
            };

            request.HttpContext.User = new ClaimsPrincipal(claimsIdentities);

            var rpcRequestObject = request.ToRpc(logger, capabilities);

            var identities    = request.HttpContext.User.Identities.ToList();
            var rpcIdentities = rpcRequestObject.Http.Identities.ToList();

            Assert.Equal(claimsIdentities.Count, rpcIdentities.Count);

            for (int i = 0; i < identities.Count; i++)
            {
                var identity    = identities[i];
                var rpcIdentity = rpcIdentities.ElementAtOrDefault(i);

                Assert.NotNull(identity);
                Assert.NotNull(rpcIdentity);

                Assert.Equal(rpcIdentity.AuthenticationType?.Value, identity.AuthenticationType);
                Assert.Equal(rpcIdentity.NameClaimType?.Value, identity.NameClaimType);
                Assert.Equal(rpcIdentity.RoleClaimType?.Value, identity.RoleClaimType);

                var claims = identity.Claims.ToList();
                for (int j = 0; j < claims.Count; j++)
                {
                    Assert.True(rpcIdentity.Claims.ElementAtOrDefault(j) != null);
                    Assert.Equal(rpcIdentity.Claims[j].Type, claims[j].Type);
                    Assert.Equal(rpcIdentity.Claims[j].Value, claims[j].Value);
                }
            }
        }
        public async Task ToRpc_Collection_Byte_Without_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            byte[][] arrByte = new byte[2][];
            arrByte[0] = new byte[] { 22 };
            arrByte[1] = new byte[] { 11 };

            TypedData returned_typedata = await arrByte.ToRpc(logger, capabilities);

            TypedData typedData = new TypedData();

            typedData.Json = JsonConvert.SerializeObject(arrByte);

            Assert.Equal(typedData.Json, returned_typedata.Json);
        }
Пример #12
0
        [InlineData("say=Hi&to=", new string[] { "say" }, new string[] { "Hi" })] // Removes empty value query params
        public void HttpObjects_Query(string queryString, string[] expectedKeys, string[] expectedValues)
        {
            var logger = MockNullLoggerFactory.CreateLogger();

            HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", $"http://localhost/api/httptrigger-scenarios?{queryString}");

            var rpcRequestObject = request.ToRpc(logger);

            // Same number of expected key value pairs
            Assert.Equal(rpcRequestObject.Http.Query.Count, expectedKeys.Length);
            Assert.Equal(rpcRequestObject.Http.Query.Count, expectedValues.Length);
            // Same key and value strings for each pair
            for (int i = 0; i < expectedKeys.Length; i++)
            {
                Assert.True(rpcRequestObject.Http.Query.ContainsKey(expectedKeys[i]));
                Assert.Equal(rpcRequestObject.Http.Query.GetValueOrDefault(expectedKeys[i]), expectedValues[i]);
            }
        }
Пример #13
0
        public void HttpObjects_StringBody(string expectedContentType, object body)
        {
            var logger = MockNullLoggerFactory.CreateLogger();

            var headers = new HeaderDictionary();

            headers.Add("content-type", expectedContentType);
            HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", "http://localhost/api/httptrigger-scenarios", headers, body);

            var rpcRequestObject = request.ToRpc(logger);

            Assert.Equal(body.ToString(), rpcRequestObject.Http.Body.String);

            string contentType;

            rpcRequestObject.Http.Headers.TryGetValue("content-type", out contentType);
            Assert.Equal(expectedContentType, contentType);
        }
        public async Task ToRpc_Bytes_With_Capabilities_Value()
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);
            MapField <string, string> addedCapabilities = new MapField <string, string>
            {
                { RpcWorkerConstants.TypedDataCollection, RpcWorkerConstants.TypedDataCollection }
            };

            capabilities.UpdateCapabilities(addedCapabilities);
            byte[] arrByte = Encoding.Default.GetBytes("HellowWorld");

            TypedData returned_typedata = await arrByte.ToRpc(logger, capabilities);

            TypedData typedData = new TypedData();

            typedData.Bytes = ByteString.CopyFrom(arrByte);

            Assert.Equal(typedData.Bytes, returned_typedata.Bytes);
        }
Пример #15
0
        public void HttpObjects_RawBodyBytes_Image_Length(string contentType, string rawBytesEnabled)
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            if (!string.Equals(rawBytesEnabled, null))
            {
                capabilities.UpdateCapabilities(new MapField <string, string>
                {
                    { LanguageWorkerConstants.RawHttpBodyBytes, rawBytesEnabled.ToString() }
                });
            }

            FileStream image = new FileStream(TestImageLocation, FileMode.Open, FileAccess.Read);

            byte[] imageBytes  = FileStreamToBytes(image);
            string imageString = Encoding.UTF8.GetString(imageBytes);

            long imageBytesLength  = imageBytes.Length;
            long imageStringLength = imageString.Length;

            var headers = new HeaderDictionary();

            headers.Add("content-type", contentType);
            HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", "http://localhost/api/httptrigger-scenarios", headers, imageBytes);

            var rpcRequestObject = request.ToRpc(logger, capabilities);

            if (string.IsNullOrEmpty(rawBytesEnabled))
            {
                Assert.Empty(rpcRequestObject.Http.RawBody.Bytes);
                Assert.Equal(imageStringLength, rpcRequestObject.Http.RawBody.String.Length);
            }
            else
            {
                Assert.Empty(rpcRequestObject.Http.RawBody.String);
                Assert.Equal(imageBytesLength, rpcRequestObject.Http.RawBody.Bytes.ToByteArray().Length);
            }
        }
        public async Task HttpTrigger_Post_ByteArray(string expectedContentType, bool rcpHttpBodyOnly)
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            if (rcpHttpBodyOnly)
            {
                capabilities.UpdateCapabilities(new MapField <string, string>
                {
                    { RpcWorkerConstants.RpcHttpBodyOnly, rcpHttpBodyOnly.ToString() }
                });
            }

            var headers = new HeaderDictionary();

            headers.Add("content-type", expectedContentType);
            byte[] body = new byte[] { 1, 2, 3, 4, 5 };

            HttpRequest request = HttpTestHelpers.CreateHttpRequest("POST", "http://localhost/api/httptrigger-scenarios", headers, body);

            var rpcRequestObject = await request.ToRpc(logger, capabilities);

            if (rcpHttpBodyOnly)
            {
                Assert.Equal(null, rpcRequestObject.Http.RawBody);
                Assert.Equal(body, rpcRequestObject.Http.Body.Bytes);
            }
            else
            {
                Assert.Equal(body, rpcRequestObject.Http.Body.Bytes);
                Assert.Equal(Encoding.UTF8.GetString(body), rpcRequestObject.Http.RawBody.String);
            }

            string contentType;

            rpcRequestObject.Http.Headers.TryGetValue("content-type", out contentType);
            Assert.Equal(expectedContentType, contentType);
        }
        public void HttpObjects_StringBody(string expectedContentType, object body, bool rcpHttpBodyOnly)
        {
            var logger       = MockNullLoggerFactory.CreateLogger();
            var capabilities = new Capabilities(logger);

            if (rcpHttpBodyOnly)
            {
                capabilities.UpdateCapabilities(new MapField <string, string>
                {
                    { RpcWorkerConstants.RpcHttpBodyOnly, rcpHttpBodyOnly.ToString() }
                });
            }

            var headers = new HeaderDictionary();

            headers.Add("content-type", expectedContentType);
            HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", "http://localhost/api/httptrigger-scenarios", headers, body);

            var rpcRequestObject = request.ToRpc(logger, capabilities);

            Assert.Equal(body.ToString(), rpcRequestObject.Http.Body.String);
            if (rcpHttpBodyOnly)
            {
                Assert.Equal(null, rpcRequestObject.Http.RawBody);
                Assert.Equal(body.ToString(), rpcRequestObject.Http.Body.String);
            }
            else
            {
                Assert.Equal(body.ToString(), rpcRequestObject.Http.RawBody.String);
                Assert.Equal(body.ToString(), rpcRequestObject.Http.Body.String);
            }

            string contentType;

            rpcRequestObject.Http.Headers.TryGetValue("content-type", out contentType);
            Assert.Equal(expectedContentType, contentType);
        }