morethantext-web/tests/step_defs/page.py

34 lines
840 B
Python
Raw Normal View History

2022-06-03 11:52:18 -04:00
"""Fisture class for page functions."""
2022-06-23 14:09:03 -04:00
from http.cookiejar import CookieJar
2022-06-03 11:52:18 -04:00
from urllib import request
from urllib.error import HTTPError
2022-06-22 12:03:17 -04:00
2022-06-03 11:52:18 -04:00
class Page:
"""Class for getting an individual page."""
def __init__(self):
"""Init function."""
self.res = None
2022-06-23 14:09:03 -04:00
self.cjar = CookieJar()
self.opener = request.build_opener(request.HTTPCookieProcessor(self.cjar))
2022-06-03 11:52:18 -04:00
def request_url(self, url):
"""Make a page request."""
try:
2022-06-23 14:09:03 -04:00
page = self.opener.open(url)
2022-06-03 11:52:18 -04:00
except HTTPError as err:
self.res = err
2022-06-22 12:03:17 -04:00
else:
with page:
self.res = page
2022-06-03 11:52:18 -04:00
def get_status_code(self):
"""Return the status code."""
return self.res.status
2022-06-22 12:03:17 -04:00
2022-06-23 14:09:03 -04:00
def get_cookies(self):
2022-06-22 12:03:17 -04:00
"""Did a session id get set."""
2022-06-23 14:09:03 -04:00
return self.cjar