Changeset 09774e2 in mod_gnutls


Ignore:
Timestamp:
Dec 19, 2019, 7:11:17 AM (3 years ago)
Author:
Fiona Klute <fiona.klute@…>
Branches:
asyncio, main, master, proxy-ticket
Children:
006f91a
Parents:
45b0a24
Message:

Optionally log HTTP responses to another stream/file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • test/mgstest/tests.py

    r45b0a24 r09774e2  
    7777                f'description={self.description!r})')
    7878
    79     def run(self, timeout=5.0, conn_log=None):
     79    def run(self, timeout=5.0, conn_log=None, response_log=None):
    8080        # note: "--logfile" option requires GnuTLS version >= 3.6.7
    8181        command = ['gnutls-cli', '--logfile=/dev/stderr']
     
    9696            for act in self.actions:
    9797                if type(act) is TestRequest:
    98                     act.run(conn)
     98                    act.run(conn, response_log)
    9999                elif type(act) is TestRaw10:
    100                     act.run(command, timeout)
     100                    act.run(command, timeout, conn_log, response_log)
    101101                else:
    102102                    raise TypeError(f'Unsupported action requested: {act!r}')
     
    139139                f'expect={self.expect!r})')
    140140
    141     def run(self, conn):
     141    def run(self, conn, response_log=None):
    142142        try:
    143143            conn.request(self.method, self.path, headers=self.headers)
     
    153153                raise err
    154154        body = resp.read().decode()
    155         print(format_response(resp, body))
     155        log_str = format_response(resp, body)
     156        print(log_str)
     157        if response_log:
     158            print(log_str, file=response_log)
    156159        self.check_response(resp, body)
    157160
     
    258261                f'headers={self.headers!r}, expect={self.expect!r})')
    259262
    260     def run(self, command, timeout=None):
     263    def run(self, command, timeout=None, conn_log=None, response_log=None):
    261264        req = f'{self.method} {self.path} HTTP/1.0\r\n'
    262265        for name, value in self.headers.items():
    263266            req = req + f'{name}: {value}\r\n'
    264267        req = req + f'\r\n'
    265         proc = subprocess.Popen(command, stdout=subprocess.PIPE,
    266                                 stdin=subprocess.PIPE, close_fds=True,
     268        proc = subprocess.Popen(command,
     269                                stdout=subprocess.PIPE,
     270                                stderr=subprocess.PIPE,
     271                                stdin=subprocess.PIPE,
     272                                close_fds=True,
    267273                                bufsize=0)
    268274        try:
    269             # Note: errs will be empty because stderr is not captured
    270275            outs, errs = proc.communicate(input=req.encode(),
    271276                                          timeout=timeout)
     
    273278            proc.kill()
    274279            outs, errs = proc.communicate()
     280
     281        if conn_log:
     282            print(errs.decode(), file=conn_log)
    275283
    276284        # first line of the received data must be the status
     
    280288        # log response for debugging
    281289        print(f'{status}\n{headers}\n\n{body}')
     290        if response_log:
     291            print(f'{status}\n{headers}\n\n{body}', file=response_log)
    282292
    283293        m = self.status_re.match(status)
     
    374384
    375385
    376 def run_test_conf(test_config, timeout=5.0, conn_log=None):
     386def run_test_conf(test_config, timeout=5.0, conn_log=None, response_log=None):
    377387    conns = None
    378388
     
    394404            print(f'Running test connection {i}.')
    395405        sys.stdout.flush()
    396         test_conn.run(timeout=timeout, conn_log=conn_log)
     406        test_conn.run(timeout=timeout, conn_log=conn_log,
     407                      response_log=response_log)
Note: See TracChangeset for help on using the changeset viewer.