# Path: openid/yadis/parsehtml.py
# class MetaNotFound(Exception):
#     """Yadis meta tag not found in the HTML page."""
# 
# def findHTMLMeta(stream):
#     """Look for a meta http-equiv tag with the YADIS header name.
# 
#     @param stream: Source of the html text
#     @type stream: Readable text I/O file object
# 
#     @return: The URI from which to fetch the XRDS document
#     @rtype: six.text_type
# 
#     @raises MetaNotFound: raised with the content that was
#         searched as the first parameter.
#     """
#     parser = etree.HTMLParser()
#     try:
#         html = etree.parse(stream, parser)
#     except (ValueError, etree.XMLSyntaxError):
#         raise MetaNotFound("Couldn't parse HTML page.")
# 
#     # Invalid input may return element with no content
#     if html.getroot() is None:
#         raise MetaNotFound("Couldn't parse HTML page.")
# 
#     # Create a XPath evaluator with a local function to lowercase values.
#     xpath_evaluator = etree.XPathEvaluator(html, extensions={(None, 'lower-case'): xpath_lower_case})
#     # Find YADIS meta tag, case insensitive to the header name.
#     yadis_headers = xpath_evaluator('/html/head/meta[lower-case(@http-equiv)="{}"]'.format(YADIS_HEADER_NAME.lower()))
#     if not yadis_headers:
#         raise MetaNotFound('Yadis meta tag not found.')
# 
#     yadis_header = yadis_headers[0]
#     yadis_url = yadis_header.get('content')
#     if yadis_url is None:
#         raise MetaNotFound('Attribute "content" missing in yadis meta tag.')
#     return yadis_url
# 
# def xpath_lower_case(context, values):
#     """Return lower cased values in XPath."""
#     return [v.lower() for v in values]


import unittest
from mock import sentinel
from six import StringIO
from openid.yadis.parsehtml import MetaNotFound, findHTMLMeta, xpath_lower_case
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_invalid_html(self):
        buff = StringIO('<!bad processing instruction!>')
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_meta_in_body(self):
        buff = StringIO('<html><head><body><meta http-equiv="X-XRDS-Location" content="found">')
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_no_content(self):
        buff = StringIO('<html><head><meta http-equiv="X-XRDS-Location"></head></html>')
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_commented_header(self):
        buff = StringIO('<html><head>'
                        '<!--<meta http-equiv="X-XRDS-Location" content="found">-->'
                        '</head></html>')
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_no_yadis_header(self):
        buff = StringIO("<html><head><title>A boring document</title></head>"
                        "<body><h1>A boring document</h1><p>There's really nothing interesting about this</p></body>"
                        "</html>")
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_unclosed_tag(self):
        # script tag not closed
        buff = StringIO('<html><head><script type="text/javascript">document.write("<body>");'
                        '<META http-equiv="X-XRDS-Location" content="found">')

# Path: openid/yadis/parsehtml.py
# class MetaNotFound(Exception):
#     """Yadis meta tag not found in the HTML page."""
# 
# def findHTMLMeta(stream):
#     """Look for a meta http-equiv tag with the YADIS header name.
# 
#     @param stream: Source of the html text
#     @type stream: Readable text I/O file object
# 
#     @return: The URI from which to fetch the XRDS document
#     @rtype: six.text_type
# 
#     @raises MetaNotFound: raised with the content that was
#         searched as the first parameter.
#     """
#     parser = etree.HTMLParser()
#     try:
#         html = etree.parse(stream, parser)
#     except (ValueError, etree.XMLSyntaxError):
#         raise MetaNotFound("Couldn't parse HTML page.")
# 
#     # Invalid input may return element with no content
#     if html.getroot() is None:
#         raise MetaNotFound("Couldn't parse HTML page.")
# 
#     # Create a XPath evaluator with a local function to lowercase values.
#     xpath_evaluator = etree.XPathEvaluator(html, extensions={(None, 'lower-case'): xpath_lower_case})
#     # Find YADIS meta tag, case insensitive to the header name.
#     yadis_headers = xpath_evaluator('/html/head/meta[lower-case(@http-equiv)="{}"]'.format(YADIS_HEADER_NAME.lower()))
#     if not yadis_headers:
#         raise MetaNotFound('Yadis meta tag not found.')
# 
#     yadis_header = yadis_headers[0]
#     yadis_url = yadis_header.get('content')
#     if yadis_url is None:
#         raise MetaNotFound('Attribute "content" missing in yadis meta tag.')
#     return yadis_url
# 
# def xpath_lower_case(context, values):
#     """Return lower cased values in XPath."""
#     return [v.lower() for v in values]


# Path: openid/test/test_parsehtml.py
import unittest
from mock import sentinel
from six import StringIO
from openid.yadis.parsehtml import MetaNotFound, findHTMLMeta, xpath_lower_case

        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_invalid_html(self):
        buff = StringIO('<!bad processing instruction!>')
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_meta_in_body(self):
        buff = StringIO('<html><head><body><meta http-equiv="X-XRDS-Location" content="found">')
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_no_content(self):
        buff = StringIO('<html><head><meta http-equiv="X-XRDS-Location"></head></html>')
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_commented_header(self):
        buff = StringIO('<html><head>'
                        '<!--<meta http-equiv="X-XRDS-Location" content="found">-->'
                        '</head></html>')
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_no_yadis_header(self):
        buff = StringIO("<html><head><title>A boring document</title></head>"
                        "<body><h1>A boring document</h1><p>There's really nothing interesting about this</p></body>"
                        "</html>")
        self.assertRaises(MetaNotFound, findHTMLMeta, buff)

    def test_unclosed_tag(self):
        # script tag not closed
        buff = StringIO('<html><head><script type="text/javascript">document.write("<body>");'
                        '<META http-equiv="X-XRDS-Location" content="found">')

        self.assertRaises(MetaNotFound, findHTMLMeta, buff)