public void IntegrationTest_Serialize()
 {
     var httpClient = new HttpClient();
     var httpResponseMessage = httpClient.GetAsync("http://google.com").Result;
     Console.WriteLine(httpResponseMessage.Headers.ToString());
     var defaultHttpResponseMessageSerializer = new MessageContentHttpMessageSerializer();
     var fileStream = new FileStream("msg.bin", FileMode.Create);
     defaultHttpResponseMessageSerializer.SerializeAsync(TaskHelpers.FromResult(httpResponseMessage), fileStream).Wait();
     fileStream.Close();
 }
 public void IntegrationTest_Serialize()
 {
     var requestMessage = new HttpRequestMessage( HttpMethod.Get, "http://some.server/api/foo");
     requestMessage.Headers.Range = new RangeHeaderValue(0, 1) { Unit = "custom" };
     var serializer = new MessageContentHttpMessageSerializer();
     var memoryStream = new MemoryStream();
     serializer.SerializeAsync(requestMessage, memoryStream).Wait();
     memoryStream.Position = 0;
     var request = serializer.DeserializeToRequestAsync(memoryStream).Result;
     Assert.AreEqual(requestMessage.Headers.Range.Unit, request.Headers.Range.Unit);
 }
        public void AddOrUpdate(CacheKey key, HttpResponseMessage response)
        {
            // removing reference to request so that the request can get GCed
            var req = response.RequestMessage;

            response.RequestMessage = null;
            var memoryStream = new MemoryStream();

            Task.Factory.StartNew(() => _messageSerializer.SerializeAsync(TaskHelpers.FromResult(response), memoryStream)).Wait();
            response.RequestMessage = req;
            _responseCache.Set(key.HashBase64, memoryStream.ToArray(), GetExpiry(response));
        }
        public void Response_Deserialize_Serialize()
        {
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CacheCow.Client.Tests.Data.Response.bin");
            var serializer = new MessageContentHttpMessageSerializer();
            var response = serializer.DeserializeToResponseAsync(stream).Result;

            var memoryStream = new MemoryStream();
            serializer.SerializeAsync(TaskHelpers.FromResult(response), memoryStream).Wait();

            memoryStream.Position = 0;
            var response2 = serializer.DeserializeToResponseAsync(memoryStream).Result;
            var result = DeepComparer.Compare(response, response2);
            if(result.Count()>0)
                Assert.Fail(string.Join("\r\n", result));
        }
        public async Task AddOrUpdateAsync(CacheKey key, HttpResponseMessage response)
        {
            // removing reference to request so that the request can get GCed
            var req = response.RequestMessage;

            response.RequestMessage = null;
            var memoryStream = new MemoryStream();
            await _messageSerializer.SerializeAsync(response, memoryStream);

            response.RequestMessage = req;
            var suggestedExpiry = response.GetExpiry() ?? DateTimeOffset.UtcNow.Add(_minExpiry);
            var minExpiry       = DateTimeOffset.UtcNow.Add(_minExpiry);
            var optimalExpiry   = (suggestedExpiry > minExpiry) ? suggestedExpiry : minExpiry;

            _responseCache.Set(key.HashBase64, memoryStream.ToArray(), optimalExpiry);
        }
        public void Request_Deserialize_Serialize()
        {
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CacheCow.Client.Tests.Data.Request.bin");
            var serializer = new MessageContentHttpMessageSerializer();
            var request = serializer.DeserializeToRequestAsync(stream).Result;

            var memoryStream = new MemoryStream();
            serializer.SerializeAsync(request, memoryStream).Wait();

            memoryStream.Position = 0;
            var request2 = serializer.DeserializeToRequestAsync(memoryStream).Result;
            var result = DeepComparer.Compare(request, request2);

            // !! Ignore this until RTM since this is fixed. See http://aspnetwebstack.codeplex.com/workitem/303
            //if (result.Count() > 0)
                //Assert.Fail(string.Join("\r\n", result));
        }
		public void Response_Deserialize_Serialize_File()
		{
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CacheCow.Client.Tests.Data.Response.cs");
			var serializer = new MessageContentHttpMessageSerializer();
			var response = serializer.DeserializeToResponseAsync(stream).Result;

			using(var fileStream = new FileStream("response.tmp", FileMode.Create))
			{
				serializer.SerializeAsync(TaskHelpers.FromResult(response), fileStream).Wait();

				fileStream.Position = 0;
				var response2 = serializer.DeserializeToResponseAsync(fileStream).Result;
				var result = DeepComparer.Compare(response, response2);
				if (result.Count() > 0)
					Assert.Fail(string.Join("\r\n", result));
			}
		}
        public void Request_Deserialize_Serialize_File()
        {
            var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CacheCow.Tests.Data.Request.bin");
            var serializer = new MessageContentHttpMessageSerializer();
            var request = serializer.DeserializeToRequestAsync(stream).Result;

            using(var fileStream = new FileStream("request.tmp", FileMode.Create))
            {
                serializer.SerializeAsync(request, fileStream).Wait();

                fileStream.Position = 0;
                var request2 = serializer.DeserializeToRequestAsync(fileStream).Result;
                var result = DeepComparer.Compare(request, request2);

                if (result.Count() > 0)
                Assert.Fail(string.Join("\r\n", result));
            }
        }
 public void IntegrationTest_Serialize_Deserialize()
 {
     var httpClient = new HttpClient();
     var httpResponseMessage = httpClient.GetAsync("http://google.com").Result;
     var contentLength = httpResponseMessage.Content.Headers.ContentLength; // access to make sure is populated http://aspnetwebstack.codeplex.com/discussions/388196
     var memoryStream = new MemoryStream();
     var defaultHttpResponseMessageSerializer = new MessageContentHttpMessageSerializer();
     defaultHttpResponseMessageSerializer.SerializeAsync(TaskHelpers.FromResult(httpResponseMessage), memoryStream).Wait();
     memoryStream.Position = 0;
     var httpResponseMessage2 = defaultHttpResponseMessageSerializer.DeserializeToResponseAsync(memoryStream).Result;
     Assert.AreEqual(httpResponseMessage.StatusCode, httpResponseMessage2.StatusCode, "StatusCode");
     Assert.AreEqual(httpResponseMessage.ReasonPhrase, httpResponseMessage2.ReasonPhrase, "ReasonPhrase");
     Assert.AreEqual(httpResponseMessage.Version, httpResponseMessage2.Version, "Version");
     Assert.AreEqual(httpResponseMessage.Headers.ToString(), httpResponseMessage2.Headers.ToString(), "Headers.ToString()");
     Assert.AreEqual(httpResponseMessage.Content.ReadAsStringAsync().Result,
         httpResponseMessage2.Content.ReadAsStringAsync().Result, "Content");
     Assert.AreEqual(httpResponseMessage.Content.Headers.ToString(),
         httpResponseMessage2.Content.Headers.ToString(), "Headers.ToString()");
 }