<commit_before>﻿using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Bloom.Api;
using NUnit.Framework;
using SIL.IO;

namespace BloomTests.web
{
	[TestFixture]
	class RequestInfoTests
	{

		[Test]
		public void RetrieveFileWithSpecialCharacters()
		{
			const string fileContents = @"\&<'@?>/" + "\r\n\"";
			using (var asciiFile = MakeTempFile(Encoding.ASCII.GetBytes(fileContents)))
			{
				using (var utf8File = MakeTempFile(Encoding.UTF8.GetBytes(fileContents)))
				{
					var request = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash);

					request.WriteCompleteOutput(File.ReadAllText(asciiFile.Path));
					var asciiString = request.ReplyContents;

					Assert.AreEqual(asciiString.Length, 11);
					Assert.AreEqual(asciiString[0], '\\');
					Assert.AreEqual(asciiString[1], '&');
					Assert.AreEqual(asciiString[2], '<');
					Assert.AreEqual(asciiString[3], '\'');
					Assert.AreEqual(asciiString[4], '@');
					Assert.AreEqual(asciiString[5], '?');
					Assert.AreEqual(asciiString[6], '>');
					Assert.AreEqual(asciiString[7], '/');
					Assert.AreEqual(asciiString[8], '\r');
					Assert.AreEqual(asciiString[9], '\n');
					Assert.AreEqual(asciiString[10], '"');

					request.WriteCompleteOutput(File.ReadAllText(utf8File.Path));
					var utf8String = request.ReplyContents;
					Assert.AreEqual(utf8String.Length, 11);
					Assert.AreEqual(utf8String[0], '\\');
					Assert.AreEqual(utf8String[1], '&');
					Assert.AreEqual(utf8String[2], '<');
					Assert.AreEqual(utf8String[3], '\'');
					Assert.AreEqual(utf8String[4], '@');
					Assert.AreEqual(utf8String[5], '?');
					Assert.AreEqual(utf8String[6], '>');
					Assert.AreEqual(utf8String[7], '/');
					Assert.AreEqual(utf8String[8], '\r');
					Assert.AreEqual(utf8String[9], '\n');
					Assert.AreEqual(utf8String[10], '"');
				}
			}
		}

		[TestCase("blah", "/blah")]
		[TestCase("bl%23ah", "/bl#ah")]
		[TestCase("bl?ah", "/bl")]
		[TestCase("bl%F4%80%80%8Aah", "/bl􀀊ah")] // private use character
		[TestCase("one + one", "/one + one")] // BL-3814. See http://stackoverflow.com/a/1006074/723299 
		[RequiresSTA]
		public void LocalPathWithoutQuery_SpecialCharactersDecodedCorrectly(string urlEnd, string expectedResult)
		{
			var listener = new HttpListener();
			// Nothing special about 33579. It is a simply a random number we hope is unused.
			// Note: if you get errors indicating the port is in use,
			// it is likely the listener.Stop() line below wasn't called somehow.
			var urlPrefix = "http://localhost:33579/";
			listener.Prefixes.Add(urlPrefix);

			new Thread(() =>
			{
				Thread.Sleep(100);
				WebRequest req = WebRequest.Create(urlPrefix + urlEnd);
				req.GetResponse();
			}).Start();

			try
			{
				listener.Start();
				var context = listener.GetContext(); // This waits for a request.
				var requestInfo = new RequestInfo(context);
				Assert.AreEqual(expectedResult, requestInfo.LocalPathWithoutQuery);
			}
			finally
			{
				listener.Stop();

				// Prevents each test case from stepping on the other
				// Not sure what the magic number is here.
				// 105 was sometimes failing on the build machine.
				// 200 seemed to usually fail for me locally (though 105 didn't).
				Thread.Sleep(300);
			}
		}

		private TempFile MakeTempFile(byte[] contents)
		{
			var file = TempFile.WithExtension(".tmp");
			File.Delete(file.Path);
			File.WriteAllBytes(file.Path, contents);
			return file;
		}
	}
}
<commit_msg>Fix fragile test that occasionally fails on TeamCity (#1197)<commit_after>﻿using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Bloom.Api;
using NUnit.Framework;
using SIL.IO;

namespace BloomTests.web
{
	[TestFixture]
	class RequestInfoTests
	{

		[Test]
		public void RetrieveFileWithSpecialCharacters()
		{
			const string fileContents = @"\&<'@?>/" + "\r\n\"";
			using (var asciiFile = MakeTempFile(Encoding.ASCII.GetBytes(fileContents)))
			{
				using (var utf8File = MakeTempFile(Encoding.UTF8.GetBytes(fileContents)))
				{
					var request = new PretendRequestInfo(ServerBase.ServerUrlWithBloomPrefixEndingInSlash);

					request.WriteCompleteOutput(File.ReadAllText(asciiFile.Path));
					var asciiString = request.ReplyContents;

					Assert.AreEqual(asciiString.Length, 11);
					Assert.AreEqual(asciiString[0], '\\');
					Assert.AreEqual(asciiString[1], '&');
					Assert.AreEqual(asciiString[2], '<');
					Assert.AreEqual(asciiString[3], '\'');
					Assert.AreEqual(asciiString[4], '@');
					Assert.AreEqual(asciiString[5], '?');
					Assert.AreEqual(asciiString[6], '>');
					Assert.AreEqual(asciiString[7], '/');
					Assert.AreEqual(asciiString[8], '\r');
					Assert.AreEqual(asciiString[9], '\n');
					Assert.AreEqual(asciiString[10], '"');

					request.WriteCompleteOutput(File.ReadAllText(utf8File.Path));
					var utf8String = request.ReplyContents;
					Assert.AreEqual(utf8String.Length, 11);
					Assert.AreEqual(utf8String[0], '\\');
					Assert.AreEqual(utf8String[1], '&');
					Assert.AreEqual(utf8String[2], '<');
					Assert.AreEqual(utf8String[3], '\'');
					Assert.AreEqual(utf8String[4], '@');
					Assert.AreEqual(utf8String[5], '?');
					Assert.AreEqual(utf8String[6], '>');
					Assert.AreEqual(utf8String[7], '/');
					Assert.AreEqual(utf8String[8], '\r');
					Assert.AreEqual(utf8String[9], '\n');
					Assert.AreEqual(utf8String[10], '"');
				}
			}
		}

		[TestCase("blah", "/blah")]
		[TestCase("bl%23ah", "/bl#ah")]
		[TestCase("bl?ah", "/bl")]
		[TestCase("bl%F4%80%80%8Aah", "/bl􀀊ah")] // private use character
		[TestCase("one + one", "/one + one")] // BL-3814. See http://stackoverflow.com/a/1006074/723299 
		[RequiresSTA]
		public void LocalPathWithoutQuery_SpecialCharactersDecodedCorrectly(string urlEnd, string expectedResult)
		{
			var listener = new HttpListener();
			// Nothing special about 33579. It is a simply a random number we hope is unused.
			// Note: if you get errors indicating the port is in use,
			// it is likely the listener.Stop() line below wasn't called somehow.
			var urlPrefix = "http://localhost:33579/";
			listener.Prefixes.Add(urlPrefix);

			var reqThread = new Thread(() =>
			{
				Thread.Sleep(100);
				WebRequest req = WebRequest.Create(urlPrefix + urlEnd);
				req.GetResponse();
			});
			reqThread.Start();

			try
			{
				listener.Start();
				var context = listener.GetContext(); // This waits for a request.
				var requestInfo = new RequestInfo(context);
				Assert.AreEqual(expectedResult, requestInfo.LocalPathWithoutQuery);
			}
			finally
			{
				listener.Stop();
				reqThread.Join();
			}
		}

		private TempFile MakeTempFile(byte[] contents)
		{
			var file = TempFile.WithExtension(".tmp");
			File.Delete(file.Path);
			File.WriteAllBytes(file.Path, contents);
			return file;
		}
	}
}

{'fraction_non_alphanumeric': 0.15946697905989163, 'fraction_numerical': 0.026358178357006882, 'mean_word_length': 3.7963483146067416, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, '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': '7044088', 'n_tokens_mistral': 2782, 'n_tokens_neox': 2209, 'n_words': 418}