Fix trailing slashes hook

This commit is contained in:
c-bata
2020-10-27 04:43:22 +09:00
parent f4b53b50b7
commit 26dc9d7411
2 changed files with 18 additions and 3 deletions
+3 -3
View File
@@ -7,7 +7,7 @@ import threading
import traceback
from typing import Union, Dict, List, Optional, TypeVar, Callable, Any, cast
from bottle import Bottle, Response, redirect, request, response, static_file, hook
from bottle import Bottle, BaseResponse, redirect, request, response, static_file
from optuna.exceptions import DuplicatedStudyError
from optuna.storages import BaseStorage, get_storage
from optuna.trial import FrozenTrial
@@ -15,7 +15,7 @@ from optuna.study import StudyDirection, StudySummary
from . import serializer
BottleViewReturn = Union[str, bytes, Dict[str, Any], Response]
BottleViewReturn = Union[str, bytes, Dict[str, Any], BaseResponse]
BottleView = TypeVar("BottleView", bound=Callable[..., BottleViewReturn])
logger = logging.getLogger(__name__)
@@ -98,7 +98,7 @@ def create_app(storage_or_url: Union[str, BaseStorage]) -> Bottle:
app = Bottle()
storage = get_storage(storage_or_url)
@hook("before_request")
@app.hook("before_request")
def remove_trailing_slashes_hook() -> None:
request.environ["PATH_INFO"] = request.environ["PATH_INFO"].rstrip("/")
+15
View File
@@ -21,6 +21,21 @@ def create_json_api_wsgi_env(
class APITestCase(TestCase):
def test_get_study_summaries(self) -> None:
storage = optuna.storages.InMemoryStorage()
storage.create_new_study("foo1")
storage.create_new_study("foo2")
app = create_app(storage)
env = create_json_api_wsgi_env(
"/api/studies/",
"GET",
)
status, _, body = send_request(app, env)
self.assertEqual(status, "200 OK")
study_summaries = json.loads(body)["study_summaries"]
self.assertEqual(len(study_summaries), 2)
def test_create_study(self) -> None:
storage = optuna.storages.InMemoryStorage()
self.assertEqual(len(storage.get_all_study_summaries()), 0)