[System.Net.Http] Return ContentLength of the buffer when content is loaded into...
authorMarek Safar <marek.safar@gmail.com>
Wed, 19 Feb 2014 14:29:02 +0000 (15:29 +0100)
committerMarek Safar <marek.safar@gmail.com>
Wed, 19 Feb 2014 14:30:42 +0000 (15:30 +0100)
mcs/class/System.Net.Http/System.Net.Http.Headers/HttpContentHeaders.cs
mcs/class/System.Net.Http/System.Net.Http/HttpContent.cs
mcs/class/System.Net.Http/Test/System.Net.Http/StreamContentTest.cs

index 57bda06b90c3cf431083d388615753634f1a3866..802d479541d6677ad2a785a341b70e6f36ae1b12 100644 (file)
@@ -73,6 +73,10 @@ namespace System.Net.Http.Headers
                                if (v != null)
                                        return v;
 
+                               v = content.LoadedBufferLength;
+                               if (v != null)
+                                       return v;
+
                                long l;
                                if (content.TryComputeLength (out l))
                                        return l;
index 021f2e9b0d9f8edac15ac185d2ccff6c6091090c..77419d28cd4e904648dda1f9c67f7abce7ceba6c 100644 (file)
@@ -75,6 +75,12 @@ namespace System.Net.Http
                        }
                }
 
+               internal long? LoadedBufferLength {
+                       get {
+                               return buffer == null ? (long?)null : buffer.Length;
+                       }
+               }
+
                public Task CopyToAsync (Stream stream)
                {
                        return CopyToAsync (stream, null);
index 413e459526e155331dd378ae912f78f61e99554e..3835eff66d5dc382557075dfae3efaf3f3d14d14 100644 (file)
@@ -90,6 +90,20 @@ namespace MonoTests.System.Net.Http
                        }
                }
 
+               class CannotSeekStream : MemoryStream
+               {
+                       public CannotSeekStream ()
+                               : base (new byte [11])
+                       {
+                       }
+
+                       public override bool CanSeek {
+                               get {
+                                       return false;
+                               }
+                       }
+               }
+
                [Test]
                public void Ctor_Invalid ()
                {
@@ -407,5 +421,13 @@ namespace MonoTests.System.Net.Http
                        Assert.AreEqual (0, stream_read.Position, "#3");        
                        Assert.AreEqual (1, stream_read.Length, "#4");
                }
+
+               [Test]
+               public void ContentLengthAfterLoad ()
+               {
+                       var sc = new StreamContent (new CannotSeekStream ());
+                       Assert.IsTrue (sc.LoadIntoBufferAsync ().Wait (3000), "#1");
+                       Assert.AreEqual (11, sc.Headers.ContentLength, "#2");
+               }
        }
 }