public void ResponseMessageShouldBeCaseInsensitive()
 {
     var dictionary = new Dictionary<string, string> { { "content-type", "some value" } };
     var message = new HttpWebResponseMessage(dictionary, 201, () => { throw new Exception(); });
     message.GetHeader("Content-Type").Should().Be("some value");
     message.GetHeader("CONTENT-TYPE").Should().Be("some value");
 }
 private void MaterializeTest(HttpStatusCode statusCode, ODataPayloadKind payloadKind)
 {
     var uri = new Uri("http://any");
     var context = new DataServiceContext();
     var requestInfo = new RequestInfo(context);
     var responseInfo = new ResponseInfo(requestInfo, MergeOption.OverwriteChanges);
     var queryComponents = new QueryComponents(uri, new Version(4, 0), typeof(Product), null, null);
     var responseMessage = new HttpWebResponseMessage(
         new HeaderCollection(),
         (int)statusCode,
         () => new MemoryStream());
     var materialize = DataServiceRequest.Materialize(
         responseInfo,
         queryComponents,
         null,
         "application/json",
         responseMessage,
         payloadKind);
     Assert.IsNull(materialize.Context);
     Assert.IsNull(materialize.Current);
     var enumerable = materialize.Cast<object>();
     Assert.AreEqual(0, enumerable.Count());
 }
Esempio n. 3
0
        private void HandleOperationResponseData(IODataResponseMessage responseMsg, Stream responseStream)
        {
            Debug.Assert(this.entryIndex >= 0 && this.entryIndex < this.ChangedEntries.Count(), string.Format(System.Globalization.CultureInfo.InvariantCulture, "this.entryIndex = '{0}', this.ChangedEntries.Count() = '{1}'", this.entryIndex, this.ChangedEntries.Count()));

            // Parse the response
            Descriptor current = this.ChangedEntries[this.entryIndex];
            MaterializerEntry entry = default(MaterializerEntry);
            Version responseVersion;
            Exception exception = BaseSaveResult.HandleResponse(this.RequestInfo, (HttpStatusCode)responseMsg.StatusCode, responseMsg.GetHeader(XmlConstants.HttpODataVersion), () => { return responseStream; }, false/*throwOnFailure*/, out responseVersion);

            var headers = new HeaderCollection(responseMsg);
            if (responseStream != null && current.DescriptorKind == DescriptorKind.Entity && exception == null)
            {
                // Only process the response if the current resource is an entity and it's an insert or update scenario
                EntityDescriptor entityDescriptor = (EntityDescriptor)current;

                // We were ignoring the payload for non-insert and non-update scenarios. We need to keep doing that.
                if (entityDescriptor.State == EntityStates.Added || entityDescriptor.StreamState == EntityStates.Added ||
                    entityDescriptor.State == EntityStates.Modified || entityDescriptor.StreamState == EntityStates.Modified)
                {
                    try
                    {
                        ResponseInfo responseInfo = this.CreateResponseInfo(entityDescriptor);
                        var responseMessageWrapper = new HttpWebResponseMessage(
                            headers,
                            responseMsg.StatusCode,
                            () => responseStream);

                        entry = ODataReaderEntityMaterializer.ParseSingleEntityPayload(responseMessageWrapper, responseInfo, entityDescriptor.Entity.GetType());
                        entityDescriptor.TransientEntityDescriptor = entry.EntityDescriptor;
                    }
                    catch (Exception ex)
                    {
                        exception = ex;

                        if (!CommonUtil.IsCatchableExceptionType(ex))
                        {
                            throw;
                        }
                    }
                }
            }

            this.cachedResponses.Add(new CachedResponse(
                current,
                headers,
                (HttpStatusCode)responseMsg.StatusCode,
                responseVersion,
                entry,
                exception));

            if (exception != null)
            {
                current.SaveError = exception;

                // DEVNOTE(pqian):
                // There are two possible scenario here:
                // 1. We are in the sync code path, and there's an in stream error on the server side, or there are bad xml thrown
                // 2. We are in the async code path, there's a error thrown on the server side (any error)
                // Ideally, we need to check whether we want to continue to the next changeset. (Call this.CheckContinueOnError)
                // However, in V1/V2, we did not do this. Thus we will always continue on error on these scenarios
            }
        }
        private static DataServiceTransportException ConvertToDataServiceWebException(WebException webException)
        {
            HttpWebResponseMessage errorResponseMessage = null;
            if (webException.Response != null)
            {
                var httpResponse = (HttpWebResponse)webException.Response;
                errorResponseMessage = new HttpWebResponseMessage(httpResponse);
            }

            return new DataServiceTransportException(errorResponseMessage, webException);
        }
 public void ResponseMessageShouldSupportSettingHeaders()
 {
     var message = new HttpWebResponseMessage(new Dictionary<string, string>(), 201, () => { throw new Exception(); });
     Action setHeader = () => message.SetHeader("Content-Type", "some value");
     setHeader.ShouldThrow<NotSupportedException>();
 }
Esempio n. 6
0
        public void SendResponse(HttpWebResponseMessage responseMessage)
        {
            Assert.IsNotNull(responseMessage, "sendResponse test hook was called with null response message");
            Dictionary<string, string> headers = WrapHttpHeaders(responseMessage.Response.Headers);
            headers.Add("__HttpStatusCode", responseMessage.Response.StatusCode.ToString());
            responseHeaders.Add(headers);

            if (null != this.CustomSendResponseAction)
            {
                this.CustomSendResponseAction(responseMessage.Response);
            }
        }