Esempio n. 1
0
        private IEnumerable <PayloadReaderTestDescriptor> CreateCrossReferenceTestDescriptors(CrossReferenceTestCase testCase, ReaderTestConfiguration testConfiguration)
        {
            ExceptionUtilities.CheckArgumentNotNull(testCase, "testCase");

            var emptyPayload = new OData.Common.PayloadTestDescriptor()
            {
                PayloadEdmModel = new EdmModel().Fixup()
            };

            IEnumerable <OData.Common.PayloadTestDescriptor> operationPayloads = new[] { emptyPayload };

            // One of the operations in the test case may specify a reference link value to use to generate payloads
            string payloadReferenceLink = testCase.ChangeSets.SelectMany(cset => cset.Operations).Select(o => o.PayloadCrossReferenceLink).SingleOrDefault(s => !string.IsNullOrEmpty(s));

            if (payloadReferenceLink != null)
            {
                EdmModel testModel = Test.OData.Utils.Metadata.TestModels.BuildTestModel();
                operationPayloads =
                    GeneratePayloadElementsWithCrossReferenceLinks(payloadReferenceLink, testConfiguration).Select(
                        p => new OData.Common.PayloadTestDescriptor
                {
                    PayloadElement  = p,
                    PayloadEdmModel = testModel,
                });
            }

            var testDescriptors = new List <PayloadReaderTestDescriptor>();

            foreach (var payload in operationPayloads)
            {
                IEnumerable <IMimePart> requestChangesets = testCase.ChangeSets.Select(
                    c => (IMimePart)BatchUtils.GetRequestChangeset(
                        c.Operations.Select(o =>
                {
                    // check whether we need to inject a payload into this operation
                    var operationPayload = string.IsNullOrEmpty(o.PayloadCrossReferenceLink) ? emptyPayload : payload;

                    ODataUri operationUri = new ODataUri(new[] { ODataUriBuilder.Unrecognized(o.Uri.OriginalString) });
                    var requestOperation  = operationPayload.InRequestOperation(HttpVerb.Post, operationUri, this.RequestManager);
                    requestOperation.Headers.Add(HttpHeaders.ContentId, o.ContentId);

                    return((IMimePart)requestOperation);
                }).ToArray(),
                        this.RequestManager));

                var testDescriptor = new PayloadReaderTestDescriptor(this.PayloadReaderSettings)
                {
                    DebugDescription      = testCase.DebugDescription,
                    PayloadElement        = PayloadBuilder.BatchRequestPayload(requestChangesets.ToArray()).AddAnnotation(new BatchBoundaryAnnotation("batch_foo")),
                    ExpectedException     = testCase.ExpectedException,
                    SkipTestConfiguration = (testConfig) => !testConfig.IsRequest,
                };

                testDescriptors.Add(testDescriptor);
            }

            return(testDescriptors);
        }
Esempio n. 2
0
            /// <summary>
            /// Converts an ODataBatchOperation to the corresponding IMimePart.
            /// </summary>
            /// <param name="operation">The operation to convert.</param>
            /// <returns>The converted operation.</returns>
            private IMimePart ConvertBatchOperation(ODataBatchOperation operation)
            {
                ODataPayloadElement payloadElement = operation.Payload == null ? null : this.Visit(operation.Payload);

                if (this.response)
                {
                    ODataBatchResponseOperation responseOperation = (ODataBatchResponseOperation)operation;
                    HttpResponseData            httpResponse      = new HttpResponseData
                    {
                        StatusCode = (HttpStatusCode)responseOperation.StatusCode,
                    };

                    if (responseOperation.Headers != null)
                    {
                        foreach (var kvp in responseOperation.Headers)
                        {
                            httpResponse.Headers.Add(kvp.Key, kvp.Value);
                        }
                    }
                    ;
                    return(new ODataResponse(httpResponse)
                    {
                        RootElement = payloadElement
                    });
                }

                ODataBatchRequestOperation requestOperation = (ODataBatchRequestOperation)operation;

                // NOTE: this is abusing the ODataUriBuilder but is sufficient for our purposes
                // We use Unrecognized because the request URI may be relative (in the case of a reference)
                ODataUri     requestUri = new ODataUri(ODataUriBuilder.Unrecognized(requestOperation.Url.OriginalString));
                ODataRequest request    = this.requestManager.BuildRequest(
                    requestUri,
                    (HttpVerb)Enum.Parse(typeof(HttpVerb), requestOperation.HttpMethod, /*ignoreCase*/ true),
                    requestOperation.Headers);

                if (payloadElement != null)
                {
                    string contentType;
                    if (!request.Headers.TryGetValue(Microsoft.OData.ODataConstants.ContentTypeHeader, out contentType))
                    {
                        throw new InvalidOperationException("ContentType header not found.");
                    }
                    request.Body = this.requestManager.BuildBody(contentType, requestUri, payloadElement);
                }

                return(request);
            }