view tests/test-annotate.py @ 37048:fc5e261915b9

wireproto: require POST for all HTTPv2 requests Wire protocol version 1 transfers argument data via request headers by default. This has historically caused problems because servers institute limits on the length of individual HTTP headers as well as the total size of all request headers. Mercurial servers can advertise the maximum length of an individual header. But there's no guarantee any intermediate HTTP agents will accept headers up to that length. In the existing wire protocol, server operators typically also key off the HTTP request method to implement authentication. For example, GET requests translate to read-only requests and can be allowed. But read-write commands must use POST and require authentication. This has typically worked because the only wire protocol commands that use POST modify the repo (e.g. the "unbundle" command). There is an experimental feature to enable clients to transmit argument data via POST request bodies. This is technically a better and more robust solution. But we can't enable it by default because of servers assuming POST means write access. In version 2 of the wire protocol, the permissions of a request are encoded in the URL. And with it being a new protocol in a new URL space, we're not constrained by backwards compatibility requirements. This commit adopts the technically superior mechanism of using HTTP request bodies to send argument data by requiring POST for all commands. Strictly speaking, it may be possible to send request bodies on GET requests. But my experience is that not all HTTP stacks support this. POST pretty much always works. Using POST for read-only operations does sacrifice some RESTful design purity. But this API cares about practicality, not about being in Roy T. Fielding's REST ivory tower. There's a chance we may relax this restriction in the future. But for now, I want to see how far we can get with a POST only API. Differential Revision: https://phab.mercurial-scm.org/D2837
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 13 Mar 2018 11:57:43 -0700
parents ec46b0ee2e3c
children 434e520adb8c
line wrap: on
line source

from __future__ import absolute_import
from __future__ import print_function

import unittest

from mercurial import (
    mdiff,
)
from mercurial.dagop import (
    annotateline,
    _annotatepair,
)

class AnnotateTests(unittest.TestCase):
    """Unit tests for annotate code."""

    def testannotatepair(self):
        self.maxDiff = None # camelcase-required

        oldfctx = b'old'
        p1fctx, p2fctx, childfctx = b'p1', b'p2', b'c'
        olddata = b'a\nb\n'
        p1data = b'a\nb\nc\n'
        p2data = b'a\nc\nd\n'
        childdata = b'a\nb2\nc\nc2\nd\n'
        diffopts = mdiff.diffopts()

        def decorate(text, fctx):
            return ([annotateline(fctx=fctx, lineno=i)
                     for i in range(1, text.count(b'\n') + 1)],
                    text)

        # Basic usage

        oldann = decorate(olddata, oldfctx)
        p1ann = decorate(p1data, p1fctx)
        p1ann = _annotatepair([oldann], p1fctx, p1ann, False, diffopts)
        self.assertEqual(p1ann[0], [
            annotateline(b'old', 1),
            annotateline(b'old', 2),
            annotateline(b'p1', 3),
        ])

        p2ann = decorate(p2data, p2fctx)
        p2ann = _annotatepair([oldann], p2fctx, p2ann, False, diffopts)
        self.assertEqual(p2ann[0], [
            annotateline(b'old', 1),
            annotateline(b'p2', 2),
            annotateline(b'p2', 3),
        ])

        # Test with multiple parents (note the difference caused by ordering)

        childann = decorate(childdata, childfctx)
        childann = _annotatepair([p1ann, p2ann], childfctx, childann, False,
                                 diffopts)
        self.assertEqual(childann[0], [
            annotateline(b'old', 1),
            annotateline(b'c', 2),
            annotateline(b'p2', 2),
            annotateline(b'c', 4),
            annotateline(b'p2', 3),
        ])

        childann = decorate(childdata, childfctx)
        childann = _annotatepair([p2ann, p1ann], childfctx, childann, False,
                                 diffopts)
        self.assertEqual(childann[0], [
            annotateline(b'old', 1),
            annotateline(b'c', 2),
            annotateline(b'p1', 3),
            annotateline(b'c', 4),
            annotateline(b'p2', 3),
        ])

        # Test with skipchild (note the difference caused by ordering)

        childann = decorate(childdata, childfctx)
        childann = _annotatepair([p1ann, p2ann], childfctx, childann, True,
                                 diffopts)
        self.assertEqual(childann[0], [
            annotateline(b'old', 1),
            annotateline(b'old', 2, True),
            # note that this line was carried over from earlier so it is *not*
            # marked skipped
            annotateline(b'p2', 2),
            annotateline(b'p2', 2, True),
            annotateline(b'p2', 3),
        ])

        childann = decorate(childdata, childfctx)
        childann = _annotatepair([p2ann, p1ann], childfctx, childann, True,
                                 diffopts)
        self.assertEqual(childann[0], [
            annotateline(b'old', 1),
            annotateline(b'old', 2, True),
            annotateline(b'p1', 3),
            annotateline(b'p1', 3, True),
            annotateline(b'p2', 3),
        ])

if __name__ == '__main__':
    import silenttestrunner
    silenttestrunner.main(__name__)