Unit Testing IsUsingIFrame function

by volkanuzun 6/13/2008 8:40:00 PM

Yesterday, i showed a function which does a regular expression check on a passed string, and returns true/false. Below is the code again one more time:

public bool IsUsingIFrame(string htmlSource)
        {
            if (htmlSource == string.Empty)
                return false;
            string strRegExIFrameCheck = "<iframe\\s+.*src\\s*=\\s*[\"']http://www.csusb.edu/banner2007/?[\"']";
            Regex regexIFrameCheck = new Regex(strRegExIFrameCheck);
            return regexIFrameCheck.IsMatch(htmlSource);
        }

So lets unit test this function using ms unit test. Right click on the function name, and then click on create unit tests, click ok again on the next window. Name your project.

Rename the unit test that vs created for you to "CheckWhenThereisNoIframe" , we are going to pass a string that does not have any iframe and test it. below is the simple code for it

[TestMethod()]
        public void CheckWhenThereisNoIframe()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = "THERE IS NO FRAME TAG HERE"; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }

We should unit test the conditions where string is empty, when iframe is there with a wrong pattern, when there is iframe with right pattern, below is my code

[TestMethod()]
        public void CheckWhenThereisNoIframe()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = "THERE IS NO FRAME TAG HERE"; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }
        [TestMethod()]
        public void CheckWhenStringIsEmpty()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = string.Empty; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void CheckWhenIframeExists()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = "<iframe src='http://www.csusb.edu/banner2007/'/>"; // TODO: Initialize to an appropriate value
            bool expected = true; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void CheckWhenThereisIframeIncorrectPattern()
        {
            Scraper target = new Scraper(); // TODO: Initialize to an appropriate value
            string htmlSource = "<iframe src='www.google.com"; // TODO: Initialize to an appropriate value
            bool expected = false; // TODO: Initialize to an appropriate value
            bool actual;
            actual = target.IsUsingIFrame(htmlSource);
            Assert.AreEqual(expected, actual);
        }

 

tomorrow i will try to mock the webcreate method .

Tags:

Scraping ?

by volkanuzun 6/13/2008 8:12:00 AM

In our campus environment, we do have a banner that we strongly encourage other sites on campus to add the banner inside an iframe. I had to write an application which gets the urls from the database, and check if the site is using or banner or log, here is what we expect to see in the sites:

&lt;iframe src ="http://www.csusb.edu/banner2007" width="100%" height="90" frameborder="0" 
    scrolling="No" title="CSUSB main navigation"&gt;

 so i need a regular expression that checks the above signature, below is the function i wrote:

public bool IsUsingIFrame(string htmlSource)
    {
        if (htmlSource == string.Empty)
            return false;
        string strRegExIFrameCheck = "<iframe\\s+.*src\\s*=\\s*[\"']http://www.csusb.edu/banner2007/?[\"']";
        Regex regexIFrameCheck = new Regex(strRegExIFrameCheck);
        return regexIFrameCheck.IsMatch(htmlSource);
    }

It is a simple function which uses regular expressions to see if there is any pattern of iframe with the specified source. now the second part of the problem is connecting to the sites, to achieve this i use webrequest class as below

public string GetSource(string Url)
    {
        if (Url == String.Empty)
        {
            this.htmlSource = String.Empty;
            return String.Empty;
        }

        string htmlSource = String.Empty;

        if (!(Url.Contains("http")))
            Url = "http://" + Url;

        try
        {

            //create a web request
            WebRequest request = WebRequest.Create(Url);
            WebResponse response = request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            //himm encoding ? do we need this if everything is english? dunnp
            Encoding utf8Encode = Encoding.GetEncoding("utf-8");
            StreamReader readStream = new StreamReader(responseStream, utf8Encode);
            htmlSource = readStream.ReadToEnd();
        }
        catch
        {
            htmlSource = String.Empty;
        }
       
        this.htmlSource = htmlSource.ToLower();
        return htmlSource;
    }

tomorrow i ll try to write a unit test agains this :)

have fun

About the author

Volkan Uzun




E-mail me Send mail

Twitter

Calendar

<<  June 2008  >>
MoTuWeThFrSaSu
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

View posts in large calendar

Flickr Badge

www.flickr.com
This is a Flickr badge showing public photos from volkanuzun. Make your own badge here.

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in