Exemplo n.º 1
0
 protected override void ProcessRequestAndTryGetResponse(HttpRequestMessage request, out HttpResponseMessage response, out object state)
 {
     if (!HttpContent.IsNullOrEmpty(request.Content))
     {
         // request.Content.LoadIntoBuffer();
     }
     response = null;
     state    = null;
 }
Exemplo n.º 2
0
 public static T Put <T>(string baseUri, string actionUri, HttpContent content)
 {
     return(GetResource <T>(baseUri, c =>
     {
         if (HttpContent.IsNullOrEmpty(content))
         {
             c.DefaultHeaders.ContentLength = 0;
         }
         return c.Put(actionUri, content);
     }));
 }
Exemplo n.º 3
0
 void DumpContent(HttpContent httpContent)
 {
     if (!HttpContent.IsNullOrEmpty(httpContent))
     {
         string content = null;
         try
         {
             httpContent.LoadIntoBuffer();
             content = httpContent.ReadAsString();
         }
         catch (Exception exception)
         {
             content = "$Exception: " + exception.ToString() + "$";
         }
         if (!string.IsNullOrEmpty(content))
         {
             Console.WriteLine(content);
         }
     }
 }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }