예제 #1
0
    def test_session(self):
        # Test that if Grab gets some cookies from the server
        # then it sends it back
        g = build_grab()
        g.setup(reuse_cookies=True)
        self.server.response["cookies"] = {"foo": "bar"}.items()
        g.go(self.server.get_url())
        self.assertEqual(g.response.cookies["foo"], "bar")
        g.go(self.server.get_url())
        self.assertEqual(self.server.request["headers"]["Cookie"], "foo=bar")
        g.go(self.server.get_url())
        self.assertEqual(self.server.request["headers"]["Cookie"], "foo=bar")

        # Test reuse_cookies=False
        g = build_grab()
        g.setup(reuse_cookies=False)
        self.server.response["cookies"] = {"foo": "baz"}.items()
        g.go(self.server.get_url())
        self.assertEqual(g.response.cookies["foo"], "baz")
        g.go(self.server.get_url())
        self.assertTrue(len(self.server.request["cookies"]) == 0)

        # Test something
        g = build_grab()
        g.setup(reuse_cookies=True)
        self.server.response["cookies"] = {"foo": "bar"}.items()
        g.go(self.server.get_url())
        self.assertEqual(g.response.cookies["foo"], "bar")
        g.clear_cookies()
        g.go(self.server.get_url())
        self.assertTrue(len(self.server.request["cookies"]) == 0)
예제 #2
0
    def test_load_proxylist(self):
        with temp_file() as tmp_file:
            content = "%s\n%s\n%s" % (PROXY1, PROXY2, PROXY3)
            open(tmp_file, "w").write(content)

            # By default auto_change is True
            g = build_grab()
            g.load_proxylist(tmp_file, "text_file")
            self.assertEqual(g.config["proxy_auto_change"], True)
            servers = set()
            for x in six.moves.range(10):
                g.go("http://yandex.ru")
                servers.add(g.config["proxy"])

            self.assertTrue(len(servers) > 1)

            # Disable auto_change
            # By default auto_init is True
            g = build_grab()
            g.load_proxylist(tmp_file, "text_file", auto_change=False)
            self.assertEqual(g.config["proxy_auto_change"], False)
            servers = set()
            for x in six.moves.range(10):
                g.go("http://yandex.ru")
                servers.add(g.config["proxy"])
            self.assertEqual(len(servers), 1)

            # Disable auto_change
            # Disable auto_init
            # Proxylist will not be used by default
            g = build_grab()
            g.load_proxylist(tmp_file, "text_file", auto_change=False, auto_init=False)
            self.assertEqual(g.config["proxy_auto_change"], False)
            g.go(self.server.get_url())
            self.assertEqual(g.config["proxy"], None)
    def test_body_inmemory(self):
        g = build_grab()
        g.setup(body_inmemory=False)
        self.assertRaises(GrabMisuseError, lambda: g.go(SERVER.BASE_URL))

        SERVER.RESPONSE['get'] = 'foo'
        g = build_grab()
        g.setup(body_inmemory=False)
        g.setup(body_storage_dir=TMP_DIR)
        g.go(SERVER.BASE_URL)
        self.assertTrue(os.path.exists(g.response.body_path))
        self.assertTrue(TMP_DIR in g.response.body_path)
        self.assertEqual('foo', open(g.response.body_path).read())
        old_path = g.response.body_path

        g.go(SERVER.BASE_URL)
        self.assertTrue(old_path != g.response.body_path)

        SERVER.RESPONSE['get'] = 'foo'
        g = build_grab()
        g.setup(body_inmemory=False)
        g.setup(body_storage_dir=TMP_DIR)
        g.setup(body_storage_filename='musik.mp3')
        g.go(SERVER.BASE_URL)
        self.assertTrue(os.path.exists(g.response.body_path))
        self.assertTrue(TMP_DIR in g.response.body_path)
        self.assertEqual('foo', open(g.response.body_path).read())
        self.assertEqual(os.path.join(TMP_DIR, 'musik.mp3'), g.response.body_path)
예제 #4
0
    def test_session(self):
        # Test that if Grab gets some cookies from the server
        # then it sends it back
        g = build_grab()
        g.setup(reuse_cookies=True)
        self.server.response['cookies'] = {'foo': 'bar'}.items()
        g.go(self.server.get_url())
        self.assertEqual(g.response.cookies['foo'], 'bar')
        g.go(self.server.get_url())
        self.assertEqual(self.server.request['headers']['Cookie'], 'foo=bar')
        g.go(self.server.get_url())
        self.assertEqual(self.server.request['headers']['Cookie'], 'foo=bar')

        # Test reuse_cookies=False
        g = build_grab()
        g.setup(reuse_cookies=False)
        self.server.response['cookies'] = {'foo': 'baz'}.items()
        g.go(self.server.get_url())
        self.assertEqual(g.response.cookies['foo'], 'baz')
        g.go(self.server.get_url())
        self.assertTrue(len(self.server.request['cookies']) == 0)

        # Test something
        g = build_grab()
        g.setup(reuse_cookies=True)
        self.server.response['cookies'] = {'foo': 'bar'}.items()
        g.go(self.server.get_url())
        self.assertEqual(g.response.cookies['foo'], 'bar')
        g.clear_cookies()
        g.go(self.server.get_url())
        self.assertTrue(len(self.server.request['cookies']) == 0)
예제 #5
0
    def test_unicode_post(self):
        # By default, unicode post should be converted into utf-8
        g = build_grab()
        data = u'фыва'
        g.setup(post=data, url=self.server.get_url())
        g.request()
        self.assertEqual(self.server.request['data'], data.encode('utf-8'))

        # Now try cp1251 with charset option
        self.server.request['charset'] = 'cp1251'
        g = build_grab()
        data = u'фыва'
        g.setup(post=data, url=self.server.get_url(),
                charset='cp1251', debug=True)
        g.request()
        self.assertEqual(self.server.request['data'], data.encode('cp1251'))

        # Now try dict with unicode value & charset option
        self.server.request['charset'] = 'cp1251'
        g = build_grab()
        data = u'фыва'
        g.setup(post={'foo': data}, url=self.server.get_url(),
                charset='cp1251', debug=True)
        g.request()
        test = 'foo=%s' % quote(data.encode('cp1251'))
        test = test.encode('utf-8')  # py3 hack
        self.assertEqual(self.server.request['data'], test)
예제 #6
0
파일: grab_proxy.py 프로젝트: qqalexqq/grab
    def test_load_proxylist(self):
        content = "%s\n%s\n%s" % (PROXY1, PROXY2, PROXY3)
        open(TMP_FILE, "w").write(content)

        # By default auto_change is True
        g = build_grab()
        g.load_proxylist(TMP_FILE, "text_file")
        self.assertEqual(g.config["proxy_auto_change"], True)
        servers = set()
        for x in xrange(10):
            g.go("http://yandex.ru")
            servers.add(g.config["proxy"])

        self.assertTrue(len(servers) > 1)

        # Disable auto_change
        # By default auto_init is True
        g = build_grab()
        g.load_proxylist(TMP_FILE, "text_file", auto_change=False)
        self.assertEqual(g.config["proxy_auto_change"], False)
        servers = set()
        for x in xrange(10):
            g.go("http://yandex.ru")
            servers.add(g.config["proxy"])
        self.assertEqual(len(servers), 1)

        # Disable auto_change
        # Disable auto_init
        # Proxylist will not be used by default
        g = build_grab()
        g.load_proxylist(TMP_FILE, "text_file", auto_change=False, auto_init=False)
        self.assertEqual(g.config["proxy_auto_change"], False)
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.config["proxy"], None)
예제 #7
0
    def test_load_dump(self):
        with temp_file() as tmp_file:
            g = build_grab()
            cookies = {"foo": "bar", "spam": "ham"}
            g.setup(cookies=cookies)
            g.go(self.server.get_url())
            g.dump_cookies(tmp_file)
            self.assertEqual(set(cookies.items()), set((x["name"], x["value"]) for x in json.load(open(tmp_file))))

            g = build_grab()
            cookies = {"foo": "bar", "spam": u"begemot"}
            g.setup(cookies=cookies)
            g.go(self.server.get_url())
            g.dump_cookies(tmp_file)
            self.assertEqual(set(cookies.items()), set((x["name"], x["value"]) for x in json.load(open(tmp_file))))

            # Test load cookies
            g = build_grab()
            cookies = [
                {"name": "foo", "value": "bar", "domain": self.server.address},
                {"name": "spam", "value": u"begemot", "domain": self.server.address},
            ]
            json.dump(cookies, open(tmp_file, "w"))
            g.load_cookies(tmp_file)
            self.assertEqual(set(g.cookies.items()), set((x["name"], x["value"]) for x in cookies))
    def test_body_inmemory_false(self):
        g = build_grab()
        g.setup(body_inmemory=False)
        self.assertRaises(GrabMisuseError, lambda: g.go(self.server.get_url()))

        self.server.response['get.data'] = b'foo'
        g = build_grab()
        g.setup(body_inmemory=False)
        g.setup(body_storage_dir=TMP_DIR)
        g.go(self.server.get_url())
        self.assertTrue(os.path.exists(g.response.body_path))
        self.assertTrue(TMP_DIR in g.response.body_path)
        self.assertEqual(b'foo', open(g.response.body_path, 'rb').read())
        self.assertEqual(g.response._bytes_body, None)
        old_path = g.response.body_path

        g.go(self.server.get_url())
        self.assertTrue(old_path != g.response.body_path)

        self.server.response['get.data'] = 'foo'
        g = build_grab()
        g.setup(body_inmemory=False)
        g.setup(body_storage_dir=TMP_DIR)
        g.setup(body_storage_filename='music.mp3')
        g.go(self.server.get_url())
        self.assertTrue(os.path.exists(g.response.body_path))
        self.assertTrue(TMP_DIR in g.response.body_path)
        self.assertEqual(b'foo', open(g.response.body_path, 'rb').read())
        self.assertEqual(os.path.join(TMP_DIR, 'music.mp3'),
                         g.response.body_path)
        self.assertEqual(g.response.body, b'foo')
        self.assertEqual(g.response._bytes_body, None)
예제 #9
0
    def test_session(self):
        # Test that if Grab gets some cookies from the server
        # then it sends it back
        g = build_grab()
        g.setup(reuse_cookies=True)
        SERVER.RESPONSE['cookies'] = {'foo': 'bar'}
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.response.cookies['foo'], 'bar')
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST['headers']['Cookie'], 'foo=bar')
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST['headers']['Cookie'], 'foo=bar')

        # Test reuse_cookies=False
        g = build_grab()
        g.setup(reuse_cookies=False)
        SERVER.RESPONSE['cookies'] = {'foo': 'baz'}
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.response.cookies['foo'], 'baz')
        g.go(SERVER.BASE_URL)
        self.assertTrue(len(SERVER.REQUEST['cookies']) == 0)

        # Test something
        g = build_grab()
        g.setup(reuse_cookies=True)
        SERVER.RESPONSE['cookies'] = {'foo': 'bar'}
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.response.cookies['foo'], 'bar')
        g.clear_cookies()
        g.go(SERVER.BASE_URL)
        self.assertTrue(len(SERVER.REQUEST['cookies']) == 0)
예제 #10
0
    def test_session(self):
        # Test that if Grab gets some cookies from the server
        # then it sends it back
        g = build_grab()
        g.setup(reuse_cookies=True)
        SERVER.RESPONSE["cookies"] = {"foo": "bar"}
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.response.cookies["foo"], "bar")
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST["headers"]["Cookie"], "foo=bar")
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST["headers"]["Cookie"], "foo=bar")

        # Test reuse_cookies=False
        g = build_grab()
        g.setup(reuse_cookies=False)
        SERVER.RESPONSE["cookies"] = {"foo": "baz"}
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.response.cookies["foo"], "baz")
        g.go(SERVER.BASE_URL)
        self.assertTrue(len(SERVER.REQUEST["cookies"]) == 0)

        # Test something
        g = build_grab()
        g.setup(reuse_cookies=True)
        SERVER.RESPONSE["cookies"] = {"foo": "bar"}
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.response.cookies["foo"], "bar")
        g.clear_cookies()
        g.go(SERVER.BASE_URL)
        self.assertTrue(len(SERVER.REQUEST["cookies"]) == 0)
예제 #11
0
파일: grab_proxy.py 프로젝트: abael/grab
    def test_load_proxylist(self):
        content = '%s\n%s\n%s' % (PROXY1, PROXY2, PROXY3)
        open(TMP_FILE, 'w').write(content)

        # By default auto_change is True
        g = build_grab()
        g.load_proxylist(TMP_FILE, 'text_file')
        self.assertEqual(g.config['proxy_auto_change'], True)
        servers = set()
        for x in six.moves.range(10):
            g.go('http://yandex.ru')
            servers.add(g.config['proxy'])

        self.assertTrue(len(servers) > 1)

        # Disable auto_change
        # By default auto_init is True
        g = build_grab()
        g.load_proxylist(TMP_FILE, 'text_file', auto_change=False)
        self.assertEqual(g.config['proxy_auto_change'], False)
        servers = set()
        for x in six.moves.range(10):
            g.go('http://yandex.ru')
            servers.add(g.config['proxy'])
        self.assertEqual(len(servers), 1)

        # Disable auto_change
        # Disable auto_init
        # Proxylist will not be used by default
        g = build_grab()
        g.load_proxylist(TMP_FILE, 'text_file', auto_change=False,
                         auto_init=False)
        self.assertEqual(g.config['proxy_auto_change'], False)
        g.go(self.server.get_url())
        self.assertEqual(g.config['proxy'], None)
예제 #12
0
    def test_load_dump(self):
        g = build_grab()
        cookies = {'foo': 'bar', 'spam': 'ham'}
        g.setup(cookies=cookies)
        g.go(self.server.get_url())
        g.dump_cookies(TMP_FILE)
        self.assertEqual(set(cookies.items()),
                         set((x['name'], x['value'])
                             for x in json.load(open(TMP_FILE))))

        # Test non-ascii
        g = build_grab()
        cookies = {'foo': 'bar', 'spam': u'бегемот'}
        g.setup(cookies=cookies)
        g.go(self.server.get_url())
        g.dump_cookies(TMP_FILE)
        self.assertEqual(set(cookies.items()),
                         set((x['name'], x['value'])
                             for x in json.load(open(TMP_FILE))))

        # Test load cookies
        g = build_grab()
        cookies = [{'name': 'foo', 'value': 'bar'},
                   {'name': 'spam', 'value': u'бегемот'}]
        json.dump(cookies, open(TMP_FILE, 'w'))
        g.load_cookies(TMP_FILE)
        self.assertEqual(set(g.cookies.items()),
                         set((x['name'], x['value']) for x in cookies))
예제 #13
0
파일: grab_api.py 프로젝트: qqalexqq/grab
 def test_adopt(self):
     g = build_grab()
     SERVER.RESPONSE['get'] = 'Moon'
     g.go(SERVER.BASE_URL)
     g2 = build_grab()
     self.assertEqual(g2.config['url'], None)
     g2.adopt(g)
     self.assertTrue('Moon' in g2.response.body)
     self.assertEqual(g2.config['url'], SERVER.BASE_URL)
예제 #14
0
 def test_make_url_absolute(self):
     g = build_grab()
     self.server.response['get.data'] = '<base href="http://foo/bar/">'
     g.go(self.server.get_url())
     absolute_url = g.make_url_absolute('/foobar', resolve_base=True)
     self.assertEqual(absolute_url, 'http://foo/foobar')
     g = build_grab()
     absolute_url = g.make_url_absolute('/foobar')
     self.assertEqual(absolute_url, '/foobar')
예제 #15
0
 def test_adopt(self):
     g = build_grab()
     self.server.response['get.data'] = 'Moon'
     g.go(self.server.get_url())
     g2 = build_grab()
     self.assertEqual(g2.config['url'], None)
     g2.adopt(g)
     self.assertTrue(b'Moon' in g2.response.body)
     self.assertEqual(g2.config['url'], self.server.get_url())
예제 #16
0
파일: grab_api.py 프로젝트: qqalexqq/grab
 def test_clone(self):
     g = build_grab()
     SERVER.RESPONSE['get'] = 'Moon'
     g.go(SERVER.BASE_URL)
     self.assertTrue('Moon' in g.response.body)
     g2 = build_grab()
     self.assertEqual(g2.response, None)
     g2 = g.clone()
     self.assertTrue('Moon' in g.response.body)
예제 #17
0
 def test_adopt(self):
     g = build_grab()
     self.server.response["get.data"] = "Moon"
     g.go(self.server.get_url())
     g2 = build_grab()
     self.assertEqual(g2.config["url"], None)
     g2.adopt(g)
     self.assertTrue(b"Moon" in g2.response.body)
     self.assertEqual(g2.config["url"], self.server.get_url())
    def test_body_inmemory(self):
        g = build_grab()
        g.setup(body_inmemory=False)
        self.assertRaises(GrabMisuseError, lambda: g.go(SERVER.BASE_URL))

        SERVER.RESPONSE['get'] = 'foo'
        g = build_grab()
        g.setup(body_inmemory=False)
        g.setup(body_storage_dir=TMP_DIR)
        g.go(SERVER.BASE_URL)
예제 #19
0
    def test_empty_document(self):
        self.server.response['get.data'] = 'oops'
        g = build_grab()
        g.go(self.server.get_url())
        g.xpath_exists('//anytag')

        self.server.response['get.data'] = '<frameset></frameset>'
        g = build_grab()
        g.go(self.server.get_url())
        g.xpath_exists('//anytag')
    def test_body_inmemory(self):
        g = build_grab()
        g.setup(body_inmemory=False)
        self.assertRaises(GrabMisuseError, lambda: g.go(self.server.get_url()))

        self.server.response["get.data"] = "foo"
        g = build_grab()
        g.setup(body_inmemory=False)
        g.setup(body_storage_dir=TMP_DIR)
        g.go(self.server.get_url())
예제 #21
0
파일: ext_lxml.py 프로젝트: Kuznitsin/grab
    def test_empty_document(self):
        SERVER.RESPONSE['get'] = 'oops'
        g = build_grab()
        g.go(SERVER.BASE_URL)
        g.xpath_exists('//anytag')

        SERVER.RESPONSE['get'] = '<frameset></frameset>'
        g = build_grab()
        g.go(SERVER.BASE_URL)
        g.xpath_exists('//anytag')
예제 #22
0
    def test_options_method(self):
        g = build_grab()
        g.setup(method="options", post=b"abc")
        g.go(self.server.get_url())
        self.assertEquals("OPTIONS", self.server.request["method"])
        self.assertEquals("3", self.server.request["headers"]["Content-Length"])

        g = build_grab()
        g.setup(method="options")
        g.go(self.server.get_url())
        self.assertEquals("OPTIONS", self.server.request["method"])
        self.assertTrue("Content-Length" not in self.server.request["headers"])
예제 #23
0
    def test_options_method(self):
        g = build_grab()
        g.setup(method='options', post=b'abc')
        g.go(self.server.get_url())
        self.assertEquals('OPTIONS', self.server.request['method'])
        self.assertEquals('3', self.server.request['headers']['Content-Length'])

        g = build_grab()
        g.setup(method='options')
        g.go(self.server.get_url())
        self.assertEquals('OPTIONS', self.server.request['method'])
        self.assertTrue('Content-Length' not in self.server.request['headers'])
예제 #24
0
    def test_redirect_session(self):
        g = build_grab()
        self.server.response["cookies"] = {"foo": "bar"}.items()
        g.go(self.server.get_url())
        self.assertEqual(g.response.cookies["foo"], "bar")

        # Setup one-time redirect
        g = build_grab()
        self.server.response["cookies"] = {}
        self.server.response_once["headers"] = [("Location", self.server.get_url()), ("Set-Cookie", "foo=bar")]
        self.server.response_once["code"] = 302
        g.go(self.server.get_url())
        self.assertEqual(self.server.request["cookies"]["foo"].value, "bar")
예제 #25
0
    def test_redirect_session(self):
        g = build_grab()
        SERVER.RESPONSE["cookies"] = {"foo": "bar"}
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.response.cookies["foo"], "bar")

        # Setup one-time redirect
        g = build_grab()
        SERVER.RESPONSE["cookies"] = {}
        SERVER.RESPONSE_ONCE["headers"].append(("Location", SERVER.BASE_URL))
        SERVER.RESPONSE_ONCE["headers"].append(("Set-Cookie", "foo=bar"))
        SERVER.RESPONSE_ONCE["code"] = 302
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST["cookies"]["foo"].value, "bar")
예제 #26
0
    def test_redirect_session(self):
        g = build_grab()
        SERVER.RESPONSE['cookies'] = {'foo': 'bar'}
        g.go(SERVER.BASE_URL)
        self.assertEqual(g.response.cookies['foo'], 'bar')

        # Setup one-time redirect
        g = build_grab()
        SERVER.RESPONSE['cookies'] = {}
        SERVER.RESPONSE_ONCE['headers'].append(('Location', SERVER.BASE_URL))
        SERVER.RESPONSE_ONCE['headers'].append(('Set-Cookie', 'foo=bar'))
        SERVER.RESPONSE_ONCE['code'] = 302
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST['cookies']['foo'].value, 'bar')
예제 #27
0
    def test_useragent(self):
        g = build_grab()

        # Null value activates default random user-agent
        # For some transports it just allow them to send default user-agent
        # like in Kit transport case
        g = build_grab()
        g.setup(user_agent=None)
        g.go(self.server.get_url())
        self.assertTrue(len(self.server.request['headers']) > 0)
        self.assertFalse('PycURL' in
                         self.server.request['headers']['user-agent'])

        # By default user_agent is None => random user agent is generated
        g = build_grab()
        g.go(self.server.get_url())
        self.assertTrue(len(self.server.request['headers']) > 0)
        self.assertFalse('PycURL' in
                         self.server.request['headers']['user-agent'])

        # Simple case: setup user agent manually
        g.setup(user_agent='foo')
        g.go(self.server.get_url())
        self.assertEqual(self.server.request['headers']['user-agent'], 'foo')

        # user agent from file should be loaded
        path = '/tmp/__ua.txt'
        open(path, 'w').write('GOD')
        g.setup(user_agent=None, user_agent_file=path)
        g.go(self.server.get_url())
        self.assertEqual(self.server.request['headers']['user-agent'], 'GOD')

        # random user agent from file should be loaded
        path = '/tmp/__ua.txt'
        open(path, 'w').write('GOD1\nGOD2')
        g.setup(user_agent=None, user_agent_file=path)
        g.go(self.server.get_url())
        self.assertTrue(self.server.request['headers']['user-agent']
                        in ('GOD1', 'GOD2'))
        ua = g.config['user_agent']

        # User-agent should not change
        g.go(self.server.get_url())
        self.assertEqual(self.server.request['headers']['user-agent'], ua)

        # User-agent should not change
        g.go(self.server.get_url())
        self.assertEqual(self.server.request['headers']['user-agent'], ua)
예제 #28
0
    def test_useragent_simple(self):
        g = build_grab()

        # Simple case: setup user agent manually
        g.setup(user_agent='foo')
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST['headers']['user-agent'], 'foo')
예제 #29
0
    def test_cookiefile(self):
        g = build_grab()

        # Empty file should not raise Exception
        open(TMP_FILE, "w").write("")
        g.setup(cookiefile=TMP_FILE)
        g.go(SERVER.BASE_URL)

        cookies = [{"name": "spam", "value": "ham"}]
        json.dump(cookies, open(TMP_FILE, "w"))

        # One cookie are sent in server reponse
        # Another cookies is passed via the `cookiefile` option
        SERVER.RESPONSE["cookies"] = {"godzilla": "monkey"}
        g.setup(cookiefile=TMP_FILE)
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST["cookies"]["spam"].value, "ham")

        # This is correct reslt of combining two cookies
        MERGED_COOKIES = [("godzilla", "monkey"), ("spam", "ham")]

        # g.cookies should contains merged cookies
        self.assertEqual(set(MERGED_COOKIES), set(g.cookies.items()))

        # `cookiefile` file should contains merged cookies
        self.assertEqual(set(MERGED_COOKIES), set((x["name"], x["value"]) for x in json.load(open(TMP_FILE))))

        # Just ensure it works
        g.go(SERVER.BASE_URL)
예제 #30
0
    def test_empty_useragent_pycurl(self):
        g = build_grab()

        # Empty string disable default pycurl user-agent
        g.setup(user_agent='')
        g.go(SERVER.BASE_URL)
        self.assertEqual(SERVER.REQUEST['headers'].get('user-agent', ''), '')
예제 #31
0
 def test_debug_nonascii_post(self):
     grab = build_grab(debug=True)
     grab.setup(post=u'фыва'.encode('cp1251'))
     grab.go(self.server.get_url())
예제 #32
0
 def test_get_method(self):
     grab = build_grab()
     grab.go(self.server.get_url())
     self.assertEqual('GET', self.server.request['method'])
예제 #33
0
 def test_incorrect_option_name(self):
     g = build_grab()
     self.assertRaises(GrabMisuseError, g.setup, save_the_word=True)
예제 #34
0
 def test_default_content_for_fake_response(self):
     content = b'<strong>test</strong>'
     g = build_grab(document_body=content)
     self.assertEqual(g.response.body, content)
예제 #35
0
 def test_empty_clone(self):
     g = build_grab()
     g.clone()
예제 #36
0
    def setUp(self):
        SERVER.reset()

        # Create fake grab instance with fake response
        self.g = build_grab()
        self.g.fake_response(HTML, charset='cp1251')
예제 #37
0
 def test_parsing_response_cookies(self):
     grab = build_grab()
     self.server.response['cookies'] = {'foo': 'bar', '1': '2'}.items()
     grab.go(self.server.get_url())
     self.assertEqual(grab.doc.cookies['foo'], 'bar')
예제 #38
0
 def test_get(self):
     self.server.response['get.data'] = 'Final Countdown'
     g = build_grab()
     g.go(self.server.get_url())
     self.assertTrue(b'Final Countdown' in g.response.body)
예제 #39
0
 def setUp(self):
     self.g = build_grab(document_body=XML)
예제 #40
0
 def test_parsing_response_headers(self):
     self.server.response['headers'] = [('Hello', 'Grab')]
     grab = build_grab()
     grab.go(self.server.get_url())
     self.assertTrue(grab.response.headers['Hello'] == 'Grab')
예제 #41
0
 def test_status_code(self):
     self.server.response['get.data'] = 'Simple String'
     grab = build_grab()
     grab.go(self.server.get_url())
     self.assertEqual(200, grab.response.code)
예제 #42
0
 def test_body_content(self):
     self.server.response['get.data'] = 'Simple String'
     grab = build_grab()
     grab.go(self.server.get_url())
     self.assertEqual(b'Simple String', grab.response.body)
예제 #43
0
 def test_debug_post(self):
     g = build_grab(debug_post=True)
     g.setup(post={'foo': 'bar'})
     self.server.response['post.data'] = 'x'
     g.go(self.server.get_url())
     self.assertEqual(b'x', g.doc.body)
예제 #44
0
 def test_task_clone_kwargs(self):
     g = build_grab()
     g.setup(url='http://foo.com/')
     task = Task('foo', grab=g, cache_timeout=1)
     task2 = task.clone(cache_timeout=2)
     self.assertEqual(2, task2.cache_timeout)
예제 #45
0
 def test_update_invalid_cookie(self):
     grab = build_grab()
     self.assertRaises(GrabMisuseError, grab.cookies.update, None)
     self.assertRaises(GrabMisuseError, grab.cookies.update, 'asdf')
     self.assertRaises(GrabMisuseError, grab.cookies.update, ['asdf'])
예제 #46
0
 def test_body_inmemory_true(self):
     g = build_grab()
     self.server.response['data'] = b'bar'
     g.go(self.server.get_url())
     self.assertEqual(g.response._bytes_body, b'bar')
예제 #47
0
 def func():
     g = build_grab()
     g.go(self.server.get_url())
예제 #48
0
 def test_request_headers(self):
     grab = build_grab(debug=True)
     grab.setup(headers={'Foo': 'Bar'})
     grab.go(self.server.get_url())
     self.assertEqual('Bar', grab.request_headers['foo'])
예제 #49
0
 def test_empty_adopt(self):
     g = build_grab()
     g2 = build_grab()
     g2.adopt(g)
예제 #50
0
 def test_default_random_user_agent(self):
     grab = build_grab()
     grab.go(self.server.get_url())
     self.assertTrue(self.server.request['headers'].get(
         'user-agent').startswith('Mozilla/5.0 '))
예제 #51
0
 def test_setup_document(self):
     data = b'''
     <h1>test</h1>
     '''
     g = build_grab(data)
     self.assertTrue(b'test' in g.doc.body)
예제 #52
0
 def test_put_method(self):
     g = build_grab()
     g.setup(method='put', post=b'')
     g.go(SERVER.BASE_URL)
     self.assertEquals('PUT', SERVER.REQUEST['method'])
예제 #53
0
 def test_download(self):
     with temp_file() as save_file:
         g = build_grab()
         self.server.response['get.data'] = 'FOO'
         length = g.download(self.server.get_url(), save_file)
         self.assertEqual(3, length)
예제 #54
0
 def test_put_method(self):
     grab = build_grab()
     grab.setup(method='put', post=b'abc')
     grab.go(self.server.get_url())
     self.assertEqual('PUT', self.server.request['method'])
     self.assertEqual('3', self.server.request['headers']['Content-Length'])
예제 #55
0
 def test_debug_post_big_str(self):
     grab = build_grab(debug_post=True)
     big_value = 'x' * 1000
     grab.setup(post=big_value)
     grab.go(self.server.get_url())
     self.assertEqual(self.server.request['data'], big_value.encode())
예제 #56
0
 def test_setup_with_proxyline_custom_proxy_type(self):
     g = build_grab()
     g.setup_with_proxyline('1.1.1.1:8080', proxy_type='socks')
     self.assertEqual(g.config['proxy'], '1.1.1.1:8080')
     self.assertEqual(g.config['proxy_userpwd'], None)
     self.assertEqual(g.config['proxy_type'], 'socks')
예제 #57
0
 def test_debug_post_integer_bug(self):
     grab = build_grab(debug_post=True)
     grab.setup(post={'foo': 3})
     self.server.response['post.data'] = 'x'
     grab.go(self.server.get_url())
     self.assertEqual(b'x', grab.doc.body)
예제 #58
0
 def test_debug_nonascii_multipart_post(self):
     grab = build_grab(debug=True)
     grab.setup(charset='cp1251',
                multipart_post=[('x', u'фыва'.encode('cp1251'))])
     grab.go(self.server.get_url())
예제 #59
0
 def test_setup_with_proxyline_userpwd(self):
     g = build_grab()
     g.setup_with_proxyline('1.1.1.1:8080:user:pass')
     self.assertEqual(g.config['proxy'], '1.1.1.1:8080')
     self.assertEqual(g.config['proxy_userpwd'], 'user:pass')
     self.assertEqual(g.config['proxy_type'], 'http')
예제 #60
0
 def test_delete_method(self):
     grab = build_grab()
     grab.setup(method='delete')
     grab.go(self.server.get_url())
     self.assertEqual('DELETE', self.server.request['method'])