def test_iterables(self): idict = {'Content-Type': 'text/html', 'X-Forwarded-For': ['ip1', 'ip2']} h = Headers(idict) self.assertEqual(dict(h), {'Content-Type': ['text/html'], 'X-Forwarded-For': ['ip1', 'ip2']}) self.assertEqual(h.keys(), ['X-Forwarded-For', 'Content-Type']) self.assertEqual(h.items(), [('X-Forwarded-For', ['ip1', 'ip2']), ('Content-Type', ['text/html'])]) self.assertEqual(list(h.iteritems()), [('X-Forwarded-For', ['ip1', 'ip2']), ('Content-Type', ['text/html'])]) self.assertEqual(h.values(), ['ip2', 'text/html'])
def test_iterables(self): idict = {'Content-Type': 'text/html', 'X-Forwarded-For': ['ip1', 'ip2']} h = Headers(idict) self.assertDictEqual(dict(h), {b'Content-Type': [b'text/html'], b'X-Forwarded-For': [b'ip1', b'ip2']}) self.assertSortedEqual(h.keys(), [b'X-Forwarded-For', b'Content-Type']) self.assertSortedEqual(h.items(), [(b'X-Forwarded-For', [b'ip1', b'ip2']), (b'Content-Type', [b'text/html'])]) self.assertSortedEqual(h.iteritems(), [(b'X-Forwarded-For', [b'ip1', b'ip2']), (b'Content-Type', [b'text/html'])]) self.assertSortedEqual(h.values(), [b'ip2', b'text/html'])
def test_headers(self): # Different ways of setting headers attribute url = 'http://www.scrapy.org' headers = {'Accept':'gzip', 'Custom-Header':'nothing to tell you'} r = self.request_class(url=url, headers=headers) p = self.request_class(url=url, headers=r.headers) self.assertEqual(r.headers, p.headers) self.assertFalse(r.headers is headers) self.assertFalse(p.headers is r.headers) # headers must not be unicode h = Headers({'key1': u'val1', u'key2': 'val2'}) h[u'newkey'] = u'newval' for k, v in h.iteritems(): self.assert_(isinstance(k, str)) for s in v: self.assert_(isinstance(s, str))
def test_headers(self): # Different ways of setting headers attribute url = 'http://www.scrapy.org' headers = {b'Accept': 'gzip', b'Custom-Header': 'nothing to tell you'} r = self.request_class(url=url, headers=headers) p = self.request_class(url=url, headers=r.headers) self.assertEqual(r.headers, p.headers) self.assertFalse(r.headers is headers) self.assertFalse(p.headers is r.headers) # headers must not be unicode h = Headers({'key1': u'val1', u'key2': 'val2'}) h[u'newkey'] = u'newval' for k, v in h.iteritems(): self.assert_(isinstance(k, bytes)) for s in v: self.assert_(isinstance(s, bytes))
def test_headers(self): # Different ways of setting headers attribute url = "http://www.scrapy.org" headers = {"Accept": "gzip", "Custom-Header": "nothing to tell you"} r = self.request_class(url=url, headers=headers) p = self.request_class(url=url, headers=r.headers) self.assertEqual(r.headers, p.headers) self.assertFalse(r.headers is headers) self.assertFalse(p.headers is r.headers) # headers must not be unicode h = Headers({"key1": u"val1", u"key2": "val2"}) h[u"newkey"] = u"newval" for k, v in h.iteritems(): self.assert_(isinstance(k, str)) for s in v: self.assert_(isinstance(s, str))
def test_iterables(self): idict = { 'Content-Type': 'text/html', 'X-Forwarded-For': ['ip1', 'ip2'] } h = Headers(idict) self.assertDictEqual(dict(h), { b'Content-Type': [b'text/html'], b'X-Forwarded-For': [b'ip1', b'ip2'] }) self.assertSortedEqual(h.keys(), [b'X-Forwarded-For', b'Content-Type']) self.assertSortedEqual(h.items(), [(b'X-Forwarded-For', [b'ip1', b'ip2']), (b'Content-Type', [b'text/html'])]) self.assertSortedEqual(h.iteritems(), [(b'X-Forwarded-For', [b'ip1', b'ip2']), (b'Content-Type', [b'text/html'])]) self.assertSortedEqual(h.values(), [b'ip2', b'text/html'])