private bool ParseBatchPart(bool isResponse, TextReader reader, InMemoryWebRequest part, string boundary, string endboundary) { bool result = false; string line; if (isResponse) { part.ParseResponseStatus(reader); } else { part.ParseRequestVerb(reader); } if (isResponse) { part.ResponseHeaders.Clear(); InMemoryWebRequest.ParseHeaders(reader, part.ResponseHeaders); } else { InMemoryWebRequest.ParseHeaders(reader, part.RequestHeaders); InMemoryWebRequest.ApplyHeadersToProperties(part); } StringBuilder sb = new StringBuilder(); string lastLine = null; while ((line = reader.ReadLine()) != null) { if (line == boundary) { break; } if (line == endboundary) { result = true; break; } if (lastLine != null) { sb.AppendLine(lastLine); } lastLine = line; } // The last line must not end with a newline - the batch adds it there, but it's not actually part of the content sb.Append(lastLine); if (isResponse) { part.SetResponseStream(new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))); } else { part.SetRequestStreamAsText(sb.ToString()); } return(result); }
/// <summary>Writes the batch as a response into an in-memory request (sets up properties and such).</summary> /// <param name="request">The request to apply the batch as a response to.</param> public void WriteResponse(InMemoryWebRequest request) { request.SetResponseStatusCode(200); string boundary = "boundary_" + Guid.NewGuid().ToString(); request.ResponseHeaders["Content-Type"] = String.Format("{0}; boundary={1}", UnitTestsUtil.MimeMultipartMixed, boundary); request.SetResponseStream(this.CreateBatchContent(true, boundary)); }
/// <summary>Creates the in-memory response from another TestWebRequest, copying its reponse.</summary> /// <param name="sourceResponse">The request to read the response from.</param> /// <returns>The newly create request object with the response proeprties filled with the data from the request response.</returns> public static InMemoryWebRequest FromResponse(TestWebRequest sourceResponse) { InMemoryWebRequest response = new InMemoryWebRequest(); response.SetResponseStatusCode(sourceResponse.ResponseStatusCode); foreach (var header in GetAllResponseHeaders(sourceResponse)) { response.ResponseHeaders[header.Key] = header.Value; } using (Stream responseStream = sourceResponse.GetResponseStream()) { response.SetResponseStream(responseStream); } return response; }
/// <summary>Creates the in-memory response from another TestWebRequest, copying its reponse.</summary> /// <param name="sourceResponse">The request to read the response from.</param> /// <returns>The newly create request object with the response proeprties filled with the data from the request response.</returns> public static InMemoryWebRequest FromResponse(TestWebRequest sourceResponse) { InMemoryWebRequest response = new InMemoryWebRequest(); response.SetResponseStatusCode(sourceResponse.ResponseStatusCode); foreach (var header in GetAllResponseHeaders(sourceResponse)) { response.ResponseHeaders[header.Key] = header.Value; } using (Stream responseStream = sourceResponse.GetResponseStream()) { response.SetResponseStream(responseStream); } return(response); }
private bool ParseBatchPart(bool isResponse, TextReader reader, InMemoryWebRequest part, string boundary, string endboundary) { bool result = false; string line; if (isResponse) { part.ParseResponseStatus(reader); } else { part.ParseRequestVerb(reader); } if (isResponse) { part.ResponseHeaders.Clear(); InMemoryWebRequest.ParseHeaders(reader, part.ResponseHeaders); } else { InMemoryWebRequest.ParseHeaders(reader, part.RequestHeaders); InMemoryWebRequest.ApplyHeadersToProperties(part); } StringBuilder sb = new StringBuilder(); string lastLine = null; while ((line = reader.ReadLine()) != null) { if (line == boundary) { break; } if (line == endboundary) { result = true; break; } if (lastLine != null) { sb.AppendLine(lastLine); } lastLine = line; } // The last line must not end with a newline - the batch adds it there, but it's not actually part of the content sb.Append(lastLine); if (isResponse) { part.SetResponseStream(new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()))); } else { part.SetRequestStreamAsText(sb.ToString()); } return result; }