Beispiel #1
0
        public async Task <Response> PostString(
            TestContext ctx, HttpContent returnContent, CancellationToken cancellationToken)
        {
            var message = Handler.CreateRequestMessage();

            message.Method     = HttpMethod.Post;
            message.RequestUri = RequestUri;
            message.Content    = Handler.CreateStringContent(Content.AsString());

            if (contentLength != null)
            {
                message.Content.ContentLength = contentLength;
            }
            if (contentType != null)
            {
                message.Content.ContentType = contentType;
            }

            var response = await Client.SendAsync(
                message, HttpCompletionOption.ResponseContentRead, cancellationToken);

            ctx.Assert(response, Is.Not.Null, "response");

            ctx.LogDebug(3, "GOT RESPONSE: {0}", response.StatusCode);

            if (!response.IsSuccessStatusCode)
            {
                return(new SimpleResponse(this, response.StatusCode, null));
            }

            string body = null;

            if (response.Content != null)
            {
                body = await response.Content.ReadAsStringAsync();

                ctx.LogDebug(5, "GOT BODY: {0}", Format(body));
            }

            if (returnContent != null)
            {
                ctx.Assert(body, Is.Not.Null, "returned body");

                body = body.TrimEnd();
                ctx.Assert(body, Is.EqualTo(returnContent.AsString()), "returned body");
            }
            else
            {
                ctx.Assert(body, Is.Empty, "returned body");
            }

            return(new SimpleResponse(this, response.StatusCode, returnContent));
        }