Just a few more examples, kind of a follow up to my Simian Satasin Asynchronous Calls Entry. Keep in mind, I’m providing these as examples, I do NOT suggest writing tests with Thread.Wait calls in them. Very very bad practice.
[sourcecode language=”csharp”]
[TestClass]
public class GetRestServices
{
private XDocument doc;
[TestMethod]
public void TestSynchronousCallWithHttpWebRequest()
{
var request = (HttpWebRequest) WebRequest.Create("https://twitter.com/statuses/public_timeline.xml");
var response = request.GetResponse();
var responseStream = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
var doc = new XmlDocument();
doc.Load(responseStream);
Assert.IsNotNull(doc);
}
[TestMethod]
public void TestAsynchronousCall()
{
const string baseUri = "http://twitter.com/statuses/public_timeline.xml";
var svc = new WebClient();
svc.DownloadStringCompleted += svc_DownloadStringCompleted;
svc.DownloadStringAsync(new Uri(baseUri));
Thread.Sleep(1000);
Assert.IsNotNull(doc);
var docTest = new XDocument();
Assert.IsInstanceOfType(doc, docTest.GetType());
}
private void svc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
doc = XDocument.Parse(e.Result);
}
[TestMethod]
public void TestAsynchronousCallWithSecurity()
{
const string baseUri = "http://twitter.com/statuses/friends_timeline.xml";
var svc = new WebClient();
svc.Credentials = new NetworkCredential("yourtuweetusername", "yourtuweetpassword");
svc.DownloadStringCompleted += svc_DownloadStringCompleted;
svc.DownloadStringAsync(new Uri(baseUri));
Thread.Sleep(3000);
Assert.IsNotNull(doc);
var docTest = new XDocument();
Assert.IsInstanceOfType(doc, docTest.GetType());
}
}
[/sourcecode]
Hope those are helpful for those searching.