Ejemplo n.º 1
0
		protected internal override async Task<HttpResponse> HandleRequest (
			TestContext ctx, HttpOperation operation, HttpConnection connection, HttpRequest request,
			RequestFlags effectiveFlags, CancellationToken cancellationToken)
		{
			switch (TestRunner.EffectiveType) {
			case HttpRequestTestType.LargeHeader:
			case HttpRequestTestType.LargeHeader2:
			case HttpRequestTestType.SendResponseAsBlob:
			case HttpRequestTestType.CloseRequestStream:
			case HttpRequestTestType.ReadTimeout:
			case HttpRequestTestType.SimpleGZip:
			case HttpRequestTestType.TestResponseStream:
			case HttpRequestTestType.LargeChunkRead:
			case HttpRequestTestType.LargeGZipRead:
			case HttpRequestTestType.GZipWithLength:
			case HttpRequestTestType.ResponseStreamCheckLength2:
			case HttpRequestTestType.ResponseStreamCheckLength:
			case HttpRequestTestType.GetNoLength:
				ctx.Assert (request.Method, Is.EqualTo ("GET"), "method");
				break;

			case HttpRequestTestType.ServerAbortsPost:
				ctx.Assert (request.Method, Is.EqualTo ("POST"), "method");
				break;

			case HttpRequestTestType.RedirectNoLength:
			case HttpRequestTestType.PutChunked:
			case HttpRequestTestType.PutChunkDontCloseRequest:
			case HttpRequestTestType.ServerAbortsRedirect:
				break;

			case HttpRequestTestType.EntityTooBig:
				await EntityTooBig ().ConfigureAwait (false);
				return null;

			case HttpRequestTestType.PostChunked:
				return await HandlePostChunked (
					ctx, operation, connection, request, effectiveFlags, cancellationToken).ConfigureAwait (false);

			case HttpRequestTestType.PostContentLength:
				await PostContentLength ().ConfigureAwait (false);
				break;

			case HttpRequestTestType.ClientAbortsPost:
				await ClientAbortsPost ().ConfigureAwait (false);
				return null;

			case HttpRequestTestType.ImplicitHost:
				var hostAndPort = TestRunner.Uri.GetComponents (UriComponents.HostAndPort, UriFormat.Unescaped);
				ctx.Assert (request.Headers["Host"], Is.EqualTo (hostAndPort), "host");
				break;

			case HttpRequestTestType.CustomHost:
				ctx.Assert (request.Headers["Host"], Is.EqualTo ("custom"), "host");
				break;

			case HttpRequestTestType.CustomHostWithPort:
				ctx.Assert (request.Headers["Host"], Is.EqualTo ("custom:8888"), "host");
				break;

			case HttpRequestTestType.CustomHostDefaultPort:
				var defaultPort = TestRunner.Server.UseSSL ? 443 : 80;
				ctx.Assert (request.Headers["Host"], Is.EqualTo ($"custom:{defaultPort}"), "host");
				break;

			default:
				throw ctx.AssertFail (TestRunner.EffectiveType);
			}

			RemoteEndPoint = connection.RemoteEndPoint;

			HttpResponse response;
			HttpRequestContent content;
			ListenerOperation redirect;

			switch (TestRunner.EffectiveType) {
			case HttpRequestTestType.LargeHeader:
				response = new HttpResponse (HttpStatusCode.OK, Content);
				response.AddHeader ("LargeTest", ConnectionHandler.GetLargeText (100));
				return response;

			case HttpRequestTestType.LargeHeader2:
				response = new HttpResponse (HttpStatusCode.OK, Content);
				response.AddHeader ("LargeTest", ConnectionHandler.GetLargeText (100));
				response.WriteAsBlob = true;
				return response;

			case HttpRequestTestType.SendResponseAsBlob:
				return new HttpResponse (HttpStatusCode.OK, Content) {
					WriteAsBlob = true
				};

			case HttpRequestTestType.ReadTimeout:
				content = new HttpRequestContent (TestRunner, currentRequest);
				return new HttpResponse (HttpStatusCode.OK, content);

			case HttpRequestTestType.RedirectNoLength:
				redirect = operation.RegisterRedirect (ctx, Target);
				response = HttpResponse.CreateRedirect (HttpStatusCode.Redirect, redirect);
				response.NoContentLength = true;
				return response;

			case HttpRequestTestType.ServerAbortsRedirect:
				if (isSecondRequest)
					throw ctx.AssertFail ("Should never happen.");
				var cloned = new HttpRequestHandler (this);
				cloned.isSecondRequest = true;
				redirect = operation.RegisterRedirect (ctx, cloned);
				response = HttpResponse.CreateRedirect (HttpStatusCode.Redirect, redirect);
				return response;

			case HttpRequestTestType.ServerAbortsPost:
				return new HttpResponse (HttpStatusCode.BadRequest, Content);

			case HttpRequestTestType.SimpleGZip:
				var gzipContent = new GZipContent (ConnectionHandler.TheQuickBrownFoxBuffer);
				return new HttpResponse (HttpStatusCode.OK, gzipContent);

			case HttpRequestTestType.TestResponseStream:
				response = new HttpResponse (HttpStatusCode.OK, Content);
				response.WriteAsBlob = true;
				return response;

			case HttpRequestTestType.LargeChunkRead:
				response = new HttpResponse (HttpStatusCode.OK, Content);
				response.WriteBodyAsBlob = true;
				return response;

			case HttpRequestTestType.LargeGZipRead:
				gzipContent = new GZipContent ((ChunkedContent)Content);
				response = new HttpResponse (HttpStatusCode.OK, gzipContent);
				return response;

			case HttpRequestTestType.GZipWithLength:
				gzipContent = new GZipContent ((StringContent)Content);
				response = new HttpResponse (HttpStatusCode.OK, gzipContent);
				return response;

			case HttpRequestTestType.ResponseStreamCheckLength2:
			case HttpRequestTestType.ResponseStreamCheckLength:
				response = new HttpResponse (HttpStatusCode.OK, Content);
				return response;

			case HttpRequestTestType.GetNoLength:
				content = new HttpRequestContent (TestRunner, currentRequest);
				return new HttpResponse (HttpStatusCode.OK, content);

			default:
				return HttpResponse.CreateSuccess (ME);
			}

			async Task EntityTooBig ()
			{
				await request.ReadHeaders (ctx, cancellationToken).ConfigureAwait (false);
				await ctx.AssertException<IOException> (() => request.Read (ctx, cancellationToken), "client doesn't send any body");
			}

			async Task ClientAbortsPost ()
			{
				await request.ReadHeaders (ctx, cancellationToken).ConfigureAwait (false);
				await ctx.AssertException<IOException> (() => request.Read (ctx, cancellationToken), "client doesn't send any body");
			}

			async Task PostContentLength ()
			{
				await request.ReadHeaders (ctx, cancellationToken).ConfigureAwait (false);
				ctx.Assert (request.ContentLength, Is.EqualTo (currentRequest.Content.Length), "request.ContentLength");
				readyTcs.TrySetResult (true);
				await request.Read (ctx, cancellationToken);
			}
		}