示例#1
0
        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);
        }
示例#2
0
            public InMemoryWebRequest ProcessRequestOverride(InMemoryWebRequest request)
            {
                if (IsTextPayloadType(request.RequestContentType))
                {
                    string payload =
                        ReplaceUriOccurences(
                            this.playbackServiceBaseUri,
                            this.underlyingServiceBaseUri,
                            new StreamReader(request.GetRequestStream()).ReadToEnd());
                    // Remove all Content-Length headers (if it's a batch since we just changed the length of the requests by replacing strings)
                    StringBuilder sb     = new StringBuilder();
                    TextReader    reader = new StringReader(payload);
                    string        line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        if (!line.StartsWith("Content-Length"))
                        {
                            sb.AppendLine(line);
                        }
                    }
                    request.SetRequestStreamAsText(sb.ToString());
                }

                // Copy the request to the server request
                request.WriteRequest(this.underlyingService);
                // Send the request
                try
                {
                    this.underlyingService.SendRequest();

                    // Copy the response to our in-memory representation
                    var response = InMemoryWebRequest.FromResponse(this.underlyingService);
                    if (IsTextPayloadType(response.ResponseContentType))
                    {
                        response.SetResponseStreamAsText(
                            ReplaceUriOccurences(
                                this.underlyingServiceBaseUri,
                                this.playbackServiceBaseUri,
                                response.GetResponseStreamAsText()));
                    }
                    var headersToReplace = new string[] { "Location", "OData-EntityId" };
                    foreach (var headerName in headersToReplace)
                    {
                        string value;
                        if (response.ResponseHeaders.TryGetValue(headerName, out value))
                        {
                            response.ResponseHeaders[headerName] =
                                ReplaceUriOccurences(
                                    this.underlyingServiceBaseUri,
                                    this.playbackServiceBaseUri,
                                    value);
                        }
                    }
                    return(response);
                }
                catch (Exception exception)
                {
                    // Translate everything into a 500, it's easier and we don't need correct error reporting on the client anyway (for versioning tests)
                    var response = new InMemoryWebRequest();
                    response.SetResponseStatusCode(500);
                    response.ResponseHeaders["Content-Type"] = UnitTestsUtil.MimeTextPlain;
                    response.SetResponseStreamAsText(exception.ToString());
                    return(response);
                }
            }
示例#3
0
            public InMemoryWebRequest ProcessRequestOverride(InMemoryWebRequest request)
            {
                if (IsTextPayloadType(request.RequestContentType))
                {
                    string payload = 
                        ReplaceUriOccurences(
                            this.playbackServiceBaseUri,
                            this.underlyingServiceBaseUri,
                            new StreamReader(request.GetRequestStream()).ReadToEnd());
                    // Remove all Content-Length headers (if it's a batch since we just changed the length of the requests by replacing strings)
                    StringBuilder sb = new StringBuilder();
                    TextReader reader = new StringReader(payload);
                    string line;
                    while((line = reader.ReadLine()) != null)
                    {
                        if (!line.StartsWith("Content-Length")) sb.AppendLine(line);
                    }
                    request.SetRequestStreamAsText(sb.ToString());
                }

                // Copy the request to the server request
                request.WriteRequest(this.underlyingService);
                // Send the request
                try
                {
                    this.underlyingService.SendRequest();

                    // Copy the response to our in-memory representation
                    var response = InMemoryWebRequest.FromResponse(this.underlyingService);
                    if (IsTextPayloadType(response.ResponseContentType))
                    {
                        response.SetResponseStreamAsText(
                            ReplaceUriOccurences(
                                this.underlyingServiceBaseUri,
                                this.playbackServiceBaseUri,
                                response.GetResponseStreamAsText()));
                    }
                    var headersToReplace = new string[] { "Location", "OData-EntityId" };
                    foreach (var headerName in headersToReplace)
                    {
                        string value;
                        if (response.ResponseHeaders.TryGetValue(headerName, out value))
                        {
                            response.ResponseHeaders[headerName] = 
                                ReplaceUriOccurences(
                                    this.underlyingServiceBaseUri, 
                                    this.playbackServiceBaseUri, 
                                    value);
                        }
                    }
                    return response;
                }
                catch (Exception exception)
                {
                    // Translate everything into a 500, it's easier and we don't need correct error reporting on the client anyway (for versioning tests)
                    var response = new InMemoryWebRequest();
                    response.SetResponseStatusCode(500);
                    response.ResponseHeaders["Content-Type"] = UnitTestsUtil.MimeTextPlain;
                    response.SetResponseStreamAsText(exception.ToString());
                    return response;
                }
            }
示例#4
0
        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;
        }