aboutsummaryrefslogtreecommitdiffstats
path: root/tests/middleware/test_options.py
blob: 8cf48eae5b9e4c3484bbe421787d5dbf8d53c7bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from wsgiref.validate import validator

import pytest

from paste import options

from ..common_wsgi import call_app


@pytest.fixture
def app():
    @validator
    @options
    @validator
    def app(_, start_response):
        start_response("200 OK", [("Content-Type", "text/plain")])
        return [b"Hello, world!"]

    return app


@pytest.mark.parametrize("method", ["GET", "HEAD", "POST", "PUT", "DELETE"])
def test_non_options(app, method):
    environ = {"REQUEST_METHOD": method}
    response = call_app(app, environ)
    assert response.data == b"Hello, world!"
    assert response.status == "200 OK"
    assert ("Content-Type", "text/plain") in response.headers


def test_options(app):
    environ = {"REQUEST_METHOD": "OPTIONS"}
    response = call_app(app, environ)
    assert response.data == b""
    assert response.status == "204 No Content"
    allow = None
    for k, v in response.headers:
        if k != "Allow":
            continue
        allow = v.split(", ")
    assert allow is not None, "Must contain an Allow header"
    assert len(set(allow)) == len(allow), "Allow header must not contain duplicates"
    assert set(allow) == {"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"}