summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/test/System.Net.Http.Formatting.Test.Unit/Mocks/MockHttpContent.cs
blob: 683e710d2763e040a82a40e63c1b575ddb45b1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.IO;
using System.Net.Http.Headers;
using System.Threading.Tasks;

namespace System.Net.Http.Formatting.Mocks
{
    public delegate bool TryComputeLengthDelegate(out long length);

    public class MockHttpContent : HttpContent
    {
        public MockHttpContent()
        {
        }

        public MockHttpContent(HttpContent innerContent)
        {
            InnerContent = innerContent;
            Headers.ContentType = innerContent.Headers.ContentType;
        }

        public MockHttpContent(MediaTypeHeaderValue contentType)
        {
            if (contentType == null)
            {
                throw new ArgumentNullException("contentType");
            }
            Headers.ContentType = contentType;
        }

        public MockHttpContent(string contentType)
        {
            if (String.IsNullOrWhiteSpace(contentType))
            {
                throw new ArgumentNullException("contentType");
            }
            Headers.ContentType = new MediaTypeHeaderValue(contentType);
        }

        public HttpContent InnerContent { get; set; }

        public Action<bool> DisposeCallback { get; set; }
        public TryComputeLengthDelegate TryComputeLengthCallback { get; set; }
        public Action<Stream, TransportContext> SerializeToStreamCallback { get; set; }
        public Func<Stream, TransportContext, Task> SerializeToStreamAsyncCallback { get; set; }

        protected override void Dispose(bool disposing)
        {
            if (DisposeCallback != null)
            {
                DisposeCallback(disposing);
            }

            base.Dispose(disposing);
        }

        protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            if (SerializeToStreamAsyncCallback != null)
            {
                return SerializeToStreamAsyncCallback(stream, context);
            }
            else if (InnerContent != null)
            {
                return InnerContent.CopyToAsync(stream, context);
            }
            else
            {
                throw new InvalidOperationException("Construct with inner HttpContent or set SerializeToStreamCallback first.");
            }
        }

        protected override bool TryComputeLength(out long length)
        {
            if (TryComputeLengthCallback != null)
            {
                return TryComputeLengthCallback(out length);
            }

            if (InnerContent != null)
            {
                long? len = InnerContent.Headers.ContentLength;
                length = len.HasValue ? len.Value : 0L;
                return len.HasValue;
            }

            length = 0L;
            return false;
        }
    }
}