aboutsummaryrefslogtreecommitdiffstats
path: root/test/mitmproxy/addons/test_upstream_auth.py
blob: c7342bb5bce5610c315e2a3cf9d47c0a7b90784f (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
import base64
import pytest

from mitmproxy import exceptions
from mitmproxy.test import taddons
from mitmproxy.test import tflow
from mitmproxy.addons import upstream_auth


def test_configure():
    up = upstream_auth.UpstreamAuth()
    with taddons.context() as tctx:
        tctx.configure(up, upstream_auth="test:test")
        assert up.auth == b"Basic" + b" " + base64.b64encode(b"test:test")

        tctx.configure(up, upstream_auth="test:")
        assert up.auth == b"Basic" + b" " + base64.b64encode(b"test:")

        tctx.configure(up, upstream_auth=None)
        assert not up.auth

        with pytest.raises(exceptions.OptionsError):
            tctx.configure(up, upstream_auth="")
        with pytest.raises(exceptions.OptionsError):
            tctx.configure(up, upstream_auth=":")
        with pytest.raises(exceptions.OptionsError):
            tctx.configure(up, upstream_auth=":test")


def test_simple():
    up = upstream_auth.UpstreamAuth()
    with taddons.context() as tctx:
        tctx.configure(up, upstream_auth="foo:bar")

        f = tflow.tflow()
        f.mode = "upstream"
        up.requestheaders(f)
        assert "proxy-authorization" in f.request.headers

        f = tflow.tflow()
        up.requestheaders(f)
        assert "proxy-authorization" not in f.request.headers

        tctx.configure(up, mode="reverse")
        f = tflow.tflow()
        f.mode = "transparent"
        up.requestheaders(f)
        assert "proxy-authorization" in f.request.headers

        f = tflow.tflow()
        f.mode = "upstream"
        up.http_connect(f)
        assert "proxy-authorization" in f.request.headers