Slide 18
Slide 18 text
Ϩεϙϯεͷৄࡉͳ֬ೝ7BMJEBUFSFTQPOTFTJOEFUBJM
@app.post("/auth")
def auth(user: User, resp: Response):
req_body = user.dict()
# {"name": "alice1234", "password": "alice1234"}
if user_collection.find_one(req_body):
resp.status_code = status.HTTP_200_OK
resp.set_cookie(key="name", value=user.name)
else:
resp.status_code = status.HTTP_403_FORBIDDEN
@app.get(“/top")
def top(resp: Response, name: Optional[str] = Cookie(None)):
if name:
return {"name": name}
resp.status_code = status.HTTP_403_FORBIDDEN
return {"message": "please login via /auth"}
USERS = [
{"name": “alice”,”password”:”alice1234”},
{"name": “bob”,”password”:”bob5678”},
]
class AuthenticatedUser(HttpUser):
def on_start(self):
if len(USERS) > 0:
user = USERS.pop()
logger.info(f"popped user: {user}")
self.name = user["name"]
self.client.post(
"/auth",
json={
"name": user["name"],
"password": user["password"],
},
)
@task
def get_name(self):
with self.client.get(“/top") as resp:
if resp.json()["name"] != self.name:
logger.warning("not match")
resp.failure()
# resp.success() ͋Δ
'BTU"1*TBNQMFBQQˣ
MPDVTUGJMFˠ name, passwordΛड͚औͬͯɺ
session idΘΓʹnameͷcookieʹηοτ
Receive name and password,
set name in cookie as if session id