def __init__(self, headers): """Takes headers as a string. """ diesel_headers = DieselHeaders() diesel_headers.parse(headers) self._diesel_headers = diesel_headers Mapping.__init__(self) self._dict.update(diesel_headers._headers)
def __init__(self, raw): """Takes a string of type application/x-www-form-urlencoded. """ self.decoded = urllib.unquote_plus(raw).decode('UTF-8') self.raw = raw Mapping.__init__(self, cgi.parse_qs( self.decoded , keep_blank_values = True , strict_parsing = False ))
def __init__(self, headers): """Takes headers as a string. """ Mapping.__init__(self) hd = defaultdict(list) for line in headers.splitlines(): k, v = line.strip().split(':', 1) hd[k.strip().lower()].append(v.strip()) self._dict.update(hd)
def __init__(self, headers): """Takes headers as a string. """ Mapping.__init__(self) hd = {} for line in headers.splitlines(): k, v = line.strip().split(': ', 1) hd[k.lower()] = v self._dict.update(hd)
def __init__(self, raw): """Takes a string of type application/x-www-form-urlencoded. """ self.decoded = urllib.unquote_plus(raw).decode("UTF-8") self.raw = raw # parse_qs does its own unquote_plus'ing ... as_dict = cgi.parse_qs(raw, keep_blank_values=True, strict_parsing=False) # ... but doesn't decode to unicode. for k, vals in as_dict.items(): as_dict[k.decode("UTF-8")] = [v.decode("UTF-8") for v in vals] Mapping.__init__(self, as_dict)
def __init__(self, raw): """Takes a string of type application/x-www-form-urlencoded. """ self.decoded = urllib.unquote_plus(raw).decode('UTF-8') self.raw = raw # parse_qs does its own unquote_plus'ing ... as_dict = cgi.parse_qs(raw, keep_blank_values=True, strict_parsing=False) # ... but doesn't decode to unicode. for k, vals in as_dict.items(): as_dict[k.decode('UTF-8')] = [v.decode('UTF-8') for v in vals] Mapping.__init__(self, as_dict)
def extract_rfc2396_params(path): """RFC2396 section 3.3 says that path components of a URI can have 'a sequence of parameters, indicated by the semicolon ";" character.' and that ' Within a path segment, the characters "/", ";", "=", and "?" are reserved.' This way you can do /frisbee;color=red;size=small/logo;sponsor=w3c;color=black/image.jpg and each path segment gets its own params. * path should be raw so we don't split or operate on a decoded character * output is decoded """ pathsegs = path.lstrip(b'/').split(b'/') def decode(input): return urllib.unquote(input).decode('UTF-8') segments_with_params = [] for component in pathsegs: parts = component.split(b';') params = Mapping() segment = decode(parts[0]) for p in parts[1:]: if '=' in p: k, v = p.split(b'=', 1) else: k, v = p, b'' params.add(decode(k), decode(v)) segments_with_params.append(PathPart(segment, params)) return segments_with_params
def test_mapping_popall_removes_the_item(): m = Mapping() m['foo'] = 1 m['foo'] = 1 m['foo'] = 3 m.popall('foo') assert 'foo' not in m, m.keys()
def test_mapping_deleting_a_key_removes_it_entirely(): m = Mapping() m['foo'] = 1 m['foo'] = 2 m['foo'] = 3 del m['foo'] assert 'foo' not in m, m.keys()
def test_mapping_pop_removes_the_item_if_that_was_the_last_value(): m = Mapping() m['foo'] = 1 m.pop('foo') expected = [] actual = m.keys() assert actual == expected, actual
def test_mapping_subscript_assignment_clobbers(): m = Mapping() m['foo'] = 'bar' m['foo'] = 'baz' m['foo'] = 'buz' expected = ['buz'] actual = dict.__getitem__(m, 'foo') assert actual == expected, actual
def test_mapping_popall_returns_a_list(): m = Mapping() m['foo'] = 1 m.add('foo', 1) m.add('foo', 3) expected = [1, 1, 3] actual = m.popall('foo') assert actual == expected, actual
def test_mapping_all_returns_list_of_all_values(): m = Mapping() m['foo'] = 'bar' m.add('foo', 'baz') m.add('foo', 'buz') expected = ['bar', 'baz', 'buz'] actual = m.all('foo') assert actual == expected, actual
def test_mapping_pop_returns_the_last_item(): m = Mapping() m['foo'] = 1 m.add('foo', 1) m.add('foo', 3) expected = 3 actual = m.pop('foo') assert actual == expected, actual
def test_mapping_get_returns_last(): m = Mapping() m['foo'] = 'bar' m['foo'] = 'baz' m['foo'] = 'buz' expected = 'buz' actual = m.get('foo') assert actual == expected, actual
def test_mapping_subscript_access_returns_last(): m = Mapping() m['foo'] = 'bar' m['foo'] = 'baz' m['foo'] = 'buz' expected = 'buz' actual = m['foo'] assert actual == expected, actual
def test_mapping_pop_leaves_the_rest(): m = Mapping() m['foo'] = 1 m.add('foo', 1) m.add('foo', 3) m.pop('foo') expected = [1, 1] actual = m.all('foo') assert actual == expected, actual
def test_default_mapping_is_case_insensitive(): m = Mapping() m['Foo'] = 1 m['foo'] = 1 m['fOO'] = 1 m['FOO'] = 1 expected = [1] actual = m.all('foo') assert actual == expected, actual
def test_accessing_missing_key_calls_keyerror(): m = Mapping() class Foobar(Exception): pass def raise_foobar(self): raise Foobar m.keyerror = raise_foobar raises(Foobar, lambda k: m[k], 'foo') raises(Foobar, m.ones, 'foo')
def test_mapping_ones_returns_list_of_last_values(): m = Mapping() m['foo'] = 1 m['foo'] = 2 m['bar'] = 3 m['bar'] = 4 m['bar'] = 5 m['baz'] = 6 m['baz'] = 7 m['baz'] = 8 m['baz'] = 9 expected = [2, 5, 9] actual = m.ones('foo', 'bar', 'baz') assert actual == expected, actual
def test_mapping_calling_ones_with_missing_key_raises_Response(): m = Mapping() assert_raises(Response, m.ones, 'foo')
def test_accessing_missing_key_raises_Response(): m = Mapping() assert_raises(Response, lambda k: m[k], 'foo')
def test_mapping_all_returns_empty_list_when_key_is_missing(): m = Mapping() expected = [] actual = m.all('foo') assert actual == expected
def test_mapping_get_default_default_is_None(): m = Mapping() expected = None actual = m.get('foo') assert actual is expected, actual
def test_mapping_get_returns_default(): m = Mapping() expected = 'cheese' actual = m.get('foo', 'cheese') assert actual == expected, actual
def test_mapping_pop_raises_KeyError_by_default(): m = Mapping() with raises(KeyError): m.pop('foo')