def test_must_accept_all_types(self): parsed_header = parse_accept_header('*/*') self.assertDictEqual( parsed_header, { '*/*': 1.0 }, "Does not accept all types" )
def __init__(self, environ): """Wrap a WSGI environ dictionary.""" self.meta = environ.copy() self.query_string = self.meta.get('QUERY_STRING', '') # This must be done to avoid a bug in cgi.FieldStorage self.meta.setdefault('QUERY_STRING', '') self.method = self.meta['REQUEST_METHOD'].upper() self.path_info = '/' + self.meta.get('PATH_INFO', '')\ .lstrip('/') self.script_name = self.meta.get('SCRIPT_NAME', '') content_type = self.meta.get('CONTENT_TYPE', '') self.content_type, pdict = cgi.parse_header(content_type) self.charset = pdict.get('charset', 'utf-8') # TODO settings self.boundary = pdict.get('boundary', '') try: self.content_length = int( self.meta.get('CONTENT_LENGTH', 0)) except ValueError: self.content_length = 0 if self.content_length < 0: raise HttpNegativeContentLengthException() accept_hdr = self.meta.get('HTTP_ACCEPT', 'text/html') # TODO settings self.accept = parse_accept_header(accept_hdr) self._stream = self.meta['wsgi.input'] self.cookies = SimpleCookie() self.cookies.load(self.meta.get('HTTP_COOKIE', ''))
def test_must_accept_multiple_accept_types_for_same_media_type(self): parsed_header = parse_accept_header('text/plain; q=0.5,\ text/html; q=0.8, text/html; q=0.2, text/html') self.assertDictEqual(parsed_header, { 'text/plain': 0.5, 'text/html': 1.0, }, "ACCEPT HEADER does not accept multiple accept-types", )
def test_must_accept_multiple_types_with_plus_symbols(self): parsed_header = parse_accept_header('text/plain; q=0.5,\ text/xhtml+xml, text/x-dvi; q=0.8, text/x-c') self.assertDictEqual(parsed_header, { 'text/plain': 0.5, 'text/xhtml+xml': 1.0, 'text/x-dvi': 0.8, 'text/x-c': 1.0, }, "ACCEPT HEADER does not accept multiple types with plus symbols", )
def test_must_accept_multiple_types_even_one_without_semicolon(self): with self.assertRaises(HttpAcceptHeaderParserException, msg="ACCEPT HEADER must not validate without\ semicolon even with multiple types"): parsed_header = parse_accept_header('text/plain q=0.5,\ text/html, text/x-dvi; q=0.8, text/x-c')
def test_must_not_validate_without_semicolon(self): with self.assertRaises(HttpAcceptHeaderParserException, msg="ACCEPT HEADER must not validate without\ semicolon"): parse_accept_header('text/plain q=0.5')
def test_must_not_validate_only_a_type(self): with self.assertRaises(HttpAcceptHeaderParserException, msg="ACCEPT HEADER must contains subtype"): parse_accept_header('text')
def test_must_not_validate_some_text(self): with self.assertRaises(HttpAcceptHeaderParserException, msg="ACCEPT HEADER must not validate 'Some text'"): parse_accept_header('Some text')