Преглед на файлове

Redirect plain hits

master
Roderic Day преди 5 години
родител
ревизия
a0fa0e2f5d
променени са 2 файла, в които са добавени 19 реда и са изтрити 8 реда
  1. +11
    -0
      pico.py
  2. +8
    -8
      test.py

+ 11
- 0
pico.py Целия файл

@@ -3,6 +3,7 @@ import collections
import datetime
import http
import json
import uuid
from functools import partial
from pathlib import Path

@@ -33,7 +34,17 @@ class PicoProtocol(websockets.WebSocketServerProtocol):
document.read_bytes(),
)

def random_redirect(self):
new_path = str(uuid.uuid4()).split('-')[1]
return (
http.HTTPStatus.FOUND,
[('Location', new_path)],
b'',
)

async def process_request(self, path, request_headers):
if path == '/':
return self.random_redirect()
if request_headers.get('Upgrade') != 'websocket':
return self.serve_file(path)
return await super().process_request(path, request_headers)

+ 8
- 8
test.py Целия файл

@@ -80,17 +80,17 @@ def test_happy_path():


def test_name_taken():
client1 = _make_client('ws://localhost:8642/', 0.10, Script()
client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
+ {'kind': 'login', 'value': 'A'}
- {'kind': 'state', 'username': 'A'}
- {'kind': 'state', 'online': ['A']}
- {'kind': 'state', 'online': ['A', 'B']}
)
client2 = _make_client('ws://localhost:8642/', 0.11, Script()
client2 = _make_client('ws://localhost:8642/A', 0.11, Script()
+ {'kind': 'login', 'value': 'A'}
- {'kind': 'error', 'value': 'Username taken'}
)
client3 = _make_client('ws://localhost:8642/', 0.12, Script()
client3 = _make_client('ws://localhost:8642/A', 0.12, Script()
+ {'kind': 'login', 'value': 'B'}
- {'kind': 'state', 'username': 'B'}
- {'kind': 'state', 'online': ['A', 'B']}
@@ -100,7 +100,7 @@ def test_name_taken():


def test_interaction():
client1 = _make_client('ws://localhost:8642/', 0.10, Script()
client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
+ {'kind': 'login', 'value': 'Alice'}
- {'kind': 'state', 'username': 'Alice'}
- {'kind': 'state', 'online': ['Alice']}
@@ -109,7 +109,7 @@ def test_interaction():
- {'kind': 'post', 'value': 'Hey Bob!', 'source': 'Alice'}
- {'kind': 'post', 'value': 'Howdy!', 'source': 'Bob'}
)
client2 = _make_client('ws://localhost:8642/', 0.11, Script()
client2 = _make_client('ws://localhost:8642/A', 0.11, Script()
+ {'kind': 'login', 'value': 'Bob'}
- {'kind': 'state', 'username': 'Bob'}
- {'kind': 'state', 'online': ['Alice', 'Bob']}
@@ -140,7 +140,7 @@ def test_rooms():


def test_private_message():
client1 = _make_client('ws://localhost:8642/', 0.10, Script()
client1 = _make_client('ws://localhost:8642/A', 0.10, Script()
+ {'kind': 'login', 'value': 'Norman'}
- {'kind': 'state', 'username': 'Norman'}
- {'kind': 'state', 'online': ['Norman']}
@@ -150,7 +150,7 @@ def test_private_message():
- {'kind': 'post', 'value': '1', 'source': 'Norman'}
- {'kind': 'post', 'value': '3', 'source': 'Emma'}
)
client2 = _make_client('ws://localhost:8642/', 0.11, Script()
client2 = _make_client('ws://localhost:8642/A', 0.11, Script()
+ {'kind': 'login', 'value': 'Ray'}
- {'kind': 'state', 'username': 'Ray'}
- {'kind': 'state', 'online': ['Norman', 'Ray']}
@@ -160,7 +160,7 @@ def test_private_message():
- {'kind': 'post', 'value': '2', 'source': 'Ray'}
- {'kind': 'state', 'online': ['Ray', 'Emma']}
)
client3 = _make_client('ws://localhost:8642/', 0.12, Script()
client3 = _make_client('ws://localhost:8642/A', 0.12, Script()
+ {'kind': 'login', 'value': 'Emma'}
- {'kind': 'state', 'username': 'Emma'}
- {'kind': 'state', 'online': ['Norman', 'Ray', 'Emma']}

Loading…
Отказ
Запис