Added a session id.

This commit is contained in:
Jeff Baskin 2022-06-22 12:03:17 -04:00
parent df3abfa4d0
commit c8680b33d4
4 changed files with 51 additions and 2 deletions

View File

@ -1,4 +1,8 @@
use tide::{http::StatusCode, Request, Response}; use tide::{
http::StatusCode,
sessions::{MemoryStore, SessionMiddleware},
Request, Response,
};
mod settings; mod settings;
@ -16,6 +20,10 @@ async fn main() -> tide::Result<()> {
async fn app_setup() -> tide::Server<()> { async fn app_setup() -> tide::Server<()> {
let mut app = tide::new(); let mut app = tide::new();
app.at("/").get(home); app.at("/").get(home);
app.with(SessionMiddleware::new(
MemoryStore::new(),
b"361f953f-56ba-45e6-86ab-9efbf61b745d",
));
return app; return app;
} }

View File

@ -0,0 +1,8 @@
Feature: Session
Browser session requirements
Scenario: Session ID is set
Given a running server
When the home page is accessed
Then there is a session id

View File

@ -3,6 +3,7 @@
from urllib import request from urllib import request
from urllib.error import HTTPError from urllib.error import HTTPError
class Page: class Page:
"""Class for getting an individual page.""" """Class for getting an individual page."""
@ -13,10 +14,17 @@ class Page:
def request_url(self, url): def request_url(self, url):
"""Make a page request.""" """Make a page request."""
try: try:
self.res = request.urlopen(url) page = request.urlopen(url)
except HTTPError as err: except HTTPError as err:
self.res = err self.res = err
else:
with page:
self.res = page
def get_status_code(self): def get_status_code(self):
"""Return the status code.""" """Return the status code."""
return self.res.status return self.res.status
def get_header(self, header):
"""Did a session id get set."""
return self.res.headers[header]

View File

@ -0,0 +1,25 @@
"""tests for server sessions."""
from pytest_bdd import given, scenarios, then, when
scenarios("../features/session.feature")
@given("a running server")
def start_server(server):
"""Make a running server."""
server.set_safe_port()
server.start()
@when("the home page is accessed")
def access_home_page(server, page):
"""Access the home page."""
url = f"http://{server.base_url}/"
page.request_url(url)
@then("there is a session id")
def confirm_session(page):
"""Confirm session id exists."""
assert page.get_header("set-cookie")