<commit_before>﻿using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using DotNetTor.SocksPort;
using Xunit;

namespace DotNetTor.Tests
{
	// For proper configuraion see https://github.com/nopara73/DotNetTor
	public class SocksPortTests
	{
		[Fact]
		public async Task CanDoBasicRequestAsync()
		{
			var requestUri = "http://api.qbit.ninja/whatisit/what%20is%20my%20future";
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				HttpResponseMessage message = await httpClient.GetAsync(requestUri);
				var content = await message.Content.ReadAsStringAsync();

				Assert.Equal("\"Good question Holmes !\"", content);
			}
		}

		[Fact]
		public async Task CanReuseHandlerAsync()
		{
			// YOU HAVE TO SET THE HTTP CLIENT NOT TO DISPOSE THE HANDLER
			var requestUri = "http://api.qbit.ninja/whatisit/what%20is%20my%20future";
			var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort);
			using (var httpClient = new HttpClient(handler, disposeHandler: false))
			{
				HttpResponseMessage message = await httpClient.GetAsync(requestUri);
				var content = await message.Content.ReadAsStringAsync();

				Assert.Equal("\"Good question Holmes !\"", content);
			}
			using (var httpClient = new HttpClient(handler))
			{
				HttpResponseMessage message = await httpClient.GetAsync(requestUri);
				var content = await message.Content.ReadAsStringAsync();

				Assert.Equal("\"Good question Holmes !\"", content);
			}
			using (var httpClient = new HttpClient(handler))
			{
				HttpResponseMessage message = await httpClient.GetAsync(requestUri);
				var content = await message.Content.ReadAsStringAsync();

				Assert.Equal("\"Good question Holmes !\"", content);
			}
		}

		[Fact]
		public async Task CanRequestDifferentWithSameHandlerAsync()
		{
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				HttpResponseMessage message =
					await httpClient.GetAsync("http://api.qbit.ninja/whatisit/what%20is%20my%20future");
				var content = await message.Content.ReadAsStringAsync();
				Assert.Equal("\"Good question Holmes !\"", content);

				message = await httpClient.GetAsync("https://api.ipify.org/");
				content = await message.Content.ReadAsStringAsync();
				var gotIp = IPAddress.TryParse(content.Replace("\n", ""), out IPAddress ip);
				Assert.True(gotIp);


				message = await httpClient.GetAsync("http://api.qbit.ninja/whatisit/what%20is%20my%20future");
				content = await message.Content.ReadAsStringAsync();
				Assert.Equal("\"Good question Holmes !\"", content);
			}
		}

		[Fact]
		private static async Task TorIpIsNotTheRealOneAsync()
		{
			var requestUri = "https://api.ipify.org/";
			IPAddress realIp;
			IPAddress torIp;

			// 1. Get real IP
			using (var httpClient = new HttpClient())
			{
				var content = await (await httpClient.GetAsync(requestUri)).Content.ReadAsStringAsync();
				var gotIp = IPAddress.TryParse(content.Replace("\n", ""), out realIp);
				Assert.True(gotIp);
			}

			// 2. Get Tor IP
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				var content =
					await (await httpClient.GetAsync(requestUri)).Content.ReadAsStringAsync()
						;
				var gotIp = IPAddress.TryParse(content.Replace("\n", ""), out torIp);
				Assert.True(gotIp);
			}

			Assert.NotEqual(realIp, torIp);
		}

		[Fact]
		public async Task CanDoHttpsAsync()
		{
			var requestUri = "https://slack.com/api/api.test";
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				var content =
					await (await httpClient.GetAsync(requestUri)).Content.ReadAsStringAsync()
						;

				Assert.Equal("{\"ok\":true}", content);
			}
		}

		[Fact]
		public async Task CanRequestInRowAsync()
		{
			var firstRequest = "http://api.qbit.ninja/transactions/38d4cfeb57d6685753b7a3b3534c3cb576c34ca7344cd4582f9613ebf0c2b02a?format=json&headeronly=true";

			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				await (await httpClient.GetAsync(firstRequest)).Content.ReadAsStringAsync();
				await (await httpClient.GetAsync("http://api.qbit.ninja/balances/15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe?unspentonly=true")).Content.ReadAsStringAsync();
				await (await httpClient.GetAsync("http://api.qbit.ninja/balances/akEBcY5k1dn2yeEdFnTMwdhVbHxtgHb6GGi?from=tip&until=336000")).Content.ReadAsStringAsync();
			}
		}

		[Fact]
		public async Task CanRequestInRowHttpsAsync()<commit_msg>add connect to ip test<commit_after>﻿using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using DotNetTor.SocksPort;
using Xunit;

namespace DotNetTor.Tests
{
	// For proper configuraion see https://github.com/nopara73/DotNetTor
	public class SocksPortTests
	{
		[Fact]
		public async Task CanDoBasicRequestAsync()
		{
			var requestUri = "http://api.qbit.ninja/whatisit/what%20is%20my%20future";
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				HttpResponseMessage message = await httpClient.GetAsync(requestUri);
				var content = await message.Content.ReadAsStringAsync();

				Assert.Equal("\"Good question Holmes !\"", content);
			}
		}

		[Fact]
		public async Task CanReuseHandlerAsync()
		{
			// YOU HAVE TO SET THE HTTP CLIENT NOT TO DISPOSE THE HANDLER
			var requestUri = "http://api.qbit.ninja/whatisit/what%20is%20my%20future";
			var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort);
			using (var httpClient = new HttpClient(handler, disposeHandler: false))
			{
				HttpResponseMessage message = await httpClient.GetAsync(requestUri);
				var content = await message.Content.ReadAsStringAsync();

				Assert.Equal("\"Good question Holmes !\"", content);
			}
			using (var httpClient = new HttpClient(handler))
			{
				HttpResponseMessage message = await httpClient.GetAsync(requestUri);
				var content = await message.Content.ReadAsStringAsync();

				Assert.Equal("\"Good question Holmes !\"", content);
			}
			using (var httpClient = new HttpClient(handler))
			{
				HttpResponseMessage message = await httpClient.GetAsync(requestUri);
				var content = await message.Content.ReadAsStringAsync();

				Assert.Equal("\"Good question Holmes !\"", content);
			}
		}

		[Fact]
		public async Task CanRequestDifferentWithSameHandlerAsync()
		{
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				HttpResponseMessage message =
					await httpClient.GetAsync("http://api.qbit.ninja/whatisit/what%20is%20my%20future");
				var content = await message.Content.ReadAsStringAsync();
				Assert.Equal("\"Good question Holmes !\"", content);

				message = await httpClient.GetAsync("https://api.ipify.org/");
				content = await message.Content.ReadAsStringAsync();
				var gotIp = IPAddress.TryParse(content.Replace("\n", ""), out IPAddress ip);
				Assert.True(gotIp);


				message = await httpClient.GetAsync("http://api.qbit.ninja/whatisit/what%20is%20my%20future");
				content = await message.Content.ReadAsStringAsync();
				Assert.Equal("\"Good question Holmes !\"", content);
			}
		}

		[Fact]
		private static async Task TorIpIsNotTheRealOneAsync()
		{
			var requestUri = "https://api.ipify.org/";
			IPAddress realIp;
			IPAddress torIp;

			// 1. Get real IP
			using (var httpClient = new HttpClient())
			{
				var content = await (await httpClient.GetAsync(requestUri)).Content.ReadAsStringAsync();
				var gotIp = IPAddress.TryParse(content.Replace("\n", ""), out realIp);
				Assert.True(gotIp);
			}

			// 2. Get Tor IP
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				var content =
					await (await httpClient.GetAsync(requestUri)).Content.ReadAsStringAsync()
						;
				var gotIp = IPAddress.TryParse(content.Replace("\n", ""), out torIp);
				Assert.True(gotIp);
			}

			Assert.NotEqual(realIp, torIp);
		}

		[Fact]
		public async Task CanDoHttpsAsync()
		{
			var requestUri = "https://slack.com/api/api.test";
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				var content =
					await (await httpClient.GetAsync(requestUri)).Content.ReadAsStringAsync();

				Assert.Equal("{\"ok\":true}", content);
			}
		}

		[Fact]
		public async Task CanDoIpAddressAsync()
		{
			var requestUri = "http://172.16.31.10";
			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				var content =
					await (await httpClient.GetAsync(requestUri)).Content.ReadAsStringAsync();

				Assert.NotEmpty(content);
			}
		}

		[Fact]
		public async Task CanRequestInRowAsync()
		{
			var firstRequest = "http://api.qbit.ninja/transactions/38d4cfeb57d6685753b7a3b3534c3cb576c34ca7344cd4582f9613ebf0c2b02a?format=json&headeronly=true";

			using (var handler = new SocksPortHandler(Shared.HostAddress, Shared.SocksPort))
			using (var httpClient = new HttpClient(handler))
			{
				await (await httpClient.GetAsync(firstRequest)).Content.ReadAsStringAsync();
				await (await httpClient.GetAsync("http://api.qbit.ninja/balances/15sYbVpRh6dyWycZMwPdxJWD4xbfxReeHe?unspentonly=true")).Content.ReadAsStringAsync();
				await (await httpClient.GetAsync("http://api.qbit.ninja/balances/akEBcY5k1dn2yeEdFnTMwdhVbHxtgHb6GGi?from=tip&until=336000")).Content.ReadAsStringAsync();
			}
		}

		[Fact]
		public async Task CanRequestInRowHttpsAsync()
{'fraction_non_alphanumeric': 0.12454728370221328, 'fraction_numerical': 0.01740442655935614, 'mean_word_length': 4.199267782426778, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7542065', 'n_tokens_mistral': 3795, 'n_tokens_neox': 3171, 'n_words': 628}