aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/addons/test_setheaders.py
blob: d7bdef61cd4bfd2db8050abed83b76cbd4ca2c9c (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
from mitmproxy.test import tflow
from mitmproxy.test import tutils

from .. import mastertest

from mitmproxy.addons import setheaders
from mitmproxy import options
from mitmproxy import proxy


class TestSetHeaders(mastertest.MasterTest):
    def mkmaster(self, **opts):
        o = options.Options(**opts)
        m = mastertest.RecordingMaster(o, proxy.DummyServer())
        sh = setheaders.SetHeaders()
        m.addons.add(sh)
        return m, sh

    def test_configure(self):
        sh = setheaders.SetHeaders()
        o = options.Options(
            setheaders = [("~b", "one", "two")]
        )
        tutils.raises(
            "invalid setheader filter pattern",
            sh.configure, o, o.keys()
        )

    def test_setheaders(self):
        m, sh = self.mkmaster(
            setheaders = [
                ("~q", "one", "two"),
                ("~s", "one", "three")
            ]
        )
        f = tflow.tflow()
        f.request.headers["one"] = "xxx"
        m.request(f)
        assert f.request.headers["one"] == "two"

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

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

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