public override bool CheckResponse(TestContext ctx, Response response)
        {
            if (Target != null)
            {
                return(Target.CheckResponse(ctx, response));
            }

            switch (TestRunner.EffectiveType)
            {
            case HttpInstrumentationTestType.AbortResponse:
                return(ctx.Expect(response.Status, Is.EqualTo(HttpStatusCode.OK), "response.StatusCode"));

            case HttpInstrumentationTestType.ReuseAfterPartialRead:
                if (!ctx.Expect(response.Content, Is.Not.Null, "response.Content != null"))
                {
                    return(false);
                }

                return(ctx.Expect(response.Content.Length, Is.GreaterThan(0), "response.Content.Length"));
            }

            if (!ctx.Expect(response.Content, Is.Not.Null, "response.Content != null"))
            {
                return(false);
            }

            return(HttpContent.Compare(ctx, response.Content, ExpectedContent, false, "response.Content"));
        }
示例#2
0
        public override bool CheckResponse(TestContext ctx, Response response)
        {
            if (Content == null)
            {
                return(ctx.Expect(response.Content, Is.Null, "response.Content"));
            }

            if (!ctx.Expect(response.Content, Is.Not.Null, "response.Content"))
            {
                return(false);
            }
            if (response.Content.HasLength && Content.HasLength)
            {
                if (!ctx.Expect(response.Content.Length, Is.EqualTo(Content.Length), "response.Content.Length"))
                {
                    return(false);
                }
                if (!HttpContent.Compare(ctx, response.Content, Content, true, "response.Content"))
                {
                    return(false);
                }
                if (false && !ctx.Expect(response.Content.AsString(), Is.EqualTo(Content.AsString()), "response.Content.AsString ()"))
                {
                    return(false);
                }
            }
            return(true);
        }
示例#3
0
        HttpResponse HandleSendAsync(TestContext ctx, HttpConnection connection, HttpRequest request, RequestFlags effectiveFlags)
        {
            var body = request.ReadBody();

            int?contentLength = null;

            if ((effectiveFlags & RequestFlags.NoContentLength) == 0)
            {
                string contentLengthValue;
                var    hasContentLength = request.Headers.TryGetValue("Content-Length", out contentLengthValue);
                ctx.Assert(hasContentLength, "Missing Content-Length");
                contentLength = int.Parse(contentLengthValue);
            }

            Debug(ctx, 5, "BODY", body);
            if ((effectiveFlags & RequestFlags.NoBody) != 0)
            {
                ctx.Assert(body, Is.Not.Null, "body");
                if (contentLength != null)
                {
                    ctx.Assert(contentLength.Value, Is.EqualTo(0), "Zero Content-Length");
                }
                return(HttpResponse.CreateSuccess());
            }

            HttpContent.Compare(ctx, body, Content, false);
            return(new HttpResponse(HttpStatusCode.OK, ReturnContent));
        }
示例#4
0
        public override bool CheckResponse(TestContext ctx, Response response)
        {
            HttpContent expectedContent;

            switch (TestRunner.EffectiveType)
            {
            case HttpClientTestType.SimpleGZip:
            case HttpClientTestType.ParallelGZip:
            case HttpClientTestType.ParallelGZipNoClose:
            case HttpClientTestType.SequentialRequests:
            case HttpClientTestType.SequentialChunked:
            case HttpClientTestType.SequentialGZip:
            case HttpClientTestType.ReuseHandler:
            case HttpClientTestType.ReuseHandlerNoClose:
            case HttpClientTestType.ReuseHandlerChunked:
            case HttpClientTestType.ReuseHandlerGZip:
                expectedContent = HttpContent.TheQuickBrownFox;
                break;

            case HttpClientTestType.CancelPost:
            case HttpClientTestType.CancelPostWhileWriting:
                return(ctx.Expect(response.Content, Is.Null));

            default:
                expectedContent = new StringContent(ME);
                break;
            }

            if (!ctx.Expect(response.Content, Is.Not.Null, "response.Content != null"))
            {
                return(false);
            }

            return(HttpContent.Compare(ctx, response.Content, expectedContent, false, "response.Content"));
        }
示例#5
0
        HttpResponse HandlePutDataAsync(TestContext ctx, HttpRequest request, RequestFlags effectiveFlags)
        {
            ctx.Assert(request.Method, Is.EqualTo("PUT"), "method");

            Debug(ctx, 5, "BODY", request.Body);
            if ((effectiveFlags & RequestFlags.NoBody) != 0)
            {
                ctx.Assert(request.Body, Is.Not.Null, "body");
                return(HttpResponse.CreateSuccess());
            }

            HttpContent.Compare(ctx, request.Body, Content, false);
            return(new HttpResponse(HttpStatusCode.OK, ReturnContent));
        }
示例#6
0
		public override bool CheckResponse (TestContext ctx, Response response)
		{
			if (Target != null)
				return Target.CheckResponse (ctx, response);

			switch (TestRunner.EffectiveType) {
			case HttpRequestTestType.ReadTimeout:
				return ctx.Expect (response.Status, Is.EqualTo (HttpStatusCode.OK), "response.StatusCode");
			}

			if (!ctx.Expect (response.Content, Is.Not.Null, "response.Content != null"))
				return false;

			return HttpContent.Compare (ctx, response.Content, ExpectedContent, false, "response.Content");
		}
示例#7
0
        HttpResponse HandlePostString(TestContext ctx, HttpConnection connection, HttpRequest request, RequestFlags effectiveFlags)
        {
            ctx.Assert(request.Method, Is.EqualTo("POST"), "method");

            var body = request.ReadBody();

            Debug(ctx, 5, "BODY", body);
            if ((effectiveFlags & RequestFlags.NoBody) != 0)
            {
                ctx.Assert(body, Is.Not.Null, "body");
                return(HttpResponse.CreateSuccess());
            }

            HttpContent.Compare(ctx, body, Content, false);
            return(new HttpResponse(HttpStatusCode.OK, ReturnContent));
        }
示例#8
0
        HttpResponse HandleSendAsync(TestContext ctx, HttpRequest request, RequestFlags effectiveFlags)
        {
            if ((effectiveFlags & RequestFlags.NoContentLength) == 0)
            {
                ctx.Assert(request.ContentLength, Is.Not.Null, "Missing Content-Length");
            }

            Debug(ctx, 5, "BODY", request.Body);
            if ((effectiveFlags & RequestFlags.NoBody) != 0)
            {
                ctx.Assert(request.Body, Is.Not.Null, "body");
                if (request.ContentLength != null)
                {
                    ctx.Assert(request.ContentLength.Value, Is.EqualTo(0), "Zero Content-Length");
                }
                return(HttpResponse.CreateSuccess());
            }

            HttpContent.Compare(ctx, request.Body, Content, false);
            return(new HttpResponse(HttpStatusCode.OK, ReturnContent));
        }
示例#9
0
        internal protected override async Task <HttpResponse> HandleRequest(
            TestContext ctx, HttpOperation operation, HttpConnection connection, HttpRequest request,
            RequestFlags effectiveFlags, CancellationToken cancellationToken)
        {
            await CompletedTask.ConfigureAwait(false);

            Debug(ctx, 2, "HANDLE POST", request.Path, request.Method, effectiveFlags);

            if (request.Headers.ContainsKey("X-Mono-Redirected"))
            {
                effectiveFlags |= RequestFlags.Redirected;
            }

            if ((effectiveFlags & RequestFlags.RedirectedAsGet) != 0)
            {
                if (!ctx.Expect(request.Method, Is.EqualTo("GET"), "method"))
                {
                    return(null);
                }
            }
            else if (Method != null)
            {
                if (!ctx.Expect(request.Method, Is.EqualTo(Method), "method"))
                {
                    return(null);
                }
            }
            else
            {
                if (!ctx.Expect(request.Method, Is.EqualTo("POST").Or.EqualTo("PUT"), "method"))
                {
                    return(null);
                }
            }

            if (customHandler != null)
            {
                var customResponse = customHandler(request);
                if (customResponse != null)
                {
                    return(customResponse);
                }
            }

            if ((effectiveFlags & RequestFlags.RedirectedAsGet) != 0)
            {
                ctx.Expect(request.ContentLength, Is.Null, "Content-Length header not allowed");
                ctx.Expect(request.TransferEncoding, Is.Null, "Transfer-Encoding header not allowed");
                return(HttpResponse.CreateSuccess());
            }

            if ((effectiveFlags & RequestFlags.NoContentLength) != 0)
            {
                ctx.Expect(request.ContentLength, Is.Null, "Content-Length header not allowed");
                ctx.Expect(request.TransferEncoding, Is.Null, "Transfer-Encoding header not allowed");
            }

            Debug(ctx, 2, "HANDLE POST #1", request.ContentLength, request.TransferEncoding);

            switch (Mode)
            {
            case TransferMode.Default:
                if (Content != null)
                {
                    ctx.Expect(request.ContentLength, Is.Not.Null, "Missing Content-Length");
                    break;
                }
                else
                {
                    ctx.Expect(request.ContentLength, Is.Null, "Content-Length header not allowed");
                    return(HttpResponse.CreateSuccess());
                }

            case TransferMode.ContentLength:
                ctx.Expect(request.ContentLength, Is.Not.Null, "Missing Content-Length");
                ctx.Expect(request.TransferEncoding, Is.Null, "Transfer-Encoding header not allowed");
                break;

            case TransferMode.Chunked:
                if ((effectiveFlags & RequestFlags.Redirected) != 0)
                {
                    goto case TransferMode.ContentLength;
                }

                ctx.Expect(request.ContentLength, Is.Null, "Content-Length header not allowed");
                var ok = ctx.Expect(request.TransferEncoding, Is.Not.Null, "Missing Transfer-Encoding header");
                if (!ok)
                {
                    break;
                }

                ok &= ctx.Expect(request.TransferEncoding.ToLowerInvariant(), Is.EqualTo("chunked"), "Invalid Transfer-Encoding");
                break;

            default:
                ctx.Expect(false, "Unknown TransferMode: '{0}'", Mode);
                return(null);
            }

            Debug(ctx, 5, "BODY", request.Body);
            if ((effectiveFlags & RequestFlags.NoBody) != 0)
            {
                ctx.Expect(HttpContent.IsNullOrEmpty(request.Body), "Must not send a body with this request.");
                return(null);
            }

            if (Content != null)
            {
                HttpContent.Compare(ctx, request.Body, Content, true);
            }
            else
            {
                ctx.Expect(HttpContent.IsNullOrEmpty(request.Body), "null or empty content");
            }

            if (ReturnContent != null)
            {
                return(new HttpResponse(HttpStatusCode.OK, ReturnContent));
            }

            return(null);
        }
示例#10
0
        protected internal override HttpResponse HandleRequest(TestContext ctx, HttpConnection connection, HttpRequest request, RequestFlags effectiveFlags)
        {
            Debug(ctx, 2, "HANDLE POST", request.Path, request.Method, effectiveFlags);

            if (request.Headers.ContainsKey("X-Mono-Redirected"))
            {
                effectiveFlags |= RequestFlags.Redirected;
            }

            if ((effectiveFlags & RequestFlags.RedirectedAsGet) != 0)
            {
                if (!ctx.Expect(request.Method, Is.EqualTo("GET"), "method"))
                {
                    return(null);
                }
            }
            else if (Method != null)
            {
                if (!ctx.Expect(request.Method, Is.EqualTo(Method), "method"))
                {
                    return(null);
                }
            }
            else
            {
                if (!ctx.Expect(request.Method, Is.EqualTo("POST").Or.EqualTo("PUT"), "method"))
                {
                    return(null);
                }
            }

            if (customHandler != null)
            {
                var customResponse = customHandler(request);
                if (customResponse != null)
                {
                    return(customResponse);
                }
            }

            var hasContentLength    = request.Headers.ContainsKey("Content-Length");
            var hasTransferEncoding = request.Headers.ContainsKey("Transfer-Encoding");

            if ((effectiveFlags & RequestFlags.RedirectedAsGet) != 0)
            {
                ctx.Expect(hasContentLength, Is.False, "Content-Length header not allowed");
                ctx.Expect(hasTransferEncoding, Is.False, "Transfer-Encoding header not allowed");
                return(HttpResponse.CreateSuccess());
            }

            if ((effectiveFlags & RequestFlags.NoContentLength) != 0)
            {
                ctx.Expect(hasContentLength, Is.False, "Content-Length header not allowed");
                ctx.Expect(hasTransferEncoding, Is.False, "Transfer-Encoding header not allowed");
            }

            Debug(ctx, 2, "HANDLE POST #1", hasContentLength, hasTransferEncoding);

            var content = request.ReadBody();

            switch (Mode)
            {
            case TransferMode.Default:
                if (Content != null)
                {
                    ctx.Expect(hasContentLength, Is.True, "Missing Content-Length");
                    break;
                }
                else
                {
                    ctx.Expect(hasContentLength, Is.False, "Content-Length header not allowed");
                    return(HttpResponse.CreateSuccess());
                }

            case TransferMode.ContentLength:
                ctx.Expect(hasContentLength, Is.True, "Missing Content-Length");
                ctx.Expect(hasTransferEncoding, Is.False, "Transfer-Encoding header not allowed");
                break;

            case TransferMode.Chunked:
                if ((effectiveFlags & RequestFlags.Redirected) != 0)
                {
                    goto case TransferMode.ContentLength;
                }

                ctx.Expect(hasContentLength, Is.False, "Content-Length header not allowed");
                var ok = ctx.Expect(hasTransferEncoding, Is.True, "Missing Transfer-Encoding header");
                if (!ok)
                {
                    break;
                }

                var transferEncoding = request.Headers ["Transfer-Encoding"];
                ok &= ctx.Expect(transferEncoding.ToLowerInvariant(), Is.EqualTo("chunked"), "Invalid Transfer-Encoding");
                break;

            default:
                ctx.Expect(false, "Unknown TransferMode: '{0}'", Mode);
                return(null);
            }

            Debug(ctx, 5, "BODY", content);
            if ((effectiveFlags & RequestFlags.NoBody) != 0)
            {
                ctx.Expect(HttpContent.IsNullOrEmpty(content), "Must not send a body with this request.");
                return(null);
            }

            if (Content != null)
            {
                HttpContent.Compare(ctx, content, Content, true);
            }
            else
            {
                ctx.Expect(HttpContent.IsNullOrEmpty(content), "null or empty content");
            }

            return(null);
        }