Skip to content

Commit

Permalink
feat(api): respond to OPTIONS requests
Browse files Browse the repository at this point in the history
  • Loading branch information
open-dynaMIX committed Jan 4, 2021
1 parent 1163b90 commit ec855f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 15 deletions.
39 changes: 27 additions & 12 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,22 +327,37 @@ def shuffle():
assert len(status["playlist"]) == 2
assert get_order(status) == order[:2]

@staticmethod
@pytest.mark.parametrize(
"endpoint,expected",
[
("api/status", "GET,OPTIONS"),
("webui.js", "GET,OPTIONS"),
("api/play", "POST,OPTIONS"),
],
)
def test_options(mpv_instance, endpoint, expected):
resp = requests.options(f"{get_uri(endpoint)}")
assert resp.status_code == 204
assert "Allow" in resp.headers
assert resp.headers["Allow"] == expected

@staticmethod
@pytest.mark.parametrize(
"endpoint,method,expected",
[
("api/status", "head", "GET"),
("api/status", "patch", "GET"),
("api/status", "post", "GET"),
("api/status", "not_a_valid_http_method", "GET"),
("webui.js", "head", "GET"),
("webui.js", "patch", "GET"),
("webui.js", "post", "GET"),
("webui.js", "not_a_valid_http_method", "GET"),
("api/play", "head", "POST"),
("api/play", "patch", "POST"),
("api/play", "get", "POST"),
("api/play", "not_a_valid_http_method", "POST"),
("api/status", "head", "GET,OPTIONS"),
("api/status", "patch", "GET,OPTIONS"),
("api/status", "post", "GET,OPTIONS"),
("api/status", "not_a_valid_http_method", "GET,OPTIONS"),
("webui.js", "head", "GET,OPTIONS"),
("webui.js", "patch", "GET,OPTIONS"),
("webui.js", "post", "GET,OPTIONS"),
("webui.js", "not_a_valid_http_method", "GET,OPTIONS"),
("api/play", "head", "POST,OPTIONS"),
("api/play", "patch", "POST,OPTIONS"),
("api/play", "get", "POST,OPTIONS"),
("api/play", "not_a_valid_http_method", "POST,OPTIONS"),
],
)
def test_not_allowed_methods(mpv_instance, endpoint, method, expected):
Expand Down
10 changes: 7 additions & 3 deletions webui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,11 @@ local function call_endpoint(endpoint, req_method, request)
allowed = allowed .. "," .. method
end
end
return allowed
return allowed .. ",OPTIONS"
end
if endpoint[req_method] == nil then
if req_method == "OPTIONS" then
return response(204, "plain", "", {Allow = get_allowed(endpoint)})
elseif endpoint[req_method] == nil then
return response(
405,
"plain",
Expand Down Expand Up @@ -801,8 +803,10 @@ local function handle_request(request, passwd)

if request.method == "GET" then
return handle_static_get(request.path)
elseif file_exists(options.static_dir .. "/" .. request.path) and request.method == "OPTIONS" then
return response(204, "plain", "", {Allow = "GET,OPTIONS"})
elseif file_exists(options.static_dir .. "/" .. request.path) then
return response(405, "plain", "Error: Method not allowed", {Allow = "GET"})
return response(405, "plain", "Error: Method not allowed", {Allow = "GET,OPTIONS"})
end
return response(404, "plain", "Error: Requested URL /"..request.path.." not found", {})
end
Expand Down

0 comments on commit ec855f4

Please sign in to comment.