Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Net.Http.Internal;
namespace System.Net.Http.Mocks
{
internal class MockNonClosingDelegatingStream : NonClosingDelegatingStream
{
public MockNonClosingDelegatingStream(Stream innerStream)
: base(innerStream)
{
}
}
}

View File

@ -0,0 +1,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;
}
}
}

View File

@ -0,0 +1,39 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Net.Http.Headers;
using System.Text;
namespace System.Net.Http.Formatting.Mocks
{
public class MockMediaTypeFormatter : MediaTypeFormatter
{
public bool CallBase { get; set; }
public Func<Type, bool> CanReadTypeCallback { get; set; }
public Func<Type, bool> CanWriteTypeCallback { get; set; }
public override bool CanReadType(Type type)
{
if (!CallBase && CanReadTypeCallback == null)
{
throw new InvalidOperationException("CallBase or CanReadTypeCallback must be set first.");
}
return CanReadTypeCallback != null ? CanReadTypeCallback(type) : true;
}
public override bool CanWriteType(Type type)
{
if (!CallBase && CanWriteTypeCallback == null)
{
throw new InvalidOperationException("CallBase or CanWriteTypeCallback must be set first.");
}
return CanWriteTypeCallback != null ? CanWriteTypeCallback(type) : true;
}
new public Encoding SelectCharacterEncoding(HttpContentHeaders contentHeaders)
{
return base.SelectCharacterEncoding(contentHeaders);
}
}
}

View File

@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.IO;
using System.Net.Http.Internal;
namespace System.Net.Http.Mocks
{
internal class MockDelegatingStream : DelegatingStream
{
public MockDelegatingStream(Stream innerStream)
: base(innerStream)
{
}
}
}

View File

@ -0,0 +1,20 @@
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Threading;
using System.Threading.Tasks;
namespace System.Net.Http.Mocks
{
public class TestableHttpMessageHandler : HttpMessageHandler
{
public virtual Task<HttpResponseMessage> SendAsyncPublic(HttpRequestMessage request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return SendAsyncPublic(request, cancellationToken);
}
}
}