示例#1
0
        /// <summary>Writes the request into a stream (including the verb, headers and everything)</summary>
        /// <param name="stream">The stream to write the request into.</param>
        /// <param name="serviceRoot">The service root URI to target the request at. If null the request uri will be left as is.</param>
        public void WriteRequest(Stream stream, Uri serviceRoot)
        {
            StringBuilder sb         = new StringBuilder();
            string        requestUri = this.RequestUriString;

            if (serviceRoot != null)
            {
                requestUri = this.RequestUriString;
                if (requestUri.StartsWith("/"))
                {
                    requestUri = requestUri.Substring(1);
                }
                requestUri = new Uri(serviceRoot, requestUri).AbsoluteUri;
            }
            sb.AppendLine(this.HttpMethod + " " + requestUri + " HTTP/1.1");
            InMemoryWebRequest.WriteHeadersToStringBuilder(sb, GetAllRequestHeaders(this));
            sb.AppendLine();

            InMemoryWebRequest.WriteStringToStream(stream, sb.ToString());
            if (this.RequestStream != null)
            {
                this.RequestStream.Seek(0, SeekOrigin.Begin);
                TestUtil.CopyStream(this.RequestStream, stream);
            }
        }
示例#2
0
        /// <summary>Writes the response part of the request into a stream (including the status, headers and everything)</summary>
        /// <param name="stream">The stream to write the response into.</param>
        public void WriteResponse(Stream stream)
        {
            System.Net.HttpStatusCode responseStatusCode = (System.Net.HttpStatusCode) this.ResponseStatusCode;

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("HTTP/1.1 " + this.ResponseStatusCode.ToString() + " " + responseStatusCode.ToString());
            InMemoryWebRequest.WriteHeadersToStringBuilder(sb, this.ResponseHeaders);
            sb.AppendLine();

            InMemoryWebRequest.WriteStringToStream(stream, sb.ToString());
            if (this.responseStream != null)
            {
                TestUtil.CopyStream(this.GetResponseStream(), stream);
            }
        }