Пример #1
0
		public Tuple<bool, string> Ping(NetworkConnection connection)
		{
			_log.Debug(string.Format("Make request to: {0}", connection.Uri));

			var request = (HttpWebRequest)WebRequest.Create(connection.Uri);

			if (connection.HasCredentials)
			{
				request.UseDefaultCredentials = false;
				request.Credentials = connection.GetCredentials();
			
				_log.Debug(string.Format("With credentials username: {0} domain: {1}", connection.GetCredentials().UserName, connection.GetCredentials().Domain));
			}

			try
			{
				using (var response = (HttpWebResponse)request.GetResponse())
				{
					var responseUrl = response.ResponseUri.AbsoluteUri;

					_log.Debug(string.Format("Response url was: {0}", responseUrl));
					_log.Debug(string.Format("Response code: {0}", response.StatusDescription));

					var result = connection.Uri.AbsoluteUri == responseUrl;
					
					return new Tuple<bool, string>(result, response.StatusDescription);
				}
			}
			catch (WebException ex)
			{
				_log.Error(ex);
				return new Tuple<bool, string>(false, ex.Message);
			}
		}
        public void Ctor_SetsUri()
        {
            // arrange
            var url = "http://www.test.com/elmah.axd";

            // act
            var connection = new NetworkConnection(url);

            // assert
            Assert.That(connection.Uri.AbsoluteUri, Is.EqualTo(url));
        }
        public ErrorLogDownloader(NetworkConnection connection, IWebRequestHelper webRequst, IFileSystemHelper fileSystemHelper, ICsvParser csvParser, ISettingsManager settingsManager)
        {
            Connection = connection;

            _webRequst = webRequst;
            _fileSystemsHelper = fileSystemHelper;
            _csvParser = csvParser;
            _settingsManager = settingsManager;

            ResolveDownloadDirectory();
        }
		private static NetworkConnection ResolveErrorLogDownloadUrl(NetworkConnection connection, KeyValuePair<Uri, DateTime> entry)
		{
			var url = entry.Key.AbsoluteUri.Replace("/detail?", "/xml?");
			
			if (connection.IsHttps)
			{
				url = url.Replace("http:", "https:");
			}

			return connection.CopyWithCredentials(url);
		}
		public void HasCredentials_CredentialsNotSet_IsFalse()
		{
			// arrange
			var url = "http://www.test.com/elmah.axd";
			var connection = new NetworkConnection(url);

			// act
			var result = connection.HasCredentials;

			// assert
			Assert.That(result, Is.False);
		}
        public void GetCredentials_CredentialsNotSet_ReturnsNull()
        {
            // arrange
            var url = "http://www.test.com/elmah.axd";
            var connection = new NetworkConnection(url);

            // act
            var result = connection.GetCredentials();

            // assert
            Assert.That(result, Is.Null);
        }
        public void GetCredentials_CredentialsIsSet_ReturnsCredentials()
        {
            // arrange
            var url = "http://www.test.com/elmah.axd";
            var connection = new NetworkConnection(url);

            connection.SetCredentials("pelle", "password", "domain1");

            // act
            var result = connection.GetCredentials();

            // assert
            Assert.That(result, Is.Not.Null);
        }
        public void CreateWithCredentials_CreatesNewConnectionAndCopiesCredentialsFromTheOriginal()
        {
            // arrange
            var url = "http://www.test.com/elmah.axd";
            var original = new NetworkConnection(url);

            original.SetCredentials("pelle", "password", "domain1");

            // act
            var copy = original.CopyWithCredentials("http://www.copy.com/elmah.axd");

            // assert
            Assert.That(copy.Uri.AbsoluteUri, Is.EqualTo("http://www.copy.com/elmah.axd"));
            Assert.That(copy.GetCredentials(), Is.EqualTo(original.GetCredentials()));
        }
        public void Download_DowloadDirectoryDoesNotExist_ShouldCreateDownloadDirectoryNamedSameAsUrlHost()
        {
            // arrange
            var connection = new NetworkConnection("http://www.test.com/elmah.axd");
            _fileSystemHelper.Setup(x => x.DirectoryExists("c:\\test\\www.test.com")).Returns(false);
            SetUpDefaultPath();

            var downloader = CreateDownloader(connection);

            // act
            downloader.Download();

            // assert
            _fileSystemHelper.Verify(x => x.CreateDirectory("c:\\test\\www.test.com_80"), Times.Once());
        }
        public void GetCredentials_CredentialsIsSet_ShouldHaveUsernamePasswordAndDomain()
        {
            // arrange
            var url = "http://www.test.com/elmah.axd";
            var connection = new NetworkConnection(url);

            connection.SetCredentials("pelle", "password", "domain1");

            // act
            var result = connection.GetCredentials();

            // assert
            Assert.That(result.UserName, Is.EqualTo("pelle"));
            Assert.That(result.Password, Is.EqualTo("password"));
            Assert.That(result.Domain, Is.EqualTo("domain1"));
        }
        private void View_OnConnectToServer(object sender, ConnectToServerEventArgs e)
        {
            View.ClearErrorMessage();

            if (!e.Url.HasValue())
            {
                View.DisplayErrorMessage("Invalid url");
                return;
            }

            try
            {
                var elmahUrl = new ElmahUrlHelper().ResolveElmahRootUrl(e.Url);
                var connection = new NetworkConnection(elmahUrl);

                if (e.HasCredentials)
                {
                    connection.SetCredentials(e.UserName, e.Password, e.Domain);
                }

                var serverResponded = _urlPing.Ping(connection);

                if (!serverResponded.Item1)
                {
                    View.DisplayErrorMessage(serverResponded.Item2);
                    return;
                }

                Connnection = connection;

                View.CloseView();
            }
            catch (ArgumentException)
            {
                View.DisplayErrorMessage("Invalid url");
            }
            catch (Exception ex)
            {
                View.DisplayErrorMessage(ex.Message);
            }
        }
Пример #12
0
		public string Uri(NetworkConnection	connection)
		{
			string result;

			using (var client = new WebClient())
			{
				if (connection.HasCredentials)
				{
					client.UseDefaultCredentials = false;
					client.Credentials = connection.GetCredentials();
				}

				var response = client.OpenRead(connection.Uri.AbsoluteUri);
				
				// TODO Check content-type is the expected one!
				// TODO Use charset in response Content-Type header
				using (var reader = new StreamReader(response))
				{
					result = reader.ReadToEnd();
				}
			}

			return result;
		}
Пример #13
0
		private static void InitializeNewErrorLogSource(ErrorLogSources source, string connection, string schema, string application, NetworkConnection networkConnection)
		{
			_container.SetLoadingState();

			DataSourceScopeController.SetNewSource(source, connection, schema, application);

			var downloadLogsTask  = new Task(() => { return; });

			if (networkConnection != null)
			{
				var downloader = ServiceLocator.ResolveWithConstructorArguments<ErrorLogDownloader>(new IParameter[] { new ConstructorArgument("connection", networkConnection) });
				DataSourceScopeController.SetNewSource(ErrorLogSources.Files, downloader.DownloadDirectory, null, null);

				downloadLogsTask = new Task(downloader.Download);
			}
 
			var repository = ServiceLocator.Resolve<IErrorLogRepository>();
			var viewPresenter = ServiceLocator.Resolve<SearchPresenter>();

			var initRepositoryTask = downloadLogsTask.ContinueWith(previousTask =>
			{
				if (previousTask.Exception != null)
				{
					_container.InvokeEx(m => m.DisplayView(new ErrorView(previousTask.Exception)));
					_container.InvokeEx(m => m.SetInitialState());
					return;
				}

				repository.Initialize();
			});

			var updateUiTask = initRepositoryTask.ContinueWith(previousTask =>
			{
				if (previousTask.Exception != null)
				{
					_container.InvokeEx(m => m.DisplayView(new ErrorView(previousTask.Exception)));
					_container.InvokeEx(m => m.SetInitialState());
					return;
				}

				_container.InvokeEx(m => m.SetReadyForWorkState());
				_container.InvokeEx(m => m.DisplayConnectionInformation(DataSourceScopeController.Source, DataSourceScopeController.Connection));
				_container.InvokeEx(m => m.DisplayView(viewPresenter.View as UserControl));
			});
			
			downloadLogsTask.Start();
		}
 private IErrorLogDownloader CreateDownloader(NetworkConnection connection)
 {
     return new ErrorLogDownloader(connection, _webRequestHelper.Object, _fileSystemHelper.Object, _csvParser.Object, _settingsManager);
 }
        public void IsHttps_UriSchemeIsNotHttps_IsFalse()
        {
            // arrange
            var url = "http://www.test.com/elmah.axd";
            var connection = new NetworkConnection(url);

            // act
            var result = connection.IsHttps;

            // assert
            Assert.That(result, Is.False);
        }
 public NetworkConnection CopyWithCredentials(string url)
 {
     var connection = new NetworkConnection(url) { Credentials = GetCredentials() };
     return connection;
 }
 public Tuple<bool, string> Ping(NetworkConnection connection)
 {
     return new Tuple<bool, string>(_returnValue, _returnMessage);
 }