Exemplo n.º 1
0
        public void StringAsyncDownloadTest()
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[]       contentAsBytes       = encoding.GetBytes(TestResources.CharacterSheet);
            MemoryStream sourceResponseStream = new MemoryStream();

            sourceResponseStream.Write(contentAsBytes, 0, contentAsBytes.Length);
            sourceResponseStream.Position = 0;
            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            _stringAsyncCompletedTrigger = new AutoResetEvent(false);
            HttpWebService service = new HttpWebService();

            service.DownloadStringAsync("http://www.battleclinic.com", StringAysncDownloadTestCompleted, null);
            _stringAsyncCompletedTrigger.WaitOne();
            if (_stringAsyncDownloadResult.Error != null)
            {
                Assert.Fail(_stringAsyncDownloadResult.Error.Message);
            }
            Assert.AreEqual(TestResources.CharacterSheet, _stringAsyncDownloadResult.Result);
            sourceResponseStream.Close();
            _stringAsyncCompletedTrigger = null;
            _stringAsyncDownloadResult   = null;
        }
Exemplo n.º 2
0
        public void ImageDownloadExceptionTest()
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[]       contentAsBytes       = encoding.GetBytes("This is not image data");
            MemoryStream sourceResponseStream = new MemoryStream();

            sourceResponseStream.Write(contentAsBytes, 0, contentAsBytes.Length);
            sourceResponseStream.Position = 0;

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            HttpWebService service = new HttpWebService();

            try
            {
                service.DownloadImage("http://www.battleclinic.com");
                Assert.Fail("Expected exception was not thrown");
            }
            catch (HttpWebServiceException ex)
            {
                Assert.AreEqual(HttpWebServiceExceptionStatus.ImageException, ex.Status);
            }
            finally
            {
                sourceResponseStream.Close();
            }
        }
Exemplo n.º 3
0
        public void ImageAsyncDownloadTest()
        {
            MemoryStream sourceResponseStream = new MemoryStream();

            TestResources.testimage.Save(sourceResponseStream, ImageFormat.Png);
            sourceResponseStream.Position = 0;

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            _imageAsyncCompletedTrigger = new AutoResetEvent(false);
            HttpWebService service = new HttpWebService();

            service.DownloadImageAsync("http://www.battleclinic.com", ImageAysncDownloadTestCompleted, null);
            _imageAsyncCompletedTrigger.WaitOne();
            if (_imageAsyncDownloadResult.Error != null)
            {
                Assert.Fail(_imageAsyncDownloadResult.Error.Message);
            }
            Assert.IsNotNull(_imageAsyncDownloadResult.Result, "No image retrieved");
            _imageAsyncCompletedTrigger = null;
            _imageAsyncDownloadResult   = null;
        }
Exemplo n.º 4
0
        public void XmlDownloadTest()
        {
            UTF8Encoding encoding = new UTF8Encoding();

            byte[]       contentAsBytes       = encoding.GetBytes(TestResources.CharacterSheet);
            MemoryStream sourceResponseStream = new MemoryStream();

            sourceResponseStream.Write(contentAsBytes, 0, contentAsBytes.Length);
            sourceResponseStream.Position = 0;
            XmlDocument contentAsXml = new XmlDocument();

            contentAsXml.Load(new StringReader(TestResources.CharacterSheet));

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            HttpWebService service = new HttpWebService();
            XmlDocument    result  = service.DownloadXml("http://www.battleclinic.com");

            StringAssert.AreEqualIgnoringCase(contentAsXml.ToString(), result.ToString());

            sourceResponseStream.Close();
        }
Exemplo n.º 5
0
        public void ImageDownloadTest()
        {
            MemoryStream sourceResponseStream = new MemoryStream();

            TestResources.testimage.Save(sourceResponseStream, ImageFormat.Png);
            sourceResponseStream.Position = 0;

            Mock <HttpWebRequest>        mockRequest  = MockManager.Mock <HttpWebRequest>(Constructor.NotMocked);
            MockObject <HttpWebResponse> mockResponse = MockManager.MockObject <HttpWebResponse>(Constructor.Mocked);

            mockResponse.ExpectAndReturn("GetResponseStream", sourceResponseStream);
            mockRequest.ExpectAndReturn("GetResponse", mockResponse.Object);

            HttpWebService service = new HttpWebService();

            try
            {
                service.DownloadImage("http://www.battleclinic.com");
            }
            finally
            {
                sourceResponseStream.Close();
            }
        }
 public void FakeWebResponseObject(string resource)
 {
     webResponseMock.ExpectAndReturn("GetResponseStream", GetResourceStream(resource));
     webResponseMock.ExpectCall("Close");
 }