Ejemplo n.º 1
0
        public void WebSources_Constructor_With_NotNullResourceCatalog_ExpectedSuccess()
        {
            const string xmlString = @"<Source ID=""1a82a341-b678-4992-a25a-39cdd57198d4"" Name=""Example Redis Source"" ResourceType=""RedisSource"" IsValid=""false"" 
                                        ConnectionString=""HostName=localhost;Port=6379;UserName=warewolf;Password=;AuthenticationType=Anonymous"" Type=""RedisSource"" ServerVersion=""1.4.1.27"" ServerID=""693ca20d-fb17-4044-985a-df3051d6bac7"">
                                          <DisplayName>Example Redis Source</DisplayName>
                                          <AuthorRoles>
                                          </AuthorRoles>
                                          <ErrorMessages />
                                          <TypeOf>RedisSource</TypeOf>
                                          <VersionInfo DateTimeStamp=""2017-05-26T14:21:24.3247847+02:00"" Reason="""" User=""NT AUTHORITY\SYSTEM"" VersionNumber=""3"" ResourceId=""1a82a341-b678-4992-a25a-39cdd57198d4"" VersionId=""b1a6de00-3cac-41cd-b0ed-9fac9bb61266"" />
                                        </Source>";

            var resourceId = Guid.Parse("1a82a341-b678-4992-a25a-39cdd57198d4");

            var mockResourceCatalog = new Mock <IResourceCatalog>();

            mockResourceCatalog.Setup(o => o.GetResourceContents(Guid.Empty, resourceId))
            .Returns(new StringBuilder(xmlString));

            var sut = new WebSources(mockResourceCatalog.Object);

            var result = sut.Get(resourceId.ToString(), Guid.Empty);

            Assert.IsNotNull(result);
            Assert.AreEqual(resourceId, result.ResourceID);
            Assert.AreEqual("Example Redis Source", result.ResourceName);
        }
Ejemplo n.º 2
0
        public void WebSourcesGetWithValidArgsExpectedReturnsSource()
        {
            var expected = CreateWebSource();
            var saveArgs = expected.ToString();

            var workspaceID   = Guid.NewGuid();
            var workspacePath = EnvironmentVariables.GetWorkspacePath(workspaceID);

            try
            {
                var handler = new WebSources();
                handler.Save(saveArgs, workspaceID, Guid.Empty);

                var actual = handler.Get(expected.ResourceID.ToString(), workspaceID, Guid.Empty);

                VerifySource(actual, expected);
            }
            finally
            {
                try
                {
                    if (Directory.Exists(workspacePath))
                    {
                        DirectoryHelper.CleanUp(workspacePath);
                    }
                }
                // ReSharper disable EmptyGeneralCatchClause
                catch (Exception)
                // ReSharper restore EmptyGeneralCatchClause
                {
                }
            }
        }
Ejemplo n.º 3
0
        public void WebSourcesTestWithInValidArgsExpectedInvalidValidationResult()
        {
            var handler = new WebSources();
            var result  = handler.Test("root:'hello'", Guid.Empty, Guid.Empty);

            Assert.IsFalse(result.IsValid);
        }
Ejemplo n.º 4
0
        public void WebSources_Execute_SetsContentTypeHeaders_multipart()
        {
            var responseFromWeb      = Encoding.ASCII.GetBytes("response from web request");
            var mockWebClientWrapper = new Mock <IWebClientWrapper>();

            mockWebClientWrapper.Setup(o => o.Headers).Returns(new WebHeaderCollection {
                "a:x", "b:e", "Content-Type: multipart/form-data"
            });
            mockWebClientWrapper.Setup(o => o.UploadData(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <byte[]>()))
            .Returns(responseFromWeb);

            var source = new WebSource
            {
                Address            = "http://www.msn.com/",
                AuthenticationType = AuthenticationType.Anonymous,
                Client             = mockWebClientWrapper.Object
            };

            _ = WebSources.Execute(source, WebRequestMethod.Post, "http://www.msn.com/", "", false, out var errors, new string[] { });

            var client      = source.Client;
            var contentType = client.Headers["Content-Type"];

            Assert.IsNotNull(contentType);
            Assert.AreEqual("multipart/form-data", contentType);
        }
Ejemplo n.º 5
0
        public void UpdateConfig()
        {
            XElement contacts =
                new XElement("Config",
                             new XElement("Port", $"{Port}"),
                             new XElement("Duration", $"{Duration}"),
                             new XElement("ModbusServerAddress", $"{ModbusServerAddress}"),
                             new XElement("Sources",
                                          WebSources.Select((source, i) => new XElement($"Source{i}",
                                                                                        new XElement("Duration", source.Duration),
                                                                                        new XElement("Name", source.Name),
                                                                                        new XElement("WebAddress", source.WebAddress),
                                                                                        new XElement("Weights", source.RegistersGroups.Select((weight, j) => new XElement($"Weight{j}",
                                                                                                                                                                          new XElement("Name", weight.Name),
                                                                                                                                                                          new XElement("Registers", weight.Registers.Select((reg, k) => new XElement($"Reg{k}",
                                                                                                                                                                                                                                                     new XElement("NeedTwoRegisters", reg.NeedTwoRegisters),
                                                                                                                                                                                                                                                     new XElement("ValueRegister", reg.ValueRegister),
                                                                                                                                                                                                                                                     new XElement("DataType", reg.DataType),
                                                                                                                                                                                                                                                     new XElement("Path",
                                                                                                                                                                                                                                                                  reg.Path.Split(Register.PathDel).Select(path => new XElement(path, path)))))))))))));

            XDocument s = new XDocument(contacts);

            s.Save(Filepath);
        }
Ejemplo n.º 6
0
        public void WebSources_Execute_WebRequestMethod_Put_ExpectExpectBase64String()
        {
            var responseFromWeb      = Encoding.ASCII.GetBytes("response from web request");
            var mockWebClientWrapper = new Mock <IWebClientWrapper>();

            mockWebClientWrapper.Setup(o => o.Headers).Returns(new WebHeaderCollection {
                "a:x", "b:e"
            });
            mockWebClientWrapper.Setup(o => o.UploadData(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <byte[]>()))
            .Returns(responseFromWeb);

            var source = new WebSource
            {
                Address            = "http://www.msn.com/",
                AuthenticationType = AuthenticationType.Anonymous,
                Client             = mockWebClientWrapper.Object
            };

            var result = WebSources.Execute(source, WebRequestMethod.Put, "http://www.msn.com/", "", false, out var errors, new string[] { });

            Assert.IsTrue(IsBase64(result));
            Assert.AreEqual(result, Convert.ToBase64String(responseFromWeb));

            var client = source.Client;

            Assert.IsFalse(errors.HasErrors(), "On executing without multipart form data web client source returned at least one error: " + (errors.HasErrors() ? errors.FetchErrors()[0] : ""));
            Assert.IsTrue(client.Headers.AllKeys.Contains("a"));
            Assert.IsTrue(client.Headers.AllKeys.Contains("b"));

            mockWebClientWrapper.Verify(o => o.UploadData(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <byte[]>()), Times.Once);
        }
Ejemplo n.º 7
0
        public void WebSources_AssertUserAgentHeaderSet_SetsUserNameAndPassword()
        {
            var source = new WebSource
            {
                Address            = "www.foo.bar",
                AuthenticationType = AuthenticationType.User,
                UserName           = "******",
                Password           = "******",
                Client             = new WebClientWrapper
                {
                    Headers = new WebHeaderCollection
                    {
                        "a:x",
                        "b:e"
                    }
                }
            };

            _ = WebSources.Execute(source, WebRequestMethod.Get, "http://consoto.com", "test data", false, out ErrorResultTO error);

            var client            = source.Client;
            var clientCredentials = client.Credentials as NetworkCredential;

            Assert.IsNotNull(clientCredentials);
            Assert.IsTrue(clientCredentials.UserName == "User");
            Assert.IsTrue(clientCredentials.Password == "pwd");
        }
Ejemplo n.º 8
0
        public void WebSources_AssertUserAgentHeaderSet_SetsOtherHeaders()
        {
            var source = new WebSource
            {
                Address            = "www.foo.bar",
                AuthenticationType = AuthenticationType.User,
                UserName           = "******",
                Password           = "******",
                Client             = new WebClientWrapper
                {
                    Headers = new WebHeaderCollection
                    {
                        "a:x"
                    }
                }
            };

            _ = WebSources.Execute(source, WebRequestMethod.Get, "http://consoto.com", "test data", false, out ErrorResultTO error, new string[] { "b:e" });

            var client = source.Client;
            var agent  = client.Headers["user-agent"];

            Assert.AreEqual(agent, GlobalConstants.UserAgentString);
            Assert.IsTrue(client.Headers.AllKeys.Contains("a"));
            Assert.IsTrue(client.Headers.AllKeys.Contains("b"));
        }
Ejemplo n.º 9
0
        public void WebSourcesSaveWithInValidArgsExpectedInvalidValidationResult()
        {
            var handler    = new WebSources();
            var jsonResult = handler.Save("root:'hello'", Guid.Empty, Guid.Empty);
            var result     = JsonConvert.DeserializeObject <ValidationResult>(jsonResult);

            Assert.IsFalse(result.IsValid);
        }
Ejemplo n.º 10
0
        public void WebSourcesGetWithInvalidArgsExpectedReturnsNewSource()
        {
            var handler = new WebSources();
            var result  = handler.Get("xxxxx", Guid.Empty, Guid.Empty);

            Assert.IsNotNull(result);
            Assert.AreEqual(Guid.Empty, result.ResourceID);
        }
Ejemplo n.º 11
0
        public void WebSourcesConstructorWithNullResourceCatalogExpectedThrowsArgumentNullException()
        {
#pragma warning disable 168
            // ReSharper disable UnusedVariable
            var handler = new WebSources(null);
            // ReSharper restore UnusedVariable
#pragma warning restore 168
        }
Ejemplo n.º 12
0
        public void WebSources_ConstructorWithNullResourceCatalogExpectedThrowsArgumentNullException()
        {
#pragma warning disable 168

            var handler = new WebSources(null);

#pragma warning restore 168
        }
Ejemplo n.º 13
0
        public void WebSources_Execute_Null_WebSource_Expect_Correct_ErrorMessage()
        {
            var result = WebSources.Execute(null, WebRequestMethod.Post, "http://www.msn.com/", "", false, out var errors, new string[] { });

            Assert.AreEqual("", result);
            var fetchErrors = errors.FetchErrors();

            Assert.AreEqual(1, fetchErrors.Count);
            Assert.AreEqual("The web source has an incomplete web address.", fetchErrors[0]);
        }
Ejemplo n.º 14
0
        public void WebSourcesTestWithInvalidAddressExpectedInvalidValidationResult()
        {
            var source = new WebSource {
                Address = "www.foo.bar", AuthenticationType = AuthenticationType.Anonymous
            }.ToString();

            var handler = new WebSources();
            var result  = handler.Test(source, Guid.Empty, Guid.Empty);

            Assert.IsFalse(result.IsValid, result.ErrorMessage);
        }
Ejemplo n.º 15
0
        public void WebSources_Test_WithInvalidAddress_ExpectedInvalidValidationResult()
        {
            var source = new WebSource {
                AuthenticationType = AuthenticationType.Anonymous
            }.ToString();

            var handler = new WebSources();
            var result  = handler.Test(source);

            Assert.IsFalse(result.IsValid, result.ErrorMessage);
        }
Ejemplo n.º 16
0
        protected override void ParseUriBody(string bodyUri)
        {
            base.ParseUriBody(bodyUri);

            var segments   = bodyUri.Trim('/').Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            var parseIndex = 1;

            TypeString = segments[0];
            if (segments[0] == "file")
            {
                FileName = segments[parseIndex++];
                Size     = segments[parseIndex++].ToInt64();
                FileHash = segments[parseIndex++];
            }
            else if (segments[0] == "serverlist")
            {
                SourceUrl = segments[parseIndex++];
            }
            else if (segments[0] == "nodeslist")
            {
                SourceUrl = segments[parseIndex++];
            }
            else if (segments[0] == "server")
            {
                ServerAddress = new DnsEndPoint(segments[parseIndex++], segments[parseIndex++].ToInt32());
            }
            for (int i = parseIndex; i < segments.Length; i++)
            {
                var segment = segments[i];

                if (segment.StartsWith("s="))
                {
                    //WEB链接
                    WebSources.Add(System.Web.HttpUtility.UrlDecode(segment.Remove(0, 2), Encoding.UTF8));
                }
                else if (segment.StartsWith("sources,"))
                {
                    Sources.AddRange(segment.Remove(0, 8).Split(',').Select(s =>
                    {
                        var arg = s.Split(':');
                        return(new DnsEndPoint(arg[0], arg[1].ToInt32()));
                    }).ToArray());
                }
                else if (segment.StartsWith("h="))
                {
                    RootHash = segment.Remove(0, 2);
                }
                else if (segment.StartsWith("p="))
                {
                    HashSet.AddRange(segment.Remove(0, 2).Split(':'));
                }
            }
        }
Ejemplo n.º 17
0
        public void WebSourcesAssertUserAgentHeaderSet()
        {
            var source = new WebSource {
                Address = "www.foo.bar", AuthenticationType = AuthenticationType.Anonymous
            };

            WebSources.EnsureWebClient(source, new List <string>());

            var client = source.Client;
            var agent  = client.Headers["user-agent"];

            Assert.IsNotNull(agent);
            Assert.AreEqual(agent, GlobalConstants.UserAgentString);
        }
Ejemplo n.º 18
0
        public void WebSources_Test_WithInValidArgs_And_SourceExecuteFails_ExpectedValidInvalidationResult()
        {
            var mockWebSource = new Mock <IWebSource>();

            mockWebSource.Setup(o => o.Address)
            .Returns("http://localhost:2121");
            mockWebSource.Setup(o => o.Client)
            .Throws(new Exception("test: false exception"));

            var handler = new WebSources();
            var result  = handler.Test(mockWebSource.Object);

            Assert.IsFalse(result.IsValid, result.ErrorMessage);
            Assert.AreEqual("test: false exception", result.ErrorMessage.Trim());
        }
Ejemplo n.º 19
0
        public void WebSources_PerformMultipartWebRequest_SetsContentTypeHeaders()
        {
            var source = new WebSource {
                Address = "http://www.msn.com/", AuthenticationType = AuthenticationType.Anonymous
            };

            WebSources.CreateWebClient(source, new List <string> {
                "a:x", "b:e", "Content-Type: multipart/form-data"
            });
            WebSources.PerformMultipartWebRequest(source.Client, source.Address, "");
            var client      = source.Client;
            var contentType = client.Headers["Content-Type"];

            Assert.IsNotNull(contentType);
            Assert.AreEqual("multipart/form-data", contentType);
        }
Ejemplo n.º 20
0
        public void WebSourcesSaveWithValidArgsExpectedInvokesResourceCatalogSave()
        {
            var expected = CreateWebSource();

            var catalog = new Mock <IResourceCatalog>();

            catalog.Setup(c => c.SaveResource(It.IsAny <Guid>(), It.IsAny <IResource>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())).Verifiable();

            var handler    = new WebSources(catalog.Object);
            var jsonResult = handler.Save(expected.ToString(), Guid.Empty, Guid.Empty);
            var actual     = JsonConvert.DeserializeObject <WebSource>(jsonResult);

            catalog.Verify(c => c.SaveResource(It.IsAny <Guid>(), It.IsAny <IResource>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>()));

            VerifySource(expected, actual);
        }
Ejemplo n.º 21
0
        /// <summary>
        /// 将当前的地址信息组合成URL地址
        /// </summary>
        /// <returns></returns>
        protected override void GenerateUrl(StringBuilder sb)
        {
            base.GenerateUrl(sb);

            var list = new List <string>(10)
            {
                "", TypeString
            };

            if (UrlType == LinkType.File)
            {
                list.Add(FileName);
                list.Add(Size.ToString());
                list.Add(FileHash);

                if (!RootHash.IsNullOrEmpty())
                {
                    list.Add("h=" + RootHash);
                }
                if (HashSet.Count > 0)
                {
                    list.Add("p=" + string.Join(":", HashSet.ToArray()));
                }
                if (WebSources.Count > 0)
                {
                    WebSources.ForEach(s => list.Add("s=" + s));
                }
                if (Sources.Count > 0)
                {
                    list.Add("/");
                    list.Add("sources," + string.Join(",", Sources.Select(s => s.Host + ":" + s.Port).ToArray()));
                }
            }
            else if (UrlType == LinkType.NodesList || UrlType == LinkType.ServerList)
            {
                list.Add(SourceUrl);
            }
            else if (UrlType == LinkType.Server)
            {
                var host = ServerAddress;
                list.Add(host.Host);
                list.Add(host.Port.ToString());
            }

            sb.Append(string.Join("|", list.ToArray()));
            sb.Append("/");
        }
Ejemplo n.º 22
0
        public void WebSources__Execute_Without_MultiPart()
        {
            var headerString = new List <string> {
                "a:x", "b:e"
            };
            var source = new WebSource {
                Address = "http://www.msn.com/", AuthenticationType = AuthenticationType.Anonymous
            };

            WebSources.Execute(source, WebRequestMethod.Post, "http://www.msn.com/", "", false, out var errors, headerString.ToArray());

            var client = source.Client;

            Assert.IsFalse(errors.HasErrors(), "On executing without multipart form data web client source returned at least one error: " + (errors.HasErrors()?errors.FetchErrors()[0]:""));
            Assert.IsTrue(client.Headers.AllKeys.Contains("a"));
            Assert.IsTrue(client.Headers.AllKeys.Contains("b"));
        }
Ejemplo n.º 23
0
        public void WebSources_AssertUserAgentHeaderSet_SetsUserNameAndPassword()

        {
            var source = new WebSource {
                Address = "www.foo.bar", AuthenticationType = AuthenticationType.User, UserName = "******", Password = "******"
            };

            WebSources.CreateWebClient(source, new List <string> {
                "a:x", "b:e"
            });

            var client = source.Client;

            Assert.IsTrue((client.Credentials as NetworkCredential).UserName == "User");

            Assert.IsTrue((client.Credentials as NetworkCredential).Password == "pwd");
        }
Ejemplo n.º 24
0
        public void WebSourcesAssertUserAgentHeaderSet_SetsOtherHeaders()
        {
            var source = new WebSource {
                Address = "www.foo.bar", AuthenticationType = AuthenticationType.Anonymous
            };

            WebSources.EnsureWebClient(source, new List <string> {
                "a:x", "b:e"
            });

            var client = source.Client;
            var agent  = client.Headers["user-agent"];

            Assert.IsNotNull(agent);
            Assert.AreEqual(agent, GlobalConstants.UserAgentString);
            Assert.IsTrue(client.Headers.AllKeys.Contains("a"));
            Assert.IsTrue(client.Headers.AllKeys.Contains("b"));
        }
Ejemplo n.º 25
0
        public void WebSources_Execute_SetsContentTypeHeaders_multipart()
        {
            var headerString = new List <string> {
                "a:x", "b:e", "Content-Type: multipart/form-data"
            };
            var source = new WebSource {
                Address = "http://www.msn.com/", AuthenticationType = AuthenticationType.Anonymous
            };

            WebSources.Execute(source, WebRequestMethod.Post, "http://www.msn.com/", "", false, out var errors, headerString.ToArray());

            var client      = source.Client;
            var contentType = client.Headers["Content-Type"];

            Assert.IsNotNull(contentType);
            Assert.IsFalse(errors.HasErrors(), "On executing with multipart form data web client source returned at least one error: " + (errors.HasErrors()?errors.FetchErrors()[0]:""));
            Assert.AreEqual("multipart/form-data", contentType);
        }
Ejemplo n.º 26
0
        public void WebSourcesAssertUserAgentHeaderSet_SetsUserNameAndPassword()

        {
            var source = new WebSource {
                Address = "www.foo.bar", AuthenticationType = AuthenticationType.User, UserName = "******", Password = "******"
            };

            WebSources.EnsureWebClient(source, new List <string> {
                "a:x", "b:e"
            });

            var client = source.Client;

            // ReSharper disable PossibleNullReferenceException
            Assert.IsTrue((client.Credentials as NetworkCredential).UserName == "User");

            Assert.IsTrue((client.Credentials as NetworkCredential).Password == "pwd");
            // ReSharper restore PossibleNullReferenceException
        }
Ejemplo n.º 27
0
        public void WebSources_HttpNewLine()
        {
            const string data =
                @"Accept-Language: en-US,en;q=0.5
                Accept-Encoding: gzip, deflate
                Cookie: __atuvc=34%7C7; permanent=0; _gitlab_session=226ad8a0be43681acf38c2fab9497240; __profilin=p%3Dt; request_method=GET
                Connection: keep-alive
                Content-Type: multipart/form-data; boundary=---------------------------9051914041544843365972754266
                Content-Length: 554

                -----------------------------9051914041544843365972754266
                Content-Disposition: form-data; name=""text""

                text default
                -----------------------------9051914041544843365972754266
                Content-Disposition: form-data; name=""file1""; filename=""a.txt""
                Content-Type: text/plain

                Content of a.txt.

                -----------------------------9051914041544843365972754266
                Content-Disposition: form-data; name=""file2""; filename=""a.html""
                Content-Type: text/html

                <!DOCTYPE html><title>Content of a.html.</title>

                -----------------------------9051914041544843365972754266--";
            var rData    = data;
            var byteData = WebSources.ConvertToHttpNewLine(ref rData);

            string expected = data;
            string result   = Encoding.UTF8.GetString(byteData);

            Assert.AreEqual(expected, result);

            rData    = data.Replace("\r\n", "\n");
            byteData = WebSources.ConvertToHttpNewLine(ref rData);

            result = Encoding.UTF8.GetString(byteData);

            Assert.AreEqual(expected, result);
        }
Ejemplo n.º 28
0
        public void WebSources_Test_WithInValidArgs_ExpectedValidValidationResult()
        {
            var mockWebClientWrapper = new Mock <IWebClientWrapper>();

            mockWebClientWrapper.Setup(o => o.Headers).Returns(new WebHeaderCollection {
                "a:x", "b:e"
            });

            var source = new WebSource
            {
                Address      = "http://sample.com",
                DefaultQuery = "/default",
                Client       = mockWebClientWrapper.Object
            };

            var handler = new WebSources();
            var result  = handler.Test(source);

            Assert.IsTrue(result.IsValid);
        }
Ejemplo n.º 29
0
        public void WebSources_AssertUserAgentHeaderSet()
        {
            var source = new WebSource
            {
                Address            = "www.foo.bar",
                AuthenticationType = AuthenticationType.Anonymous,
                Client             = new WebClientWrapper
                {
                    Headers = new WebHeaderCollection
                    {
                        "a:x",
                        "b:e"
                    }
                }
            };

            _ = WebSources.Execute(source, WebRequestMethod.Get, "http://consoto.com", "test data", false, out ErrorResultTO error);

            var client = source.Client;
            var agent  = client.Headers["user-agent"];

            Assert.AreEqual(agent, GlobalConstants.UserAgentString);
        }
Ejemplo n.º 30
0
        public StringBuilder Execute(Dictionary <string, StringBuilder> values, IWorkspace theWorkspace)
        {
            ExecuteMessage     msg        = new ExecuteMessage();
            Dev2JsonSerializer serializer = new Dev2JsonSerializer();

            try
            {
                Dev2Logger.Info("Test WebserviceSource");
                StringBuilder resourceDefinition;

                values.TryGetValue("WebserviceSource", out resourceDefinition);

                var src    = serializer.Deserialize <WebServiceSourceDefinition>(resourceDefinition);
                var con    = new WebSources();
                var result = con.Test(new WebSource
                {
                    Address            = src.HostName,
                    DefaultQuery       = src.DefaultQuery,
                    AuthenticationType = src.AuthenticationType,
                    UserName           = src.UserName,
                    Password           = src.Password
                });


                msg.HasError = false;
                msg.Message  = new StringBuilder(result.IsValid ? serializer.Serialize(result.Result) : result.ErrorMessage);
                msg.HasError = !result.IsValid;
            }
            catch (Exception err)
            {
                msg.HasError = true;
                msg.Message  = new StringBuilder(err.Message);
                Dev2Logger.Error(err);
            }

            return(serializer.SerializeToBuilder(msg));
        }