1import os
2import sys
3from nose.tools import assert_raises
4from paste.cgiapp import CGIApplication, CGIError
5from paste.fixture import *
6
7data_dir = os.path.join(os.path.dirname(__file__), 'cgiapp_data')
8
9# these CGI scripts can't work on Windows or Jython
10if sys.platform != 'win32' and not sys.platform.startswith('java'):
11    def test_ok():
12        app = TestApp(CGIApplication({}, script='ok.cgi', path=[data_dir]))
13        res = app.get('')
14        assert res.header('content-type') == 'text/html; charset=UTF-8'
15        assert res.full_status == '200 Okay'
16        assert 'This is the body' in res
17
18    def test_form():
19        app = TestApp(CGIApplication({}, script='form.cgi', path=[data_dir]))
20        res = app.post('', params={'name': b'joe'},
21                       upload_files=[('up', 'file.txt', b'x'*10000)])
22        assert 'file.txt' in res
23        assert 'joe' in res
24        assert 'x'*10000 in res
25
26    def test_error():
27        app = TestApp(CGIApplication({}, script='error.cgi', path=[data_dir]))
28        assert_raises(CGIError, app.get, '', status=500)
29
30    def test_stderr():
31        app = TestApp(CGIApplication({}, script='stderr.cgi', path=[data_dir]))
32        res = app.get('', expect_errors=True)
33        assert res.status == 500
34        assert 'error' in res
35        assert b'some data' in res.errors
36
37