Exemplo n.º 1
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);
                }
            }