Ejemplo n.º 1
0
        /// <summary>
        /// Configure the flow data to respond in the way we want for
        /// this test.
        /// </summary>
        /// <param name="flowData">
        /// The mock flow data instance to configure
        /// </param>
        /// <param name="jsonData">
        /// The JSON data to embed in the flow data.
        /// This will be copied into the JavaScript that is produced.
        /// </param>
        /// <param name="hostName">
        /// The host name to add to the evidence.
        /// The JavaScriptBuilder should use this to generate the
        /// callback URL.
        /// </param>
        /// <param name="protocol">
        /// The protocol to add to the evidence.
        /// The JavaScriptBuilder should use this to generate the
        /// callback URL.
        /// </param>
        /// <param name="userAgent">
        /// The User-Agent to add to the evidence.
        /// </param>
        /// <param name="latitude">
        /// The latitude to add to the evidence.
        /// </param>
        /// <param name="longitude">
        /// The longitude to add to the evidence.
        /// </param>
        private void Configure(
            Mock <IFlowData> flowData,
            JObject jsonData = null,
            string hostName  = "localhost",
            string protocol  = "https",
            string userAgent = "iPhone",
            string latitude  = "51",
            string longitude = "-1",
            string jsObjName = null)
        {
            if (jsonData == null)
            {
                jsonData           = new JObject();
                jsonData["device"] = new JObject(new JProperty("ismobile", true));
            }

            flowData.Setup(d => d.Get <IJsonBuilderElementData>()).Returns(() =>
            {
                var d  = new JsonBuilderElementData(new Mock <ILogger <JsonBuilderElementData> >().Object, flowData.Object.Pipeline);
                d.Json = jsonData.ToString();
                return(d);
            });

            string session  = "abcdefg-hijklmn-opqrst-uvwxyz";
            int    sequence = 1;

            // Setup the TryGetEvidence methods that are used to get
            // host and protocol for the callback URL
            flowData.Setup(d => d.TryGetEvidence(JavaScriptBuilder.Constants.EVIDENCE_HOST_KEY, out It.Ref <object> .IsAny))
            .Callback(new GetValueCallback((string key, out object result) => { result = hostName; })).Returns(true);
            flowData.Setup(d => d.TryGetEvidence(Core.Constants.EVIDENCE_PROTOCOL, out It.Ref <object> .IsAny))
            .Callback(new GetValueCallback((string key, out object result) => { result = protocol; })).Returns(true);
            flowData.Setup(d => d.TryGetEvidence(Engines.FiftyOne.Constants.EVIDENCE_SESSIONID, out It.Ref <object> .IsAny))
            .Callback(new GetValueCallback((string key, out object result) => { result = session; })).Returns(true);
            flowData.Setup(d => d.TryGetEvidence(Engines.FiftyOne.Constants.EVIDENCE_SEQUENCE, out It.Ref <object> .IsAny))
            .Callback(new GetValueCallback((string key, out object result) => { result = sequence; })).Returns(true);

            flowData.Setup(d => d.GetAsString(It.IsAny <string>())).Returns("None");
            var evidenceDict = new Dictionary <string, object>()
            {
                { JavaScriptBuilder.Constants.EVIDENCE_HOST_KEY, hostName },
                { Core.Constants.EVIDENCE_PROTOCOL, protocol },
                { Core.Constants.EVIDENCE_QUERY_USERAGENT_KEY, userAgent },
                { "query.latitude", latitude },
                { "query.longitude", longitude },
                { Engines.FiftyOne.Constants.EVIDENCE_SEQUENCE, sequence },
                { Engines.FiftyOne.Constants.EVIDENCE_SESSIONID, session }
            };

            if (jsObjName != null)
            {
                flowData.Setup(d => d.TryGetEvidence(JavaScriptBuilder.Constants.EVIDENCE_OBJECT_NAME, out It.Ref <object> .IsAny))
                .Callback(new GetValueCallback((string key, out object result) => { result = jsObjName; })).Returns(true);
                evidenceDict.Add(JavaScriptBuilder.Constants.EVIDENCE_OBJECT_NAME, jsObjName);
            }

            flowData.Setup(d => d.GetEvidence().AsDictionary()).Returns(evidenceDict);
            flowData.Setup(d => d.Get(It.IsAny <string>())).Returns(_elementDataMock.Object);
        }
        public void JavaScriptBuilder_VerifyUrl()
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory)
                .SetEndpoint("/json")
                .Build();

            var flowData = new Mock <IFlowData>();

            flowData.Setup(d => d.Get <IJsonBuilderElementData>()).Returns(() =>
            {
                var d  = new JsonBuilderElementData(new Mock <ILogger <JsonBuilderElementData> >().Object, null);
                d.Json = @"{ ""test"": ""value"" }";
                return(d);
            });
            // Setup the TryGetEvidence methods that are used to get
            // host and protocol for the callback URL
            flowData.Setup(d => d.TryGetEvidence(Pipeline.JavaScriptBuilder.Constants.EVIDENCE_HOST_KEY, out It.Ref <string> .IsAny))
            .Callback(new GetValueCallback((string key, out string result) => { result = "localhost"; }));
            flowData.Setup(d => d.TryGetEvidence(Pipeline.JavaScriptBuilder.Constants.EVIDENCE_PROTOCOL, out It.Ref <string> .IsAny))
            .Callback(new GetValueCallback((string key, out string result) => { result = "https"; }));
            // Setup the evidence dictionary accessor.
            // This is used to get the query parameters to add to the
            // callback URL.
            flowData.Setup(d => d.GetEvidence().AsDictionary()).Returns(new Dictionary <string, object>()
            {
                { Pipeline.JavaScriptBuilder.Constants.EVIDENCE_HOST_KEY, "localhost" },
                { Pipeline.JavaScriptBuilder.Constants.EVIDENCE_PROTOCOL, "https" },
            });
            // Setup the GetOrAdd method to catch the data object that is
            // set by the element so we can check it's values.
            IJavaScriptBuilderElementData result = null;

            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });

            _javaScriptBuilderElement.Process(flowData.Object);

            string expectedUrl = "https://localhost/json";

            Assert.IsTrue(result.JavaScript.Contains(expectedUrl),
                          $"JavaScript does not contain expected URL '{expectedUrl}'.");
        }
        public void JavaScriptBuilderElement_JavaScript(string key, string property, object value)
        {
            _javaScriptBuilderElement =
                new JavaScriptBuilderElementBuilder(_loggerFactory).Build();

            dynamic json = new JObject();

            if (value == null)
            {
                json[key] = new JObject(new JProperty(property, value), new JProperty(property + "nullreason", "No value set"));
                value     = "No value set";
            }
            else
            {
                json[key] = new JObject(new JProperty(property, value));
            }
            IJavaScriptBuilderElementData result = null;
            var flowData = new Mock <IFlowData>();

            flowData.Setup(d => d.Get <IJsonBuilderElementData>()).Returns(() =>
            {
                var d  = new JsonBuilderElementData(new Mock <ILogger <JsonBuilderElementData> >().Object, flowData.Object.Pipeline);
                d.Json = json.ToString();
                return(d);
            });

            flowData.Setup(d => d.GetAsString(It.IsAny <string>())).Returns("None");
            flowData.Setup(d => d.GetEvidence().AsDictionary()).Returns(new Dictionary <string, object>()
            {
                { Pipeline.JavaScriptBuilder.Constants.EVIDENCE_HOST_KEY, "localhost" },
                { Pipeline.JavaScriptBuilder.Constants.EVIDENCE_PROTOCOL, "https" },
            });
            flowData.Setup(d => d.Get(It.IsAny <string>())).Returns(_elementDataMock.Object);
            flowData.Setup(d => d.GetOrAdd(
                               It.IsAny <ITypedKey <IJavaScriptBuilderElementData> >(),
                               It.IsAny <Func <IPipeline, IJavaScriptBuilderElementData> >()))
            .Returns <ITypedKey <IJavaScriptBuilderElementData>, Func <IPipeline, IJavaScriptBuilderElementData> >((k, f) =>
            {
                result = f(flowData.Object.Pipeline);
                return(result);
            });
            _javaScriptBuilderElement.Process(flowData.Object);

            Assert.IsTrue(IsValidFodObject(result.JavaScript, key, property, value));
        }