/// <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); } }
/// <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); } }
private MemoryStream CreateBatchContent(bool isResponse, string boundary) { MemoryStream payload = new MemoryStream(); foreach (var r in this.parts) { StringBuilder header = new StringBuilder(); header.AppendLine("--" + boundary); header.AppendLine("Content-Type: application/http"); header.AppendLine("Content-Transfer-Encoding: binary"); header.AppendLine(); InMemoryWebRequest.WriteStringToStream(payload, header.ToString()); if (isResponse) { r.WriteResponse(payload); } else { r.WriteRequest(payload, null); } } int contentId = 0; foreach (var c in this.changesets) { string changesetBoundary = "changeset_" + Guid.NewGuid().ToString(); StringBuilder header = new StringBuilder(); header.AppendLine("--" + boundary); header.AppendLine("Content-Type: multipart/mixed; boundary=" + changesetBoundary); MemoryStream changesetPayload = new MemoryStream(); foreach (var r in c.Parts) { StringBuilder cheader = new StringBuilder(); cheader.AppendLine("--" + changesetBoundary); cheader.AppendLine("Content-Type: application/http"); cheader.AppendLine("Content-Transfer-Encoding: binary"); string contentIdStr; r.ResponseHeaders.TryGetValue("Content-ID", out contentIdStr); cheader.AppendLine("Content-Id: " + contentIdStr ?? (++contentId).ToString()); cheader.AppendLine(); InMemoryWebRequest.WriteStringToStream(changesetPayload, cheader.ToString()); if (isResponse) { r.WriteResponse(changesetPayload); } else { r.WriteRequest(changesetPayload, null); } } InMemoryWebRequest.WriteStringToStream(changesetPayload, Environment.NewLine + "--" + changesetBoundary + "--" + Environment.NewLine); changesetPayload.Position = 0; header.AppendLine("Content-Length: " + changesetPayload.Length.ToString()); header.AppendLine(); InMemoryWebRequest.WriteStringToStream(payload, header.ToString()); TestUtil.CopyStream(changesetPayload, payload); } InMemoryWebRequest.WriteStringToStream(payload, Environment.NewLine + "--" + boundary + "--" + Environment.NewLine); payload.Position = 0; return(payload); }