Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
.Net WebClientBase, WebClientWrapper and WebClientFake for a testable System.Net.WebClient
public abstract class WebClientBase : IDisposable
{
WebHeaderCollection headers = new WebHeaderCollection();
public abstract void UploadValuesAsync(Uri address, string method, NameValueCollection data);
public abstract void UploadValuesAsync(Uri address, string method, NameValueCollection data, object userToken);
public abstract byte[] UploadData(Uri address, string method, byte[] data);
public abstract byte[] UploadFile(Uri address, string method, string fileName);
public abstract byte[] UploadValues(Uri address, string method, NameValueCollection data);
public abstract string UploadString(Uri address, string method, string data);
public static implicit operator WebClientBase(WebClient webclient)
{
return new WebClientWrapper(webclient);
}
public abstract void Dispose();
public virtual WebHeaderCollection Headers
{
get { return headers; }
set { headers = value; }
}
public abstract Task<byte[]> UploadValuesTaskAsync(Uri address, string method, NameValueCollection data);
}
public class WebClientWrapper : WebClientBase
{
readonly WebClient webClient;
public WebClientWrapper(WebClient webClient)
{
this.webClient = webClient;
}
public override void Dispose(){ webClient.Dispose(); }
public override WebHeaderCollection Headers
{
get { return webClient.Headers;}
set { webClient.Headers = value; }
}
public override async Task<byte[]> UploadValuesTaskAsync(Uri address, string method, NameValueCollection data)
{
return await webClient.UploadValuesTaskAsync(address, method, data);
}
public override void UploadValuesAsync(Uri address, string method, NameValueCollection data)
{
webClient.UploadValuesAsync(address, method, data);
}
public override void UploadValuesAsync(Uri address, string method, NameValueCollection data, object userToken)
{
webClient.UploadValuesAsync(address, method, data, userToken);
}
public override byte[] UploadData(Uri address, string method, byte[] data)
{
return webClient.UploadData(address, method, data);
}
public override byte[] UploadFile(Uri address, string method, string fileName)
{
return webClient.UploadFile(address, method, fileName);
}
public override byte[] UploadValues(Uri address, string method, NameValueCollection data)
{
return webClient.UploadValues(address, method, data);
}
public override string UploadString(Uri address, string method, string data)
{
return webClient.UploadString(address, method, data);
}
}
public class WebClientFake : WebClientBase
{
public byte[] FakeResponse = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new {id = 1}));
public override void UploadValuesAsync(Uri address, string method, NameValueCollection data)
{
UploadedValues.Add(Tuple.Create(address, method, data));
}
public override void UploadValuesAsync(Uri address, string method, NameValueCollection data, object userToken)
{
UploadedValues.Add(Tuple.Create(address, method, data));
}
public override byte[] UploadData(Uri address, string method, byte[] data)
{
UploadedStrings.Add(Tuple.Create(address, method, Encoding.Unicode.GetString(data)));
return FakeResponse;
}
public override byte[] UploadFile(Uri address, string method, string fileName)
{
UploadedFileNames.Add(Tuple.Create(address, method, fileName));
return FakeResponse;
}
public override byte[] UploadValues(Uri address, string method, NameValueCollection data)
{
UploadedValues.Add(Tuple.Create(address, method, data));
return FakeResponse;
}
public override string UploadString(Uri address, string method, string data)
{
UploadedStrings.Add(Tuple.Create(address, method, data));
return Encoding.Unicode.GetString( FakeResponse);
}
public override void Dispose(){}
public override async Task<byte[]> UploadValuesTaskAsync(Uri address, string method, NameValueCollection data)
{
return await Task.FromResult(UploadValues(address, method, data));
}
public readonly List<Tuple<Uri, string, NameValueCollection>> UploadedValues = new List<Tuple<Uri, string, NameValueCollection>>();
public readonly List<Tuple<Uri,string,string>> UploadedStrings = new List<Tuple<Uri, string, string>>();
public readonly List<Tuple<Uri,string,string>> UploadedFileNames = new List<Tuple<Uri, string, string>>();
public WebClientFake(){Headers= new WebHeaderCollection();}
}
@chrisfcarroll

This comment has been minimized.

Copy link
Owner Author

commented Nov 5, 2018

Note these are uncomplete. I've only used UploadValuesTaskAsync, UploadValues so far, so you will need to carry on adding methods, as and when you need them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.