public static void StartAsyncWebRequest(string url, Action <ICsqWebResponse> callback, object id = null) { var request = new CsqWebRequest(url); request.Id = id; request.Async = true; var mrEvent = request.GetAsync(callback); AsyncEvents.Add(mrEvent); }
public void Utf8NoContentType() { var creator = new Mocks.MockWebRequestCreator(); creator.CharacterSet = "ISO-8859-1"; creator.ResponseStream = GetMemoryStream(TestHtml("arabic"), new UTF8Encoding(false)); CsqWebRequest request = new CsqWebRequest("http://test.com", creator); // remove the content type header var html = ReplaceCharacterSet(request.Get()); var dom = CQ.CreateDocument(html); Assert.AreNotEqual(arabicExpected, dom["h1"].Text()); //test synchronous: this is the code that CreateFromURL uses creator.CharacterSet = null; request = new CsqWebRequest("http://test.com", creator); var httpRequest = request.GetWebRequest(); var response = httpRequest.GetResponse(); var responseStream = response.GetResponseStream(); var encoding = CsqWebRequest.GetEncoding(response); var dom2 = CQ.CreateDocument(responseStream, encoding); Assert.AreEqual(arabicExpected, dom2["h1"].Text()); // Test async version now request = new CsqWebRequest("http://test.com", creator); bool?done = null; CQ dom3 = null; request.GetAsync((r) => { dom3 = r.Dom; done = true; }, (r) => { done = false; }); while (done == null) { ; } Assert.IsTrue((bool)done); Assert.AreEqual(arabicExpected, dom3["h1"].Text()); }
public void GetHtmlAsyncTimeout() { var creator = new Mocks.MockWebRequestCreator(); creator.ResponseTime = 1000; creator.ResponseStream = GetMemoryStream("<div>Test</div>"); var request = new CsqWebRequest("http://test.com", creator); request.Options = new ServerConfig { Timeout = TimeSpan.FromMilliseconds(500), UserAgent = "test" }; bool?done = null; request.GetAsync((r) => { done = true; }, (r) => { done = false; }); while (done == null) { ; } Assert.IsFalse((bool)done); creator.ResponseTime = 300; request = new CsqWebRequest("http://test.com", creator); done = null; request.GetAsync((r) => { done = true; }, (r) => { done = false; }); while (done == null) { ; } Assert.IsTrue((bool)done); }