Changeset 096859f in mod_gnutls


Ignore:
Timestamp:
Dec 1, 2019, 10:14:18 AM (11 months ago)
Author:
Fiona Klute <fiona.klute@…>
Branches:
master, proxy-ticket
Children:
8335f8c
Parents:
a4e136a
Message:

https-test-client.py: Check if the HTTP body contains certain strings

There are a few test cases where we currently only check the end of
the returned body. Searching for certain strings within the body
instead of expecting a full match allows them to work with the
Python-based test client.

Location:
test
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • test/.gitignore

    ra4e136a r096859f  
    66authority/client/uid
    77authority/tofu.db
     8/__pycache__/
    89
    910# generated X.509 data
  • test/https-test-client.py

    ra4e136a r096859f  
    7777    def __repr__(self):
    7878        return (f'{self.__class__.__name__!s}(path={self.path!r}, '
    79                 f'method={self.method!r}), headers={self.headers!r}, '
    80                 f'expect={self.expect!r}')
     79                f'method={self.method!r}, headers={self.headers!r}, '
     80                f'expect={self.expect!r})')
     81
     82    def _check_body(self, body):
     83        """
     84        >>> r1 = TestRequest(path='/test.txt', method='GET', headers={}, expect={'status': 200, 'body': {'exactly': 'test\\n'}})
     85        >>> r1._check_body('test\\n')
     86        >>> r1._check_body('xyz\\n')
     87        Traceback (most recent call last):
     88        ...
     89        https-test-client.TestExpectationFailed: Unexpected body: 'xyz\\n' != 'test\\n'
     90        >>> r2 = TestRequest(path='/test.txt', method='GET', headers={}, expect={'status': 200, 'body': {'contains': ['tes', 'est']}})
     91        >>> r2._check_body('test\\n')
     92        >>> r2._check_body('est\\n')
     93        Traceback (most recent call last):
     94        ...
     95        https-test-client.TestExpectationFailed: Unexpected body: 'est\\n' does not contain 'tes'
     96        >>> r3 = TestRequest(path='/test.txt', method='GET', headers={}, expect={'status': 200, 'body': {'contains': 'test'}})
     97        >>> r3._check_body('test\\n')
     98        """
     99        if 'exactly' in self.expect['body'] \
     100           and body != self.expect['body']['exactly']:
     101            raise TestExpectationFailed(
     102                f'Unexpected body: {body!r} != '
     103                f'{self.expect["body"]["exactly"]!r}')
     104        if 'contains' in self.expect['body']:
     105            if type(self.expect['body']['contains']) is str:
     106                self.expect['body']['contains'] = [
     107                    self.expect['body']['contains']]
     108            for s in self.expect['body']['contains']:
     109                if not s in body:
     110                    raise TestExpectationFailed(
     111                        f'Unexpected body: {body!r} does not contain '
     112                        f'{s!r}')
    81113
    82114    def check_response(self, response, body):
     
    85117                f'Unexpected status: {response.status} != '
    86118                f'{self.expect["status"]}')
    87         if 'body' in self.expect and self.expect['body'] != body:
    88             raise TestExpectationFailed(
    89                 f'Unexpected body: {body!r} != {self.expect["body"]!r}')
     119        if 'body' in self.expect:
     120            self._check_body(body)
    90121
    91122    @classmethod
  • test/sample_test.yml

    ra4e136a r096859f  
    1414    expect:
    1515      status: 200
    16       body: |
    17         test
     16      body:
     17        exactly: |
     18          test
     19  - !request
     20    path: /test.txt
     21    expect:
     22      status: 200
     23      body:
     24        contains:
     25          - 'tes'
     26          - 'est'
Note: See TracChangeset for help on using the changeset viewer.