/// <summary>
        /// Puts payload in a batch request.
        /// </summary>
        /// <typeparam name="T">T must be a PayloadTestDescriptor.</typeparam>
        /// <param name="originalPayload">Payload to put into batch.</param>
        /// <param name="verb">HttpVerb associated with payload.</param>
        /// <param name="random">Use of random makes this method non deterministic.</param>
        /// <param name="requestManager">Used to construct requests.</param>
        /// <param name="operationsBefore">Number of operations/changesets to go before payload.</param>
        /// <param name="operationsAfter">Number of operations/changesets to go after payload.</param>
        /// <param name="version">Maximum version for extra payloads</param>
        /// <returns>The test descriptor for the new BatchRequestPayload</returns>
        public static T InBatchRequest <T>(
            this T originalPayload,
            HttpVerb verb,
            IRandomNumberGenerator random,
            IODataRequestManager requestManager,
            int operationsBefore = 0,
            int operationsAfter  = 0,
            ODataVersion version = ODataVersion.V4
            ) where T : PayloadTestDescriptor
        {
            ExceptionUtilities.CheckArgumentNotNull(originalPayload, "originalPayload");
            ExceptionUtilities.CheckArgumentNotNull(verb, "verb");
            ExceptionUtilities.CheckArgumentNotNull(random, "random");
            ExceptionUtilities.CheckArgumentNotNull(requestManager, "requestManager");
            var payload = (T)originalPayload.Clone();

            var baseUri = new ODataUri(ODataUriBuilder.Root(new Uri("http://www.odata.org/service.svc")));

            IMimePart[] extraOperations = BatchUtils.ExtraRequestChangesets(random, requestManager, (EdmModel)payload.PayloadEdmModel, baseUri, version);  // add extraChangesets methods
            extraOperations.Concat(BatchUtils.ExtraRequestChangesets(random, requestManager, (EdmModel)payload.PayloadEdmModel, baseUri, version));

            //Work out the operations and changesets to go before the payload
            var parts = Enumerable.Range(0, operationsBefore).Select(i => random.ChooseFrom(extraOperations));

            if (verb != HttpVerb.Get)
            {
                extraOperations.ConcatSingle(payload.InRequestOperation(HttpVerb.Get, baseUri, requestManager));
            }
            else
            {
                extraOperations.ConcatSingle(payload.InRequestChangeset(verb, random, requestManager, random.NextFromRange(0, 2), random.NextFromRange(0, 2), version));
            }

            parts.Concat(Enumerable.Range(0, operationsAfter).Select(i => random.ChooseFrom(extraOperations)));

            var batchRequest = new BatchRequestPayload();

            foreach (var part in parts)
            {
                IHttpRequest operation = part as IHttpRequest;
                if (operation != null)
                {
                    batchRequest.Add(operation.AsBatchFragment());
                }
                BatchRequestChangeset changeset = part as BatchRequestChangeset;
                if (changeset != null)
                {
                    batchRequest.Add(changeset);
                }
            }

            //payload.PayloadEdmModel.Fixup();
            payload.PayloadElement = batchRequest;
            return(payload);
        }
Пример #2
0
        /// <summary>
        /// Deserializes the given request's binary payload into a batch payload
        /// </summary>
        /// <param name="request">The request to deserialize</param>
        /// <returns>The deserialized batch request payload</returns>
        public BatchRequestPayload DeserializeBatchRequest(HttpRequestData request)
        {
            var encoding = request.GetEncodingFromHeadersOrDefault();

            MultipartMimeData <MimePartData <byte[]> > split;

            ExceptionUtilities.Assert(TrySplitMimePart(request, encoding, out split), "Failed to split batch response body");

            var batchRequest = new BatchRequestPayload();

            foreach (var subPart in split.ToList())
            {
                MultipartMimeData <MimePartData <byte[]> > splitChangeset;
                if (TrySplitMimePart(subPart, encoding, out splitChangeset))
                {
                    var changeset = new BatchRequestChangeset();
                    changeset.Headers.AddRange(splitChangeset.Headers);

                    foreach (var changesetPart in splitChangeset)
                    {
                        if (changesetPart.Body != null)
                        {
                            if (changesetPart.Body.Length > 0)
                            {
                                changeset.Add(this.BuildRequestFromPart(changesetPart, encoding));
                            }
                        }
                    }

                    batchRequest.Add(changeset);
                }
                else
                {
                    batchRequest.Add(this.BuildRequestFromPart(subPart, encoding));
                }
            }

            return(batchRequest);
        }
        /// <summary>
        /// Generates a dummy request based on the supplied response and model
        /// </summary>
        /// <param name="batchResponse">Response to build the request for.</param>
        /// <param name="model">Model to get the types from.</param>
        /// <returns></returns>
        public BatchRequestPayload GenerateRequestPayload(BatchResponsePayload batchResponse, EntityModelSchema model)
        {
            ExceptionUtilities.CheckArgumentNotNull(batchResponse, "batchResponse");
            ExceptionUtilities.CheckArgumentNotNull(model, "model");
            var batchRequest = new BatchRequestPayload();
            foreach (var part in batchResponse.Parts)
            {
                var operation = part as MimePartData<HttpResponseData>;
                if (operation != null)
                {
                    batchRequest.Add(this.GenerateRequestOperation(operation.Body as ODataResponse, model).AsBatchFragment());
                }

                BatchResponseChangeset changeset = part as BatchResponseChangeset;
                if (changeset != null)
                {
                    batchRequest.Add(this.GenerateRequestChangeset(changeset, model));
                }
            }

            return batchRequest;
        }
        /// <summary>
        /// Deserializes the given request's binary payload into a batch payload
        /// </summary>
        /// <param name="request">The request to deserialize</param>
        /// <returns>The deserialized batch request payload</returns>
        public BatchRequestPayload DeserializeBatchRequest(HttpRequestData request)
        {
            var encoding = request.GetEncodingFromHeadersOrDefault();

            MultipartMimeData<MimePartData<byte[]>> split;
            ExceptionUtilities.Assert(TrySplitMimePart(request, encoding, out split), "Failed to split batch response body");

            var batchRequest = new BatchRequestPayload();

            foreach (var subPart in split.ToList())
            {
                MultipartMimeData<MimePartData<byte[]>> splitChangeset;
                if (TrySplitMimePart(subPart, encoding, out splitChangeset))
                {
                    var changeset = new BatchRequestChangeset();
                    changeset.Headers.AddRange(splitChangeset.Headers);

                    foreach (var changesetPart in splitChangeset)
                    {
                        if (changesetPart.Body != null)
                        {
                            if (changesetPart.Body.Length > 0)
                            {
                                changeset.Add(this.BuildRequestFromPart(changesetPart, encoding));
                            }
                        }
                    }

                    batchRequest.Add(changeset);
                }
                else
                {
                    batchRequest.Add(this.BuildRequestFromPart(subPart, encoding));    
                }
            }

            return batchRequest;
        }
        /// <summary>
        /// Generates a dummy request based on the supplied response and model
        /// </summary>
        /// <param name="batchResponse">Response to build the request for.</param>
        /// <param name="model">Model to get the types from.</param>
        /// <returns></returns>
        public BatchRequestPayload GenerateRequestPayload(BatchResponsePayload batchResponse, EntityModelSchema model)
        {
            ExceptionUtilities.CheckArgumentNotNull(batchResponse, "batchResponse");
            ExceptionUtilities.CheckArgumentNotNull(model, "model");
            var batchRequest = new BatchRequestPayload();

            foreach (var part in batchResponse.Parts)
            {
                var operation = part as MimePartData <HttpResponseData>;
                if (operation != null)
                {
                    batchRequest.Add(this.GenerateRequestOperation(operation.Body as ODataResponse, model).AsBatchFragment());
                }

                BatchResponseChangeset changeset = part as BatchResponseChangeset;
                if (changeset != null)
                {
                    batchRequest.Add(this.GenerateRequestChangeset(changeset, model));
                }
            }

            return(batchRequest);
        }