aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/addons/test_setheaders.py
blob: 0091fc967e990233baefe90d6c5dc6d84653bab4 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pytest

from mitmproxy.test import tflow
from mitmproxy.test import taddons

from mitmproxy.addons import setheaders


class TestSetHeaders:
    def test_parse_setheaders(self):
        x = setheaders.parse_setheader("/foo/bar/voing")
        assert x == ("foo", "bar", "voing")
        x = setheaders.parse_setheader("/foo/bar/vo/ing/")
        assert x == ("foo", "bar", "vo/ing/")
        x = setheaders.parse_setheader("/bar/voing")
        assert x == (".*", "bar", "voing")
        with pytest.raises("invalid replacement"):
            setheaders.parse_setheader("/")

    def test_configure(self):
        sh = setheaders.SetHeaders()
        with taddons.context() as tctx:
            with pytest.raises("invalid setheader filter pattern"):
                tctx.configure(sh, setheaders = [("~b", "one", "two")])
            tctx.configure(sh, setheaders = ["/foo/bar/voing"])

    def test_setheaders(self):
        sh = setheaders.SetHeaders()
        with taddons.context() as tctx:
            tctx.configure(
                sh,
                setheaders = [
                    ("~q", "one", "two"),
                    ("~s", "one", "three")
                ]
            )
            f = tflow.tflow()
            f.request.headers["one"] = "xxx"
            sh.request(f)
            assert f.request.headers["one"] == "two"

            f = tflow.tflow(resp=True)
            f.response.headers["one"] = "xxx"
            sh.response(f)
            assert f.response.headers["one"] == "three"

            tctx.configure(
                sh,
                setheaders = [
                    ("~s", "one", "two"),
                    ("~s", "one", "three")
                ]
            )
            f = tflow.tflow(resp=True)
            f.request.headers["one"] = "xxx"
            f.response.headers["one"] = "xxx"
            sh.response(f)
            assert f.response.headers.get_all("one") == ["two", "three"]

            tctx.configure(
                sh,
                setheaders = [
                    ("~q", "one", "two"),
                    ("~q", "one", "three")
                ]
            )
            f = tflow.tflow()
            f.request.headers["one"] = "xxx"
            sh.request(f)
            assert f.request.headers.get_all("one") == ["two", "three"]