<commit_before>﻿using MbDotNet.Enums;
using MbDotNet.Exceptions;
using MbDotNet.Models.Predicates;
using MbDotNet.Models.Predicates.Fields;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MbDotNet.Tests.Acceptance
{
	[TestClass, TestCategory("Acceptance")]
	public class ImposterTests : AcceptanceTestBase
	{
		private HttpClient _httpClient;

		public ImposterTests()
		{
			_httpClient = new HttpClient();
		}

		[TestInitialize]
		public async Task TestInitialize()
		{
			await _client.DeleteAllImpostersAsync();
		}

		[TestMethod]
		public async Task CanCreateAndGetHttpImposter()
		{
			const int port = 6000;
			var imposter =_client.CreateHttpImposter(port);

			await _client.SubmitAsync(imposter);

			var retrievedImposter = await _client.GetHttpImposterAsync(port);
			Assert.IsNotNull(retrievedImposter);
		}

		[TestMethod]
		public async Task CanCreateAndGetHttpsImposter()
		{
			const int port = 6000;
			var imposter = _client.CreateHttpsImposter(port);

			await _client.SubmitAsync(imposter);

			var retrievedImposter = await _client.GetHttpsImposterAsync(port);
			Assert.IsNotNull(retrievedImposter);
		}

		[TestMethod]
		public async Task CanCreateAndGetTcpImposter()
		{
			const int port = 6000;
			var imposter = _client.CreateTcpImposter(port);

			await _client.SubmitAsync(imposter);

			var retrievedImposter = await _client.GetTcpImposterAsync(port);
			Assert.IsNotNull(retrievedImposter);
		}

		[TestMethod]
		public async Task CanCreateHttpProxyImposter()
		{
			const int sourceImposterPort = 6000;
			const int proxyImposterPort = 6001;

			var sourceImposter = _client.CreateHttpImposter(sourceImposterPort);
			sourceImposter.AddStub().ReturnsStatus(System.Net.HttpStatusCode.OK);
			await _client.SubmitAsync(sourceImposter);

			var proxyImposter = _client.CreateHttpImposter(proxyImposterPort);
			var predicateGenerators = new List<MatchesPredicate<HttpBooleanPredicateFields>>
			{
				new MatchesPredicate<HttpBooleanPredicateFields>(new HttpBooleanPredicateFields
				{
					QueryParameters = true
				})
			};

			proxyImposter.AddStub().ReturnsProxy(
				new System.Uri($"http://localhost:{sourceImposterPort}"),
				ProxyMode.ProxyOnce, predicateGenerators);

			await _client.SubmitAsync(proxyImposter);

			// Make a request to the imposter to trigger the proxy
			var request = new HttpRequestMessage(HttpMethod.Get, $"http://localhost:{proxyImposterPort}/test?param=value");
			var response = await _httpClient.SendAsync(request);
			Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Request to proxy imposter failed");

			var retrievedSourceImposter = await _client.GetHttpImposterAsync(sourceImposterPort);
			Assert.IsNotNull(retrievedSourceImposter);
			Assert.AreEqual(1, retrievedSourceImposter.NumberOfRequests);
		}

		[TestMethod]
		public async Task CanCreateTcpProxyImposter()
		{
			const int sourceImposterPort = 6000;
			const int proxyImposterPort = 6001;

			var sourceImposter = _client.CreateTcpImposter(sourceImposterPort);
			sourceImposter.AddStub().ReturnsData("abc123");
			await _client.SubmitAsync(sourceImposter);

			var proxyImposter = _client.CreateTcpImposter(proxyImposterPort);
			var predicateGenerators = new List<MatchesPredicate<TcpBooleanPredicateFields>>
			{
				new MatchesPredicate<TcpBooleanPredicateFields>(new TcpBooleanPredicateFields
				{
					Data = true
				})
			};

			proxyImposter.AddStub().ReturnsProxy(
				new System.Uri($"tcp://localhost:{sourceImposterPort}"),
				ProxyMode.ProxyOnce, predicateGenerators);

			await _client.SubmitAsync(proxyImposter);

			// Make a request to the imposter to trigger the proxy
			using (var client = new TcpClient("localhost", proxyImposterPort))
			{
				var data = Encoding.ASCII.GetBytes("testdata");
				using (var stream = client.GetStream())
				{
					await stream.WriteAsync(data, 0, data.Length);
				}
			}<commit_msg>Fixed flaky TCP proxy test (#68)<commit_after>﻿using MbDotNet.Enums;
using MbDotNet.Exceptions;
using MbDotNet.Models.Predicates;
using MbDotNet.Models.Predicates.Fields;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace MbDotNet.Tests.Acceptance
{
	[TestClass, TestCategory("Acceptance")]
	public class ImposterTests : AcceptanceTestBase
	{
		private HttpClient _httpClient;

		public ImposterTests()
		{
			_httpClient = new HttpClient();
		}

		[TestInitialize]
		public async Task TestInitialize()
		{
			await _client.DeleteAllImpostersAsync();
		}

		[TestMethod]
		public async Task CanCreateAndGetHttpImposter()
		{
			const int port = 6000;
			var imposter =_client.CreateHttpImposter(port);

			await _client.SubmitAsync(imposter);

			var retrievedImposter = await _client.GetHttpImposterAsync(port);
			Assert.IsNotNull(retrievedImposter);
		}

		[TestMethod]
		public async Task CanCreateAndGetHttpsImposter()
		{
			const int port = 6000;
			var imposter = _client.CreateHttpsImposter(port);

			await _client.SubmitAsync(imposter);

			var retrievedImposter = await _client.GetHttpsImposterAsync(port);
			Assert.IsNotNull(retrievedImposter);
		}

		[TestMethod]
		public async Task CanCreateAndGetTcpImposter()
		{
			const int port = 6000;
			var imposter = _client.CreateTcpImposter(port);

			await _client.SubmitAsync(imposter);

			var retrievedImposter = await _client.GetTcpImposterAsync(port);
			Assert.IsNotNull(retrievedImposter);
		}

		[TestMethod]
		public async Task CanCreateHttpProxyImposter()
		{
			const int sourceImposterPort = 6000;
			const int proxyImposterPort = 6001;

			var sourceImposter = _client.CreateHttpImposter(sourceImposterPort);
			sourceImposter.AddStub().ReturnsStatus(System.Net.HttpStatusCode.OK);
			await _client.SubmitAsync(sourceImposter);

			var proxyImposter = _client.CreateHttpImposter(proxyImposterPort);
			var predicateGenerators = new List<MatchesPredicate<HttpBooleanPredicateFields>>
			{
				new MatchesPredicate<HttpBooleanPredicateFields>(new HttpBooleanPredicateFields
				{
					QueryParameters = true
				})
			};

			proxyImposter.AddStub().ReturnsProxy(
				new System.Uri($"http://localhost:{sourceImposterPort}"),
				ProxyMode.ProxyOnce, predicateGenerators);

			await _client.SubmitAsync(proxyImposter);

			// Make a request to the imposter to trigger the proxy
			var request = new HttpRequestMessage(HttpMethod.Get, $"http://localhost:{proxyImposterPort}/test?param=value");
			var response = await _httpClient.SendAsync(request);
			Assert.AreEqual(HttpStatusCode.OK, response.StatusCode, "Request to proxy imposter failed");

			var retrievedSourceImposter = await _client.GetHttpImposterAsync(sourceImposterPort);
			Assert.IsNotNull(retrievedSourceImposter);
			Assert.AreEqual(1, retrievedSourceImposter.NumberOfRequests);
		}

		[TestMethod]
		public async Task CanCreateTcpProxyImposter()
		{
			const int sourceImposterPort = 6000;
			const int proxyImposterPort = 6001;

			var sourceImposter = _client.CreateTcpImposter(sourceImposterPort);
			sourceImposter.AddStub().ReturnsData("abc123");
			await _client.SubmitAsync(sourceImposter);

			var proxyImposter = _client.CreateTcpImposter(proxyImposterPort);
			var predicateGenerators = new List<MatchesPredicate<TcpBooleanPredicateFields>>
			{
				new MatchesPredicate<TcpBooleanPredicateFields>(new TcpBooleanPredicateFields
				{
					Data = true
				})
			};

			proxyImposter.AddStub().ReturnsProxy(
				new System.Uri($"tcp://localhost:{sourceImposterPort}"),
				ProxyMode.ProxyOnce, predicateGenerators);

			await _client.SubmitAsync(proxyImposter);

			// Make a request to the imposter to trigger the proxy
			using (var client = new TcpClient("localhost", proxyImposterPort))
			{
				var data = Encoding.ASCII.GetBytes("testdata");
				using (var stream = client.GetStream())
				{
					await stream.WriteAsync(data, 0, data.Length);
					await stream.FlushAsync();
					await stream.ReadAsync(new byte[6], 0, 6);
				}
			}
{'fraction_non_alphanumeric': 0.0936930368209989, 'fraction_numerical': 0.008628022846032325, 'mean_word_length': 4.925125989920806, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6948518', 'n_tokens_mistral': 2953, 'n_tokens_neox': 2484, 'n_words': 492}