#!/usr/bin/python
"""
$Id: refresh_redirect.py,v 1.9 2006/11/14 17:39:00 dom Exp $

The RefreshRedirectTestCase implements the validator.testcase.TestCase
interface for the tests related to the following BPs:
* Do not create periodically auto-refreshing pages, unless you have
  informed the user and provided a means of stopping it.
* Do not use markup to redirect pages automatically. Instead,
  configure the server to perform redirects by means of HTTP 3xx codes.
  
License
-------
Copyright (c) 2006 World Wide Web Consortium, (Massachusetts
Institute of Technology, European Research Consortium for Informatics
and Mathematics, Keio University). All Rights Reserved. This work is
distributed under the W3C Software License [1] in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[1] http://www.w3.org/Consortium/Legal/copyright-software

"""


from  validator import testcase 

# Tests regarding the AUTO_REFRESH and REDIRECTION BP
class RefreshRedirectTestCase(testcase.XMLBasedTestCase):
    BpId = ["AUTO_REFRESH","REDIRECTION"]

    def startElement(self, name, attrs):
        if name=="meta" and attrs.has_key("http-equiv") and attrs["http-equiv"].lower()=='refresh' and attrs.has_key("content"):
            location = testcase.LineColumnLocation(self.uri,self.getEncoding(),self.locator.getLineNumber(),self.locator.getColumnNumber())
            if attrs["content"]==self.uri:
                # refresh
                self.addObservation(testcase.Observation("RR1",location))
            else:
                # redirect
                self.addObservation(testcase.Observation("RR2",location))


    def endDocument(self):
        if self._headers.has_key("refresh"):
            if self._headers["refresh"]==self.uri:
                self.addObservation(testcase.Observation("RR4",location))
            else:
                self.addObservation(testcase.Observation("RR5",location))
        if not "RR1" in self._observations and not "RR2" in self._observations and not "RR4" in self._observations and not "RR5" in self._observations:
            self.addObservation(testcase.Observation("RR3",testcase.Location(self.uri))) 



# -------------------------
# Unit tests for this module
import unittest

from validator.testcase import TestResults, Observation, Location, LineColumnLocation
class Tests(unittest.TestCase):
    def _test(self,inp,res):
        a = RefreshRedirectTestCase()
        self.assertEqual(a.run(inp),res)
    
    def testWithNeither(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/base.xhtml"
        res = TestResults(
            [Observation("RR3",Location(inp))]
            )
        self._test(inp,res)
        

    def testWithRedirect(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/redirect.xhtml"
        res = TestResults(
            [Observation("RR2",LineColumnLocation(inp,'utf-8',6,0))])
        self._test(inp,res)

    def testWithRefresh(self):
        inp = "http://dev.w3.org/cvsweb/~checkout~/2006/mwbp-validator/tests/refresh.xhtml"
        res = TestResults(
            [Observation("RR1",LineColumnLocation(inp,'utf-8',6,0))])
        self._test(inp,res)


def _test():
    import doctest, access_key
    doctest.testmod(access_key)
    unittest.main()

if __name__ == '__main__':
    _test()



