File: test_examples.py

package info (click to toggle)
python-dugong 3.3%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 548 kB
  • ctags: 388
  • sloc: python: 2,205; makefile: 20; sh: 1
file content (91 lines) | stat: -rwxr-xr-x 3,053 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
'''
test_dugong.py - Unit tests for Dugong - run with py.test

Copyright (c) Nikolaus Rath <Nikolaus@rath.org>

This module may be distributed under the terms of the Python Software Foundation
License Version 2.  The complete license text may be retrieved from
http://hg.python.org/cpython/file/65f2c92ed079/LICENSE.
'''

if __name__ == '__main__':
    import pytest
    import sys
    sys.exit(pytest.main([__file__] + sys.argv[1:]))

import subprocess
import os
import sys
import pytest
from urllib.request import (build_opener, ProxyHandler, URLError, HTTPRedirectHandler,
                            HTTPError)
try:
    import asyncio
except ImportError:
    asyncio = None

basename = os.path.join(os.path.dirname(__file__), '..')

# Yes, this is the way to avoid following redirects with urllib,
# and it is indeed absolutely terrible. But dependending on the
# requests module just to run this simple test does not (yet)
# seem appropriate either.
class HTTPNoRedirectHandler(HTTPRedirectHandler):
    def http_error_302(self, url, fp, errcode, errmsg, headers):
        fp.close()
        raise HTTPError(url, errcode, errmsg, headers, None)

    http_error_301 = http_error_303 = http_error_307 = http_error_302

def check_url(url):
    '''Skip test if *url* cannot be reached'''

    # To avoid using proxies (as the examples do), we need to add a proxy
    # handler, but pass an empty dictionary for the proxy settings. urllib
    # again...
    opener = build_opener(ProxyHandler({}), HTTPNoRedirectHandler())

    try:
        resp = opener.open(url, None, 15)
    except URLError:
        pytest.skip('%s not reachable but required for testing' % url)

    if resp.status != 200:
        pytest.skip('%s not reachable but required for testing' % url)

    resp.close()

def test_httpcat():
    url =  'http://docs.oracle.com/javaee/7/firstcup/doc/creating-example.htm'
    check_url(url)
    cmdline = [sys.executable,
               os.path.join(basename, 'examples', 'httpcat.py'), url ]

    with open('/dev/null', 'wb') as devnull:
        subprocess.check_call(cmdline, stdout=devnull)

def test_extract_links():
    url =  'http://docs.oracle.com/javaee/7/firstcup/doc/creating-example.htm'
    check_url(url)
    cmdline = [sys.executable,
              os.path.join(basename, 'examples', 'extract_links.py'), url ]

    with open('/dev/null', 'wb') as devnull:
        subprocess.check_call(cmdline, stdout=devnull)

@pytest.mark.skipif(asyncio is None,
                    reason='asyncio module not available')
def test_pipeline1():
    cmdline = [sys.executable,
              os.path.join(basename, 'examples', 'pipeline1.py') ]

    for x in ('preface.htm', 'intro.htm', 'java-ee.htm',
              'creating-example.htm', 'next-steps.htm',
              'creating-example001.htm'):
        url = 'http://docs.oracle.com/javaee/7/firstcup/doc/' + x
        check_url(url)
        cmdline.append(url)

    with open('/dev/null', 'wb') as devnull:
        subprocess.check_call(cmdline, stdout=devnull)