def test_language_cookie_caching(self): """Regression test for caching the wrong language.""" # Run a session where the default language is English en_session = self.s = scrape.Session(verbose=1) doc = self.go('/haiti?lang=en') # sets cookie assert 'I\'m looking for someone' in doc.text doc = self.go('/haiti') assert 'I\'m looking for someone' in doc.text # Run a separate session where the default language is French fr_session = self.s = scrape.Session(verbose=1) doc = self.go('/haiti?lang=fr') # sets cookie assert 'Je recherche une personne' in doc.text doc = self.go('/haiti') assert 'Je recherche une personne' in doc.text # Check that this didn't screw up the language for the other session self.s = en_session doc = self.go('/haiti') assert 'I\'m looking for someone' in doc.text
def download_content(url): s = scrape.Session() s.go(url) d = s.doc file = open('url_content', 'w') file.write(unicode(d.text).encode('ascii', 'ignore')) file.close() print 'Download finished'
def __init__(self, conf): super(SearchRecordsLoadTest, self).__init__('search_record', conf) assert self.conf['repo'] in model.Repo.list(), ( 'Repository "%s" doesn\'t exist.' % self.conf['repo']) scraper = scrape.Session(verbose=1) self.search_page = scraper.go( '%s/%s/query?role=seek' % (self.conf['base_url'], self.conf['repo']))
def execute_one(self, query): status = None try: logging.info('Search record: %s', query) scraper = scrape.Session(verbose=1) scraper.go(self.search_page) scraper.submit(scraper.doc.cssselect_one('form'), query=query) status = scraper.status finally: self.data['http_statuses'].append(status)
def __init__(self, conf): super(CreateRecordsLoadTest, self).__init__('create_record', conf) repo_exists = self.conf['repo'] in model.Repo.list() if not self.conf['test_mode'] and repo_exists: raise Exception( '"create" task must be done against a new repository, but a ' 'repository "%s" already exists. If you really want to do ' 'this, set "test_mode" to true in the config JSON.' % self.conf['repo']) if not repo_exists: self.create_repo(self.conf['repo']) scraper = scrape.Session(verbose=1) self.create_page = scraper.go( '%s/%s/create?role=provide' % (self.conf['base_url'], self.conf['repo']))
def test_language(self): """Tests logic to choose the language of the page.""" # Defaults to the first language in the language menu for the repository # if no other hint is available. self.s = scrape.Session(verbose=1) doc = self.go('/japan') assert doc.xpath_one('/html').get('lang') == 'ja' # Follows "lang" URL parameter when avaiable. Once you specify "lang" # URL parameter, it remembers the choice in a cookie. # "lang" URL parameter precedes the cookie. self.s = scrape.Session(verbose=1) doc = self.go('/haiti') assert doc.xpath_one('/html').get('lang') == 'en' doc = self.go('/haiti?lang=ja') assert doc.xpath_one('/html').get('lang') == 'ja' doc = self.go('/haiti') assert doc.xpath_one('/html').get('lang') == 'ja' doc = self.go('/haiti?lang=ko') assert doc.xpath_one('/html').get('lang') == 'ko' # Follows "Accept-Language" HTTP header when available. self.s = scrape.Session(verbose=1) doc = self.go('/haiti', accept_language='ko') assert doc.xpath_one('/html').get('lang') == 'ko' # Uses one with higher quality value (defaults to 1) in the header. self.s = scrape.Session(verbose=1) doc = self.go('/haiti', accept_language='ko,ja;q=0.9') assert doc.xpath_one('/html').get('lang') == 'ko' # Falls back to lower quality languages when the language is not # supported. self.s = scrape.Session(verbose=1) doc = self.go('/haiti', accept_language='xx,ja;q=0.9,ko;q=0.8') assert doc.xpath_one('/html').get('lang') == 'ja' # "lang" URL parameter precedes "Accept-Language" HTTP header. self.s = scrape.Session(verbose=1) doc = self.go('/haiti?lang=ja', accept_language='ko') assert doc.xpath_one('/html').get('lang') == 'ja' # The cookie precedes "Accept-Language" HTTP header. self.s = scrape.Session(verbose=1) doc = self.go('/haiti?lang=ja') assert doc.xpath_one('/html').get('lang') == 'ja' doc = self.go('/haiti', accept_language='ko') assert doc.xpath_one('/html').get('lang') == 'ja'
def set_utcnow_for_test(self, new_utcnow, flush=''): """Sets the utils.get_utcnow() clock locally and on the server, and optionally also flushes caches on the server. Args: new_utcnow: A datetime, timestamp, or None to revert to real time. flush: Names of caches to flush (see main.flush_caches). """ if new_utcnow is None: param = 'real' elif isinstance(new_utcnow, (int, float)): param = str(new_utcnow) else: param = calendar.timegm(new_utcnow.utctimetuple()) path = '/?utcnow=%s&flush=%s' % (param, flush) # Requesting '/' gives a fast redirect; to save time, don't follow it. scrape.Session(verbose=0).go(self.path_to_url(path), redirects=0) utils.set_utcnow_for_test(new_utcnow)
def execute_one(self, name): status = None try: logging.info('Create record: %s', name) # Creates a new scrape.Session instance here because scrape.Session # is not thread-safe. Note that this scraper.go() doesn't cause # extra HTTP request. scraper = scrape.Session(verbose=1) scraper.go(self.create_page) (given_name, family_name) = name.split(' ') scraper.submit(scraper.doc.cssselect_one('form'), given_name=given_name, family_name=family_name, author_name='load_test.py', text='This is a record created by load_test.py.') status = scraper.status finally: self.data['http_statuses'].append(status)
def setUp(self): """Sets up a scrape Session for each test.""" # See http://zesty.ca/scrape for documentation on scrape. self.s = scrape.Session(verbose=1) self.set_utcnow_for_test(ServerTestsBase.TEST_TIMESTAMP, flush='*') config.set(xsrf_token_key='abc123')
def setUp(self): """Sets up a scrape Session for each test.""" self.s = scrape.Session(verbose=1)