File: environ_tests.py

package info (click to toggle)
jython 2.7.2%2Brepack1-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 62,676 kB
  • sloc: python: 640,908; java: 306,458; xml: 1,984; sh: 522; ansic: 126; makefile: 76
file content (107 lines) | stat: -rw-r--r-- 3,196 bytes parent folder | download | duplicates (7)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# -*- coding: windows-1252 -*-

###
#
# Copyright Alan Kennedy. 
# 
# You may contact the copyright holder at this uri:
# 
# http://www.xhaus.com/contact/modjy
# 
# The licence under which this code is released is the Apache License v2.0.
# 
# The terms and conditions of this license are listed in a file contained
# in the distribution that also contained this file, under the name
# LICENSE.txt.
# 
# You may also read a copy of the license at the following web address.
# 
# http://modjy.xhaus.com/LICENSE.txt
#
###


"""
    A variety of app callables used to test the WSGI environment.
"""

def test_echo_wsgi_env(environ, start_response):
    writer = start_response("200 OK", [])
    output_dict = {}
    for k in environ["QUERY_STRING"].split(';'):
        output_dict[k] = environ[k]
    return [repr(output_dict)]

def test_env_is_dict(environ, start_response):
    writer = start_response("200 OK", [])
    if type(environ) is type({}):
        writer("true")
    else:
        writer("false")
    return []

def test_env_is_mutable(environ, start_response):
    writer = start_response("200 OK", [])
    try:
        environ['some_key'] = 'some value'
        writer("true")
    except:
        writer("false")
    return []

def test_env_contains_request_method(environ, start_response):
    writer = start_response("200 OK", [])
    try:
        writer(environ['REQUEST_METHOD'])
    except KeyError, k:
        writer(str(k))
    return []

def test_env_script_name_path_info(environ, start_response):
    writer = start_response("200 OK", [])
    writer("%s:::%s" % (environ['SCRIPT_NAME'], environ['PATH_INFO']))
    return []

def test_env_query_string(environ, start_response):
    writer = start_response("200 OK", [])
    writer(environ['QUERY_STRING'])
    return []

required_cgi_vars = [
    'REQUEST_METHOD',
    'SCRIPT_NAME',
    'PATH_INFO',
    'QUERY_STRING',
    'CONTENT_TYPE',
    'CONTENT_LENGTH',
    'SERVER_NAME',
    'SERVER_PORT',
    'SERVER_PROTOCOL',
]

other_cgi_vars = [
]

def test_cgi_vars_are_str(environ, start_response):
    start_response("200 OK", [])
    for rcv in required_cgi_vars:
        if environ.has_key(rcv) and not isinstance(environ[rcv], str):
            return ["fail: '%s(%s)' is not 'str'" % (rcv, str(type(environ[rcv])))]
    for ocv in other_cgi_vars:
        if environ.has_key(ocv) and not isinstance(environ[ocv], str):
            return ["fail: '%s(%s)' is not 'str'" % (ocv, str(type(environ[ocv])))]
    for k in environ.keys():
        if k.startswith('HTTP_') and not isinstance(environ[k], str):
            return ["fail: '%s(%s)' is not 'str'" % (k, str(type(environ[k])))]
    return ["pass"]

def test_multiple_header_values(environ, start_response):
    start_response("200 OK", [])
    env_var_name = 'HTTP_MULTIPLE'
    env_var = environ[env_var_name]
    if not isinstance(env_var, list):
        return ["fail: environment variable '%s' is not a list, but a '%s'" % (env_var_name, type(env_var))]
    for ix, ev in enumerate(env_var):
        if not isinstance(ev, str):
            return ["fail: environment variable '%s'[%d] is not a str, but a '%s'" % (env_var_name, ix, type(ev))]
    return ["pass"]