Esempio n. 1
0
        private static void ExecuteUpdate(DSPResource resource, int id, TestWebRequest baseRequest, string httpMethod, string preferHeader, bool useBatch, DSPResourceSerializerFormat payloadFormat)
        {
            string payload = DSPResourceSerializer.WriteEntity(resource, payloadFormat);

            bool isPost = httpMethod == "POST";
            bool expectedReturnContent = preferHeader == "return=representation" || isPost && preferHeader == null;

            string uriSuffix = isPost ? String.Empty : String.Format("({0})", id);

            TestWebRequest request = useBatch ? new InMemoryWebRequest() : baseRequest;

            request.RequestUriString         = String.Format("/{0}s{1}", resource.ResourceType.Name, uriSuffix);
            request.HttpMethod               = httpMethod;
            request.RequestVersion           = "4.0;";
            request.RequestMaxVersion        = "4.0;";
            request.RequestHeaders["Prefer"] = preferHeader;
            request.Accept             = payloadFormat == DSPResourceSerializerFormat.Atom ? "application/atom+xml,application/xml" : UnitTestsUtil.JsonLightMimeType;
            request.RequestContentType = "application/atom+xml";
            request.SetRequestStreamAsText(payload);

            if (useBatch)
            {
                var batchRequest = new BatchWebRequest();
                var changeset    = new BatchWebRequest.Changeset();
                changeset.Parts.Add((InMemoryWebRequest)request);
                batchRequest.Changesets.Add(changeset);
                batchRequest.SendRequest(baseRequest);

                Assert.IsTrue(UnitTestsUtil.IsSuccessStatusCode(baseRequest.ResponseStatusCode), "Unexpected error occurred on batch.");
            }
            else
            {
                request.SendRequest();
            }

            Assert.IsTrue(UnitTestsUtil.IsSuccessStatusCode(request.ResponseStatusCode), "Unexpected error occurred when sending the request.");
            if (expectedReturnContent)
            {
                // If the request is expected to return content, verify there were no instream errors
                Exception e            = request.ParseResponseInStreamError();
                string    errorMessage = e != null ? e.Message : string.Empty;
                Assert.IsNull(e, "Expected no exception, but got the following error", errorMessage);
            }
        }