示例#1
0
def test_assemble_request_headers():
    # https://github.com/mitmproxy/mitmproxy/issues/186
    r = treq(body=b"")
    r.headers[b"Transfer-Encoding"] = b"chunked"
    c = _assemble_request_headers(r)
    assert b"Transfer-Encoding" in c

    assert b"Host" in _assemble_request_headers(treq(headers=Headers()))
示例#2
0
    def test_equal(self):
        a = tutils.treq(timestamp_start=42, timestamp_end=43)
        b = tutils.treq(timestamp_start=42, timestamp_end=43)
        assert a == b

        assert not a == 'foo'
        assert not b == 'foo'
        assert not 'foo' == a
        assert not 'foo' == b
示例#3
0
    def test_equal(self):
        a = tutils.treq()
        b = tutils.treq()
        assert a == b

        assert not a == 'foo'
        assert not b == 'foo'
        assert not 'foo' == a
        assert not 'foo' == b
示例#4
0
    def test_equal(self):
        a = tutils.treq()
        b = tutils.treq()
        assert a == b

        assert not a == 'foo'
        assert not b == 'foo'
        assert not 'foo' == a
        assert not 'foo' == b
示例#5
0
def test_assemble_request():
    assert assemble_request(treq()) == (b"GET /path HTTP/1.1\r\n"
                                        b"header: qvalue\r\n"
                                        b"content-length: 7\r\n"
                                        b"host: address:22\r\n"
                                        b"\r\n"
                                        b"content")

    with raises(HttpException):
        assemble_request(treq(content=None))
示例#6
0
    def test_get_form_with_url_encoded(self, mock_method_urlencoded, mock_method_multipart):
        req = tutils.treq()
        assert req.get_form() == ODict()

        req = tutils.treq()
        req.body = "foobar"
        req.headers["Content-Type"] = HDR_FORM_URLENCODED
        req.get_form()
        assert req.get_form_urlencoded.called
        assert not req.get_form_multipart.called
示例#7
0
    def test_get_form_with_url_encoded(self, mock_method_urlencoded, mock_method_multipart):
        req = tutils.treq()
        assert req.get_form() == odict.ODict()

        req = tutils.treq()
        req.body = "foobar"
        req.headers["Content-Type"] = [semantics.HDR_FORM_URLENCODED]
        req.get_form()
        assert req.get_form_urlencoded.called
        assert not req.get_form_multipart.called
示例#8
0
def test_assemble_request():
    c = assemble_request(treq()) == (b"GET /path HTTP/1.1\r\n"
                                     b"header: qvalue\r\n"
                                     b"Host: address:22\r\n"
                                     b"Content-Length: 7\r\n"
                                     b"\r\n"
                                     b"content")

    with raises(HttpException):
        assemble_request(treq(content=CONTENT_MISSING))
示例#9
0
def test_expected_http_body_size():
    # Expect: 100-continue
    assert expected_http_body_size(
        treq(headers=Headers(expect="100-continue", content_length="42"))) == 0

    # http://tools.ietf.org/html/rfc7230#section-3.3
    assert expected_http_body_size(
        treq(method=b"HEAD"), tresp(headers=Headers(content_length="42"))) == 0
    assert expected_http_body_size(treq(method=b"CONNECT"), tresp()) == 0
    for code in (100, 204, 304):
        assert expected_http_body_size(treq(), tresp(status_code=code)) == 0

    # chunked
    assert expected_http_body_size(
        treq(headers=Headers(transfer_encoding="chunked")), ) is None

    # explicit length
    for val in (b"foo", b"-7"):
        with raises(HttpSyntaxException):
            expected_http_body_size(treq(headers=Headers(content_length=val)))
    assert expected_http_body_size(
        treq(headers=Headers(content_length="42"))) == 42

    # no length
    assert expected_http_body_size(treq(headers=Headers())) == 0
    assert expected_http_body_size(treq(headers=Headers()),
                                   tresp(headers=Headers())) == -1
示例#10
0
def test_assemble_request_line():
    assert _assemble_request_line(treq().data) == b"GET /path HTTP/1.1"

    authority_request = treq(method=b"CONNECT", first_line_format="authority").data
    assert _assemble_request_line(authority_request) == b"CONNECT address:22 HTTP/1.1"

    absolute_request = treq(first_line_format="absolute").data
    assert _assemble_request_line(absolute_request) == b"GET http://address:22/path HTTP/1.1"

    with raises(RuntimeError):
        _assemble_request_line(treq(first_line_format="invalid_form").data)
示例#11
0
def test_assemble_request_line():
    assert _assemble_request_line(treq().data) == b"GET /path HTTP/1.1"

    authority_request = treq(method=b"CONNECT", first_line_format="authority").data
    assert _assemble_request_line(authority_request) == b"CONNECT address:22 HTTP/1.1"

    absolute_request = treq(first_line_format="absolute").data
    assert _assemble_request_line(absolute_request) == b"GET http://address:22/path HTTP/1.1"

    with raises(RuntimeError):
        _assemble_request_line(treq(first_line_format="invalid_form").data)
示例#12
0
def test_assemble_request():
    assert assemble_request(treq()) == (
        b"GET /path HTTP/1.1\r\n"
        b"header: qvalue\r\n"
        b"content-length: 7\r\n"
        b"host: address:22\r\n"
        b"\r\n"
        b"content"
    )

    with raises(HttpException):
        assemble_request(treq(content=None))
示例#13
0
def test_assemble_request():
    c = assemble_request(treq()) == (
        b"GET /path HTTP/1.1\r\n"
        b"header: qvalue\r\n"
        b"Host: address:22\r\n"
        b"Content-Length: 7\r\n"
        b"\r\n"
        b"content"
    )

    with raises(HttpException):
        assemble_request(treq(content=CONTENT_MISSING))
示例#14
0
 def test_set_cookies(self):
     r = tutils.treq()
     r.headers = Headers(cookie="cookiename=cookievalue")
     result = r.get_cookies()
     result["cookiename"] = ["foo"]
     r.set_cookies(result)
     assert r.get_cookies()["cookiename"] == ["foo"]
示例#15
0
    def test_host(self):
        request = treq()
        assert request.host == request.data.host.decode("idna")

        # Test IDNA encoding
        # Set str, get raw bytes
        request.host = "ídna.example"
        assert request.data.host == b"xn--dna-qma.example"
        # Set raw bytes, get decoded
        request.data.host = b"xn--idn-gla.example"
        assert request.host == "idná.example"
        # Set bytes, get raw bytes
        request.host = b"xn--dn-qia9b.example"
        assert request.data.host == b"xn--dn-qia9b.example"
        # IDNA encoding is not bijective
        request.host = "fußball"
        assert request.host == "fussball"

        # Don't fail on garbage
        request.data.host = b"foo\xFF\x00bar"
        assert request.host.startswith("foo")
        assert request.host.endswith("bar")
        # foo.bar = foo.bar should not cause any side effects.
        d = request.host
        request.host = d
        assert request.data.host == b"foo\xFF\x00bar"
示例#16
0
def test_read_response():
    req = treq()
    rfile = BytesIO(b"HTTP/1.1 418 I'm a teapot\r\n\r\nbody")
    r = read_response(rfile, req)
    assert r.status_code == 418
    assert r.content == b"body"
    assert r.timestamp_end
示例#17
0
 def test_anticache(self):
     req = tutils.treq()
     req.headers.add("If-Modified-Since", "foo")
     req.headers.add("If-None-Match", "bar")
     req.anticache()
     assert "If-Modified-Since" not in req.headers
     assert "If-None-Match" not in req.headers
示例#18
0
    def test_get_form_urlencoded(self):
        req = tutils.treq("foobar")
        assert req.get_form_urlencoded() == odict.ODict()

        req.headers["Content-Type"] = semantics.HDR_FORM_URLENCODED
        assert req.get_form_urlencoded() == odict.ODict(
            utils.urldecode(req.body))
示例#19
0
def test_read_response():
    req = treq()
    rfile = BytesIO(b"HTTP/1.1 418 I'm a teapot\r\n\r\nbody")
    r = read_response(rfile, req)
    assert r.status_code == 418
    assert r.content == b"body"
    assert r.timestamp_end
示例#20
0
 def test_get_cookies_double(self):
     r = tutils.treq()
     r.headers = http.Headers(cookie="cookiename=cookievalue;othercookiename=othercookievalue")
     result = r.get_cookies()
     assert len(result) == 2
     assert result['cookiename'] == ['cookievalue']
     assert result['othercookiename'] == ['othercookievalue']
示例#21
0
    def test_legacy_first_line(self):
        req = tutils.treq()

        assert req.legacy_first_line('relative') == "GET /path HTTP/1.1"
        assert req.legacy_first_line('authority') == "GET address:22 HTTP/1.1"
        assert req.legacy_first_line('absolute') == "GET http://address:22/path HTTP/1.1"
        tutils.raises(http.HttpError, req.legacy_first_line, 'foobar')
示例#22
0
 def test_set_cookies(self):
     request = treq()
     request.headers = Headers(cookie="cookiename=cookievalue")
     result = request.cookies
     result["cookiename"] = ["foo"]
     request.cookies = result
     assert request.cookies["cookiename"] == ["foo"]
示例#23
0
    def test_legacy_first_line(self):
        req = tutils.treq()

        assert req.legacy_first_line('relative') == "GET /path HTTP/1.1"
        assert req.legacy_first_line('authority') == "GET address:22 HTTP/1.1"
        assert req.legacy_first_line('absolute') == "GET http://address:22/path HTTP/1.1"
        tutils.raises(http.HttpError, req.legacy_first_line, 'foobar')
示例#24
0
    def test_host(self):
        if six.PY2:
            from unittest import SkipTest

            raise SkipTest()

        request = treq()
        assert request.host == request.data.host.decode("idna")

        # Test IDNA encoding
        # Set str, get raw bytes
        request.host = "ídna.example"
        assert request.data.host == b"xn--dna-qma.example"
        # Set raw bytes, get decoded
        request.data.host = b"xn--idn-gla.example"
        assert request.host == "idná.example"
        # Set bytes, get raw bytes
        request.host = b"xn--dn-qia9b.example"
        assert request.data.host == b"xn--dn-qia9b.example"
        # IDNA encoding is not bijective
        request.host = "fußball"
        assert request.host == "fussball"

        # Don't fail on garbage
        request.data.host = b"foo\xFF\x00bar"
        assert request.host.startswith("foo")
        assert request.host.endswith("bar")
        # foo.bar = foo.bar should not cause any side effects.
        d = request.host
        request.host = d
        assert request.data.host == b"foo\xFF\x00bar"
示例#25
0
 def test_set_cookies(self):
     r = tutils.treq()
     r.headers = http.Headers(cookie="cookiename=cookievalue")
     result = r.get_cookies()
     result["cookiename"] = ["foo"]
     r.set_cookies(result)
     assert r.get_cookies()["cookiename"] == ["foo"]
示例#26
0
 def test_get_cookies_withequalsign(self):
     request = treq()
     request.headers = Headers(cookie="cookiename=coo=kievalue;othercookiename=othercookievalue")
     result = request.cookies
     assert len(result) == 2
     assert result["cookiename"] == ["coo=kievalue"]
     assert result["othercookiename"] == ["othercookievalue"]
示例#27
0
 def test_anticache(self):
     request = treq()
     request.headers["If-Modified-Since"] = "foo"
     request.headers["If-None-Match"] = "bar"
     request.anticache()
     assert "If-Modified-Since" not in request.headers
     assert "If-None-Match" not in request.headers
示例#28
0
 def test_path(self):
     req = treq()
     _test_decoded_attr(req, "path")
     # path can also be None.
     req.path = None
     assert req.path is None
     assert req.data.path is None
示例#29
0
 def test_anticache(self):
     req = tutils.treq()
     req.headers["If-Modified-Since"] = "foo"
     req.headers["If-None-Match"] = "bar"
     req.anticache()
     assert "If-Modified-Since" not in req.headers
     assert "If-None-Match" not in req.headers
示例#30
0
 def test_get_cookies_withequalsign(self):
     request = treq()
     request.headers = Headers(cookie="cookiename=coo=kievalue;othercookiename=othercookievalue")
     result = request.cookies
     assert len(result) == 2
     assert result['cookiename'] == 'coo=kievalue'
     assert result['othercookiename'] == 'othercookievalue'
示例#31
0
    def test_har_extractor(self):
        if sys.version_info >= (3, 0):
            with tutils.raises("does not work on Python 3"):
                tscript("har_extractor.py")
            return

        with tutils.raises(ScriptError):
            tscript("har_extractor.py")

        with tutils.tmpdir() as tdir:
            times = dict(
                timestamp_start=746203272,
                timestamp_end=746203272,
            )

            path = os.path.join(tdir, "file")
            m, sc = tscript("har_extractor.py", six.moves.shlex_quote(path))
            f = tutils.tflow(
                req=netutils.treq(**times),
                resp=netutils.tresp(**times)
            )
            self.invoke(m, "response", f)
            m.addons.remove(sc)

            with open(path, "rb") as f:
                test_data = json.load(f)
            assert len(test_data["log"]["pages"]) == 1
示例#32
0
 def test_get_cookies_double(self):
     request = treq()
     request.headers = Headers(cookie="cookiename=cookievalue;othercookiename=othercookievalue")
     result = request.cookies
     assert len(result) == 2
     assert result['cookiename'] == 'cookievalue'
     assert result['othercookiename'] == 'othercookievalue'
示例#33
0
    def test_get_multipart_form(self):
        request = treq(content="foobar")
        assert request.multipart_form is None

        request.headers["Content-Type"] = "multipart/form-data"
        assert request.multipart_form == ODict(
            utils.multipartdecode(request.headers, request.content))
示例#34
0
    def test_get_urlencoded_form(self):
        request = treq(content="foobar")
        assert request.urlencoded_form is None

        request.headers["Content-Type"] = "application/x-www-form-urlencoded"
        assert request.urlencoded_form == ODict(
            utils.urldecode(request.content))
示例#35
0
    def test_har_extractor(self):
        if sys.version_info >= (3, 0):
            with tutils.raises("does not work on Python 3"):
                tscript("har_extractor.py")
            return

        with tutils.raises(ScriptError):
            tscript("har_extractor.py")

        with tutils.tmpdir() as tdir:
            times = dict(
                timestamp_start=746203272,
                timestamp_end=746203272,
            )

            path = os.path.join(tdir, "file")
            m, sc = tscript("har_extractor.py", six.moves.shlex_quote(path))
            f = tutils.tflow(req=netutils.treq(**times),
                             resp=netutils.tresp(**times))
            self.invoke(m, "response", f)
            m.addons.remove(sc)

            with open(path, "rb") as f:
                test_data = json.load(f)
            assert len(test_data["log"]["pages"]) == 1
示例#36
0
def test_har_extractor(log):
    if sys.version_info >= (3, 0):
        with tutils.raises("does not work on Python 3"):
            with example("har_extractor.py -"):
                pass
        return

    with tutils.raises(script.ScriptException):
        with example("har_extractor.py"):
            pass

    times = dict(
        timestamp_start=746203272,
        timestamp_end=746203272,
    )

    flow = tutils.tflow(
        req=netutils.treq(**times),
        resp=netutils.tresp(**times)
    )

    with example("har_extractor.py -") as ex:
        ex.run("response", flow)

        with open(tutils.test_data.path("data/har_extractor.har")) as fp:
            test_data = json.load(fp)
            assert json.loads(ex.ns["context"].HARLog.json()) == test_data["test_response"]
示例#37
0
 def test_get_form_with_multipart(self, mock_method_urlencoded, mock_method_multipart):
     req = tutils.treq()
     req.body = "foobar"
     req.headers["Content-Type"] = HDR_FORM_MULTIPART
     req.get_form()
     assert not req.get_form_urlencoded.called
     assert req.get_form_multipart.called
示例#38
0
    def test_get_form_multipart(self):
        req = tutils.treq("foobar")
        assert req.get_form_multipart() == odict.ODict()

        req.headers["Content-Type"] = semantics.HDR_FORM_MULTIPART
        assert req.get_form_multipart() == odict.ODict(
            utils.multipartdecode(req.headers, req.body))
示例#39
0
 def test_set_cookies(self):
     request = treq()
     request.headers = Headers(cookie="cookiename=cookievalue")
     result = request.cookies
     result["cookiename"] = ["foo"]
     request.cookies = result
     assert request.cookies["cookiename"] == ["foo"]
示例#40
0
def test_read_chunked():
    req = treq(content=None)
    req.headers["Transfer-Encoding"] = "chunked"

    data = b"1\r\na\r\n0\r\n"
    with raises(HttpSyntaxException):
        b"".join(_read_chunked(BytesIO(data)))

    data = b"1\r\na\r\n0\r\n\r\n"
    assert b"".join(_read_chunked(BytesIO(data))) == b"a"

    data = b"\r\n\r\n1\r\na\r\n1\r\nb\r\n0\r\n\r\n"
    assert b"".join(_read_chunked(BytesIO(data))) == b"ab"

    data = b"\r\n"
    with raises("closed prematurely"):
        b"".join(_read_chunked(BytesIO(data)))

    data = b"1\r\nfoo"
    with raises("malformed chunked body"):
        b"".join(_read_chunked(BytesIO(data)))

    data = b"foo\r\nfoo"
    with raises(HttpSyntaxException):
        b"".join(_read_chunked(BytesIO(data)))

    data = b"5\r\naaaaa\r\n0\r\n\r\n"
    with raises("too large"):
        b"".join(_read_chunked(BytesIO(data), limit=2))
示例#41
0
 def test_path(self):
     req = treq()
     _test_decoded_attr(req, "path")
     # path can also be None.
     req.path = None
     assert req.path is None
     assert req.data.path is None
示例#42
0
def test_read_chunked():
    req = treq(content=None)
    req.headers["Transfer-Encoding"] = "chunked"

    data = b"1\r\na\r\n0\r\n"
    with raises(HttpSyntaxException):
        b"".join(_read_chunked(BytesIO(data)))

    data = b"1\r\na\r\n0\r\n\r\n"
    assert b"".join(_read_chunked(BytesIO(data))) == b"a"

    data = b"\r\n\r\n1\r\na\r\n1\r\nb\r\n0\r\n\r\n"
    assert b"".join(_read_chunked(BytesIO(data))) == b"ab"

    data = b"\r\n"
    with raises("closed prematurely"):
        b"".join(_read_chunked(BytesIO(data)))

    data = b"1\r\nfoo"
    with raises("malformed chunked body"):
        b"".join(_read_chunked(BytesIO(data)))

    data = b"foo\r\nfoo"
    with raises(HttpSyntaxException):
        b"".join(_read_chunked(BytesIO(data)))

    data = b"5\r\naaaaa\r\n0\r\n\r\n"
    with raises("too large"):
        b"".join(_read_chunked(BytesIO(data), limit=2))
示例#43
0
 def test_get_form_with_multipart(self, mock_method_urlencoded, mock_method_multipart):
     req = tutils.treq()
     req.body = "foobar"
     req.headers["Content-Type"] = [semantics.HDR_FORM_MULTIPART]
     req.get_form()
     assert not req.get_form_urlencoded.called
     assert req.get_form_multipart.called
示例#44
0
 def test_get_cookies_withequalsign(self):
     r = tutils.treq()
     r.headers = Headers(cookie="cookiename=coo=kievalue;othercookiename=othercookievalue")
     result = r.get_cookies()
     assert len(result) == 2
     assert result['cookiename'] == ['coo=kievalue']
     assert result['othercookiename'] == ['othercookievalue']
示例#45
0
    def test_get_form_for_multipart(self):
        r = tutils.treq()
        r.headers.add("content-type", "multipart/form-data")
        r.get_form_multipart = MagicMock()

        r.get_form()

        assert r.get_form_multipart.called
示例#46
0
 def test_get_cookies_double(self):
     request = treq()
     request.headers = Headers(
         cookie="cookiename=cookievalue;othercookiename=othercookievalue")
     result = request.cookies
     assert len(result) == 2
     assert result['cookiename'] == ['cookievalue']
     assert result['othercookiename'] == ['othercookievalue']
示例#47
0
 def test_get_cookies_single(self):
     h = odict.ODictCaseless()
     h["Cookie"] = ["cookiename=cookievalue"]
     r = tutils.treq()
     r.headers = h
     result = r.get_cookies()
     assert len(result) == 1
     assert result['cookiename'] == ['cookievalue']
示例#48
0
 def test_get_cookies_single(self):
     h = odict.ODictCaseless()
     h["Cookie"] = ["cookiename=cookievalue"]
     r = tutils.treq()
     r.headers = h
     result = r.get_cookies()
     assert len(result) == 1
     assert result["cookiename"] == ["cookievalue"]
示例#49
0
 def test_get_cookies_withequalsign(self):
     r = tutils.treq()
     r.headers = http.Headers(
         cookie="cookiename=coo=kievalue;othercookiename=othercookievalue")
     result = r.get_cookies()
     assert len(result) == 2
     assert result['cookiename'] == ['coo=kievalue']
     assert result['othercookiename'] == ['othercookievalue']
示例#50
0
    def test_get_form_for_urlencoded(self):
        r = tutils.treq()
        r.headers.add("content-type", "application/x-www-form-urlencoded")
        r.get_form_urlencoded = MagicMock()

        r.get_form()

        assert r.get_form_urlencoded.called
示例#51
0
def test_read_response(input):
    req = treq()
    rfile = BytesIO(input)
    r = read_response(rfile, req)
    assert r.http_version == "HTTP/1.1"
    assert r.status_code == 418
    assert r.reason == "I'm a teapot"
    assert r.content == b"body"
    assert r.timestamp_end
示例#52
0
def test_assemble_request_headers_host_header():
    r = treq()
    r.headers = Headers()
    c = _assemble_request_headers(r.data)
    assert b"host" in c

    r.host = None
    c = _assemble_request_headers(r.data)
    assert b"host" not in c
示例#53
0
    def replace(self):
        r = treq()
        r.path = b"foobarfoo"
        r.replace(b"foo", "bar")
        assert r.path == b"barbarbar"

        r.path = b"foobarfoo"
        r.replace(b"foo", "bar", count=1)
        assert r.path == b"barbarfoo"
示例#54
0
def test_assemble_request_headers_host_header():
    r = treq()
    r.headers = Headers()
    c = _assemble_request_headers(r.data)
    assert b"host" in c

    r.host = None
    c = _assemble_request_headers(r.data)
    assert b"host" not in c
示例#55
0
    def test_host_header_update(self):
        request = treq()
        assert "host" not in request.headers
        request.host = "example.com"
        assert "host" not in request.headers

        request.headers["Host"] = "foo"
        request.host = "example.org"
        assert request.headers["Host"] == "example.org"
示例#56
0
def test_modify_querystring():
    flow = tutils.tflow(req=netutils.treq(path=b"/search?q=term"))
    with example("modify_querystring.py") as ex:
        ex.run("request", flow)
        assert flow.request.query["mitmproxy"] == "rocks"

        flow.request.path = "/"
        ex.run("request", flow)
        assert flow.request.query["mitmproxy"] == "rocks"
示例#57
0
    def test_pretty_url(self):
        req = tutils.treq()
        req.form_out = "authority"
        assert req.pretty_url(True) == "address:22"
        assert req.pretty_url(False) == "address:22"

        req.form_out = "relative"
        assert req.pretty_url(True) == "http://address:22/path"
        assert req.pretty_url(False) == "http://address:22/path"
示例#58
0
    def flow(self, resp_content=b'message'):
        times = dict(
            timestamp_start=746203272,
            timestamp_end=746203272,
        )

        # Create a dummy flow for testing
        return tutils.tflow(req=netutils.treq(method=b'GET', **times),
                            resp=netutils.tresp(content=resp_content, **times))
示例#59
0
    def test_host_header_update(self):
        request = treq()
        assert "host" not in request.headers
        request.host = "example.com"
        assert "host" not in request.headers

        request.headers["Host"] = "foo"
        request.host = "example.org"
        assert request.headers["Host"] == "example.org"