Last active
November 15, 2018 17:31
-
-
Save chrisfcarroll/69fe3f94dd5f8e2132d8b898dfcb3515 to your computer and use it in GitHub Desktop.
.Net WebClientBase, WebClientWrapper and WebClientFake for a testable System.Net.WebClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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();} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.