Exemple #1
0
def test_get_reset():
    request = Request(
        scheme='http',
        method='GET',
        headers={
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent':
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36',
            'Dnt': '1',
            'Accept':
            'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'en-US,en;q=0.9,eo;q=0.8',
            'Cookie':
            'gsScrollPos-6417=0; gsScrollPos-5300=0; gsScrollPos-2934=0; gsScrollPos-3120=0; gsScrollPos-3574=0; gsScrollPos-3508=; gsScrollPos-4736=0; gsScrollPos-3123=0; gsScrollPos-3571=',
            'If-Modified-Since': 'Sun, 06 Jan 2019 03:37:46 GMT'
        },
        host='127.0.0.1:5000',
        path='/reset')

    real = Response.from_requests_response(
        request.send_request(verify=verify_ssl))
    expect = Response(status=304, headers={}, body='')
    matcher.match_responses(expect, real)
def test_ignore_all_headers_compare():
    matcher = ResponseMatcher(ignore_all_headers=True)
    r1 = Response(status=200, headers={"a": "1", "c": "not"}, body="ok")
    r2 = Response(status=200,
                  headers={
                      "a": "1",
                      "c": "same",
                      "d": 3
                  },
                  body="ok")
    f = lambda: matcher.match_responses(r1, r2)
    assert not raised_error(f, MatchError)
def test_ignore_headers_compare():
    matcher = ResponseMatcher(ignore_headers=['c', 'd', 'e'])
    r1 = Response(status=200, headers={"a": "1", "c": "not"}, body="ok")
    r2 = Response(status=200, headers={"a": "1", "c": "same"}, body="ok")
    f = lambda: matcher.match_responses(r1, r2)
    assert not raised_error(f, MatchError)
    r1.headers['d'] = "ignored should"
    assert not raised_error(f, MatchError)
    r2.headers['e'] = "ignored should"
    assert not raised_error(f, MatchError)
    r1.headers['g'] = "not ignored"
    assert raised_error(f, MatchError)
Exemple #4
0
def test_renderer_with_ignore_headers():
    options = dict()
    match_options = dict(ignore_headers=('date', 'auth'))
    records = list()
    records.append((Request(scheme='http',
                            method='GET',
                            host='for_test.org',
                            path='/',
                            headers=dict(header_1='1'),
                            data='just_for_test'),
                    Response(status=200,
                             headers=dict(content_type="text",
                                          date='yesterday',
                                          auth='secret'),
                             body='ok')))
    records.append((Request(scheme='https',
                            method='POST',
                            host='for_test.org',
                            path='/second_request',
                            headers=dict(header_a='a'),
                            data='second_request'),
                    Response(status=200,
                             headers=dict(content_type="text", date='today'),
                             body='ok')))
    renderer = Renderer(options=options, match_options=match_options)

    cases = [
        dict(request=Request(scheme='http',
                             method='GET',
                             host='for_test.org',
                             path='/',
                             headers=dict(header_1='1'),
                             data='just_for_test'),
             response=Response(status=200,
                               headers=dict(content_type="text"),
                               body='ok'),
             func_name='get_root'),
        dict(request=Request(scheme='https',
                             method='POST',
                             host='for_test.org',
                             path='/second_request',
                             headers=dict(header_a='a'),
                             data='second_request'),
             response=Response(status=200,
                               headers=dict(content_type="text"),
                               body='ok'),
             func_name='post_second_request'),
    ]

    assert renderer.prepare(records) == cases
Exemple #5
0
 def read_record(self, readable):
     line = readable.readline()
     if not line:
         return None
     record = json.loads(line)
     request = Request()
     request.__dict__.update(record['request'])
     response = Response()
     response.__dict__.update(record['response'])
     return request, response
def test_post_get_audit_state_2():
    request = Request(
        scheme='http',
        method='POST',
        headers={
            'Connection':
            'keep-alive',
            'Content-Length':
            '0',
            'Accept':
            '*/*',
            'Origin':
            'http://127.0.0.1:9000',
            'X-Requested-With':
            'XMLHttpRequest',
            'User-Agent':
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36',
            'Dnt':
            '1',
            'Content-Type':
            'application/json',
            'Referer':
            'http://127.0.0.1:9000/',
            'Accept-Encoding':
            'gzip, deflate, br',
            'Accept-Language':
            'en-US,en;q=0.9,eo;q=0.8',
            'Cookie':
            'gsScrollPos-6417=0; gsScrollPos-5300=0; gsScrollPos-2934=0; gsScrollPos-3120=0; gsScrollPos-3574=0; gsScrollPos-3508=; gsScrollPos-4736=0; gsScrollPos-3571=0; gsScrollPos-3123=0'
        },
        host='127.0.0.1:5000',
        path='/get-audit-state')

    real = Response.from_requests_response(
        request.send_request(verify=verify_ssl))
    expect = Response(
        status=200,
        headers={'content-type': 'application/json'},
        body=
        '{"all_contests":[{"candidates":["DEM Sheldon Whitehouse","REP Robert G. Flanders Jr."],"id":"senator","title":"Senator in Congress"},{"candidates":["DEM David N. Cicilline (13215)","REP Patrick J. Donovan"],"id":"rep_1","title":"Representative in Congress District 1 (13213)"},{"candidates":["DEM Gina M. Raimondo","MOD William H. Gilbert","REP Allan W. Fung","Com Anne Armstrong","Ind Luis Daniel Munoz","Ind Joseph A. Trillo"],"id":"governor","title":"Governor"},{"candidates":["DEM Daniel J. McKee","MOD Joel J. Hellmann","REP Paul E. Pence","Ind Jonathan J. Riccitelli","Ind Ross K. McCurdy"],"id":"lieutenant_governor","title":"Lieutenant Governor"},{"candidates":["DEM Nellie M. Gorbea","REP Pat V. Cortellessa"],"id":"secretary_of_state","title":"Secretary of State"},{"candidates":["DEM Peter F. Neronha","Com Alan Gordon"],"id":"attorney_general","title":"Attorney General"},{"candidates":["DEM Seth Magaziner","REP Michael G. Riley"],"id":"treasurer","title":"General Treasurer"},{"candidates":["Approve","Reject"],"id":"issue_1","title":"1. RHODE ISLAND SCHOOL BUILDINGS - $250,000,000"},{"candidates":["Approve","Reject"],"id":"issue_2","title":"2. HIGHER EDUCATION FACILITIES - $70,000,000"},{"candidates":["Approve","Reject"],"id":"issue_3","title":"3. GREEN ECONOMY AND CLEAN WATER - $47,300,000"}],"all_interpretations":[],"audit_name":"comparison-zerotest-2","audit_type_name":"ballot_comparison","ballot_ids":null,"ballot_manifest":null,"cvr_hash":null,"main_contest_in_progress":null,"reported_results":[{"contest_id":"senator","results":[{"candidate":"DEM Sheldon Whitehouse","proportion":0.595,"votes":5367},{"candidate":"REP Robert G. Flanders Jr.","proportion":0.389,"votes":3506},{"candidate":"Write-in","proportion":0.002,"votes":19},{"candidate":"undervote","proportion":0.014,"votes":127},{"candidate":"overvote","proportion":0.0002,"votes":2}]},{"contest_id":"rep_1","results":[{"candidate":"DEM David N. Cicilline (13215)","proportion":0.601,"votes":5424},{"candidate":"REP Patrick J. Donovan","proportion":0.379,"votes":3417},{"candidate":"Write-in","proportion":0.001,"votes":13},{"candidate":"undervote","proportion":0.018,"votes":166},{"candidate":"overvote","proportion":0.0001,"votes":1}]},{"contest_id":"governor","results":[{"candidate":"DEM Gina M. Raimondo","proportion":0.526,"votes":4749},{"candidate":"MOD William H. Gilbert","proportion":0.028,"votes":256},{"candidate":"REP Allan W. Fung","proportion":0.343,"votes":3094},{"candidate":"Com Anne Armstrong","proportion":0.011,"votes":103},{"candidate":"Ind Luis Daniel Munoz","proportion":0.014,"votes":123},{"candidate":"Ind Joseph A. Trillo","proportion":0.056,"votes":509},{"candidate":"Write-in","proportion":0.004,"votes":32},{"candidate":"undervote","proportion":0.016,"votes":143},{"candidate":"overvote","proportion":0.001,"votes":12}]},{"contest_id":"lieutenant_governor","results":[{"candidate":"DEM Daniel J. McKee","proportion":0.575,"votes":5184},{"candidate":"MOD Joel J. Hellmann","proportion":0.032,"votes":291},{"candidate":"REP Paul E. Pence","proportion":0.273,"votes":2464},{"candidate":"Ind Jonathan J. Riccitelli","proportion":0.028,"votes":251},{"candidate":"Ind Ross K. McCurdy","proportion":0.022,"votes":202},{"candidate":"Write-in","proportion":0.015,"votes":137},{"candidate":"undervote","proportion":0.054,"votes":490},{"candidate":"overvote","proportion":0.0002,"votes":2}]},{"contest_id":"secretary_of_state","results":[{"candidate":"DEM Nellie M. Gorbea","proportion":0.651,"votes":5873},{"candidate":"REP Pat V. Cortellessa","proportion":0.306,"votes":2757},{"candidate":"Write-in","proportion":0.001,"votes":10},{"candidate":"undervote","proportion":0.042,"votes":381},{"candidate":"overvote","proportion":0.0,"votes":0}]},{"contest_id":"attorney_general","results":[{"candidate":"DEM Peter F. Neronha","proportion":0.727,"votes":6560},{"candidate":"Com Alan Gordon","proportion":0.163,"votes":1468},{"candidate":"Write-in","proportion":0.006,"votes":51},{"candidate":"undervote","proportion":0.104,"votes":941},{"candidate":"overvote","proportion":0.0001,"votes":1}]},{"contest_id":"treasurer","results":[{"candidate":"DEM Seth Magaziner","proportion":0.658,"votes":5932},{"candidate":"REP Michael G. Riley","proportion":0.305,"votes":2751},{"candidate":"Write-in","proportion":0.008,"votes":7},{"candidate":"undervote","proportion":0.037,"votes":330},{"candidate":"overvote","proportion":0.0001,"votes":1}]},{"contest_id":"issue_1","results":[{"candidate":"Approve","proportion":0.724,"votes":6534},{"candidate":"Reject","proportion":0.225,"votes":2027},{"candidate":"undervote","proportion":0.051,"votes":459},{"candidate":"overvote","proportion":0.0001,"votes":1}]},{"contest_id":"issue_2","results":[{"candidate":"Approve","proportion":0.53,"votes":4779},{"candidate":"Reject","proportion":0.411,"votes":3708},{"candidate":"undervote","proportion":0.059,"votes":533},{"candidate":"overvote","proportion":0.0001,"votes":1}]},{"contest_id":"issue_3","results":[{"candidate":"Approve","proportion":0.749,"votes":6753},{"candidate":"Reject","proportion":0.197,"votes":1779},{"candidate":"undervote","proportion":0.054,"votes":489}]}],"seed":null,"total_number_of_ballots":null}\n'
    )
    matcher.match_responses(expect, real)
Exemple #7
0
def test_post_get_audit_state():
    request = Request(
        scheme='http',
        method='POST',
        headers={
            'Connection':
            'keep-alive',
            'Content-Length':
            '0',
            'Accept':
            '*/*',
            'Origin':
            'http://127.0.0.1:9000',
            'X-Requested-With':
            'XMLHttpRequest',
            'User-Agent':
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36',
            'Dnt':
            '1',
            'Content-Type':
            'application/json',
            'Referer':
            'http://127.0.0.1:9000/',
            'Accept-Encoding':
            'gzip, deflate, br',
            'Accept-Language':
            'en-US,en;q=0.9,eo;q=0.8',
            'Cookie':
            'gsScrollPos-6417=0; gsScrollPos-5300=0; gsScrollPos-2934=0; gsScrollPos-3120=0; gsScrollPos-3574=0; gsScrollPos-3508=; gsScrollPos-4736=0; gsScrollPos-3123=0; gsScrollPos-3571='
        },
        host='127.0.0.1:5000',
        path='/get-audit-state')

    real = Response.from_requests_response(
        request.send_request(verify=verify_ssl))
    expect = Response(
        status=200,
        headers={'content-type': 'application/json'},
        body=
        '{"all_contests":null,"all_interpretations":[],"audit_name":null,"audit_type_name":null,"ballot_ids":null,"ballot_manifest":null,"cvr_hash":null,"main_contest_in_progress":null,"reported_results":null,"seed":null,"total_number_of_ballots":null}\n'
    )
    matcher.match_responses(expect, real)
Exemple #8
0
def test_post_reset_audit_state():
    request = Request(
        scheme='http',
        method='POST',
        headers={
            'Connection':
            'keep-alive',
            'Content-Length':
            '2',
            'Accept':
            '*/*',
            'Origin':
            'http://127.0.0.1:9000',
            'X-Requested-With':
            'XMLHttpRequest',
            'User-Agent':
            'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.28 Safari/537.36',
            'Dnt':
            '1',
            'Content-Type':
            'application/json',
            'Referer':
            'http://127.0.0.1:9000/reset',
            'Accept-Encoding':
            'gzip, deflate, br',
            'Accept-Language':
            'en-US,en;q=0.9,eo;q=0.8',
            'Cookie':
            'gsScrollPos-6417=0; gsScrollPos-5300=0; gsScrollPos-2934=0; gsScrollPos-3120=0; gsScrollPos-3574=0; gsScrollPos-3508=; gsScrollPos-4736=0; gsScrollPos-3123=0; gsScrollPos-3571='
        },
        host='127.0.0.1:5000',
        path='/reset-audit-state',
        data='{}')

    real = Response.from_requests_response(
        request.send_request(verify=verify_ssl))
    expect = Response(status=200,
                      headers={'content-type': 'text/html; charset=utf-8'},
                      body='')
    matcher.match_responses(expect, real)
Exemple #9
0
    def __call__(self, environ, start_response):
        # pop incorrect content length, I don't know why
        environ.pop('CONTENT_LENGTH', None)
        request = werkzeug.wrappers.Request(environ)

        headers = {k: v for k, v in request.headers if k not in ('Host',)}
        LOG.debug("forward to [%s]%s, headers: -----%s-----", request.method, self._forward_url, headers)
        forward_request = Request(method=request.method, headers=headers, data=request.data,
                                  params=request.query_string, path=request.path, endpoint=self._forward_url)
        response = forward_request.send_request()
        forward_response = Response.from_requests_response(response)
        self.trigger_on_forward_complete(forward_request, forward_response)
        return response_with_response(response, start_response)
Exemple #10
0
def test_ignore_headers_compare():
    matcher = ResponseMatcher(ignore_headers=['c', 'd', 'e'])
    r1 = Response(status=200, headers={"a": "1", "c": "not"}, body="ok")
    r2 = Response(status=200, headers={"a": "1", "c": "same"}, body="ok")
    matcher.match_responses(r1, r2)
    r1.headers['d'] = "ignored should"
    matcher.match_responses(r1, r2)
    r2.headers['e'] = "ignored should"
    matcher.match_responses(r1, r2)
    r1.headers['g'] = "not ignored"
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)
def test_ignore_headers_compare():
    matcher = ResponseMatcher(ignore_headers=['c', 'd', 'e'])
    r1 = Response(status=200, headers={"a": "1", "c": "not"}, body="ok")
    r2 = Response(status=200, headers={"a": "1", "c": "same"}, body="ok")
    f = lambda: matcher.match_responses(r1, r2)
    assert not raised_error(f, MatchError)
    r1.headers['d'] = "ignored should"
    assert not raised_error(f, MatchError)
    r2.headers['e'] = "ignored should"
    assert not raised_error(f, MatchError)
    r1.headers['g'] = "not ignored"
    assert raised_error(f, MatchError)
def test_compare():
    matcher = ResponseMatcher()
    r1 = Response(status=200, headers={"a": "1"}, body="ok")
    r2 = Response(status=200, headers={"a": "1"}, body="ok")
    f = lambda: matcher.match_responses(r1, r2)
    assert not raised_error(f, MatchError)
    r2.body = "not ok"
    assert raised_error(f, MatchError)
    r2.body = "ok"
    r2.status = 201
    assert raised_error(f, MatchError)
    r2.status = 200
    r2.headers['b'] = 2
    assert raised_error(f, MatchError)
    del r2.headers['b']
    assert not raised_error(f, MatchError)
Exemple #13
0
def test_compare():
    matcher = ResponseMatcher()
    r1 = Response(status=200, headers={"a": "1"}, body="ok")
    r2 = Response(status=200, headers={"a": "1"}, body="ok")
    matcher.match_responses(r1, r2)
    r2.body = "not ok"
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)
    r2.body = "ok"
    r2.status = 201
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)
    r2.status = 200
    r2.headers['b'] = 2
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)
    del r2.headers['b']
    matcher.match_responses(r1, r2)
Exemple #14
0
    def __call__(self, environ, start_response):
        # pop incorrect content length, I don't know why
        if not environ.get('CONTENT_LENGTH'):
            environ.pop('CONTENT_LENGTH', None)

        request = werkzeug.wrappers.Request(environ)

        headers = {k: v for k, v in request.headers if k not in ('Host', )}
        LOG.debug("forward to [%s]%s, headers: -----%s-----, data %s",
                  request.method, self._forward_url, headers, request.data)
        forward_request = Request(method=request.method,
                                  headers=headers,
                                  data=request.data,
                                  params=request.query_string,
                                  path=request.path,
                                  endpoint=self._forward_url)
        response = forward_request.send_request()
        forward_response = Response.from_requests_response(response)
        self.trigger_on_forward_complete(forward_request, forward_response)
        return response_with_response(response, start_response)
def test_compare():
    matcher = ResponseMatcher()
    r1 = Response(status=200, headers={"a": "1"}, body="ok")
    r2 = Response(status=200, headers={"a": "1"}, body="ok")
    f = lambda: matcher.match_responses(r1, r2)
    assert not raised_error(f, MatchError)
    r2.body = "not ok"
    assert raised_error(f, MatchError)
    r2.body = "ok"
    r2.status = 201
    assert raised_error(f, MatchError)
    r2.status = 200
    r2.headers['b'] = 2
    assert raised_error(f, MatchError)
    del r2.headers['b']
    assert not raised_error(f, MatchError)
Exemple #16
0
def test_fuzzy_compare():
    matcher = ResponseMatcher(fuzzy_match=True)
    allow_none_matcher = ResponseMatcher(fuzzy_match=True, fuzzy_match_options={"allow_none": True})
    allow_blank_matcher = ResponseMatcher(fuzzy_match=True, fuzzy_match_options={"allow_blank": True})
    r1 = Response(status=200, headers={}, body=json.dumps({"id": 1, "name": "test"}))
    r2 = Response(status=200, headers={}, body=json.dumps({"id": 2, "name": "test", "some_field": 0}))
    # not work if not content-type
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r2.body = json.dumps({"id": 1, "name": "test"})
    r1.headers['content-type'] = 'application/json'
    r2.headers['CONTENT-TYPE'] = 'application/json'
    matcher.match_responses(r1, r2)

    r2.body = json.dumps({"id": "42", "name": "test42"})
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r2.body = json.dumps({"id": 42, "name": 42})
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r2.body = json.dumps({"id": "42", "name": 42})
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r2.body = json.dumps({"id": 42, "name": "test42"})
    matcher.match_responses(r1, r2)

    r2.headers['use-less-header'] = True
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r2.headers.pop('use-less-header')
    r1.body = json.dumps({"id": 1, "name": "test", "followers": []})
    r2.body = json.dumps({"id": 42, "name": "test42", "followers": [1, 2, 3]})
    matcher.match_responses(r1, r2)

    r1.body = json.dumps({"id": 1, "name": "test", "followers": ["1", "2"]})
    r2.body = json.dumps({"id": 42, "name": "test42", "followers": [1, 2, 3]})
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r1.body = json.dumps({"id": 1, "name": "test", "followers": [1, 2]})
    matcher.match_responses(r1, r2)

    r1.body = json.dumps(
        {
            "id": 1, "name": "test", "children":
            [{"id": 2, "name": "test2"}, {"id": 3, "name": "test3"}],
            "parent": {"id": 0, "name": "test0"}
        })
    r2.body = json.dumps(
        {
            "id": 42, "name": "test", "children":
            [{"id": 4, "name": "test4"}],
            "parent": {"id": 5, "name": "test5"}
        })
    matcher.match_responses(r1, r2)

    r2.body = json.dumps(
        {
            "id": 42, "name": "test", "children":
            [{"id": 4, "name": "test4"}, {"id": "what?"}],
            "parent": {"id": 5, "name": "test5"}
        })
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r2.body = json.dumps(
        {
            "id": 42, "name": "test", "children":
            [{"id": 4, "name": "test4"}, {}],
            "parent": {"id": "5", "name": "test5"}
        })
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r2.body = json.dumps(
        {
            "id": 42, "name": "test", "children":
            [{"id": 4, "name": "test4"}, {}],
            "parent": {"id": "5", "name": "test5"}
        })
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)

    r2.body = json.dumps(
        {
            "id": 42, "name": "test", "children":
            [{"id": 4, "name": "test4"}],
            "parent": {}
        })
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)
    with pytest.warns(FuzzyMatchWarning):
        allow_blank_matcher.match_responses(r1, r2)

    r2.body = json.dumps(
        {
            "id": 42, "name": "test", "children":
            [{"id": 4, "name": "test4"}],
            "parent": None
        })
    with pytest.raises(AssertionError):
        matcher.match_responses(r1, r2)
    with pytest.raises(AssertionError):
        allow_blank_matcher.match_responses(r1, r2)
    with pytest.warns(FuzzyMatchWarning):
        allow_none_matcher.match_responses(r1, r2)

    r2.body = json.dumps(
        {
            "id": 42, "name": "test", "children":
            [],
            "parent": {"id": 5, "name": "test5"}
        })
    matcher.match_responses(r1, r2)
from zerotest.utils.io_helper import StringIO

from zerotest.record.formatter import Formatter
from zerotest.request import Request
from zerotest.response import Response

req = Request(scheme="http",
              method="get",
              params="query_string=here",
              host="example.com",
              path="/test",
              headers={"just": "header"},
              data="request")
res = Response(status=200, headers={"responsed": "header"}, body="response")
formatter = Formatter()


def test_formatter():
    writable = StringIO()
    formatter.write_record(writable, req, res)
    readable = StringIO(writable.getvalue())
    request, response = formatter.read_record(readable)
    assert req.__dict__ == request.__dict__
    assert res.__dict__ == response.__dict__
def test_ignore_fields_compare():
    import json
    matcher = ResponseMatcher(ignore_fields=['some_record.created_at', 'id'])
    f = lambda: matcher.match_responses(r1, r2)
    r1 = Response(status=200,
                  headers={},
                  body=json.dumps({
                      "id": 1,
                      "name": "test"
                  }))
    r2 = Response(status=200,
                  headers={},
                  body=json.dumps({
                      "id": 2,
                      "name": "test"
                  }))
    # not work if not content-type
    assert raised_error(f, MatchError)
    r1.headers['content-type'] = 'application/json'
    r2.headers['CONTENT-TYPE'] = 'application/json'
    assert not raised_error(f, MatchError)

    r1.body = json.dumps({
        "some_record.created_at": 'now',
        'record': {
            'created_at': '111'
        }
    })
    r2.body = json.dumps({"record": {'created_at': '111'}})
    assert not raised_error(f, MatchError)
    r1.body = json.dumps({
        "some_record.created_at": 'now',
        'some_record': {
            'created_at': '111'
        }
    })
    r2.body = json.dumps({"some_record": {'created_at': '111'}})
    assert raised_error(f, MatchError)

    r1.body = json.dumps({"some_record.created_at": 'now'})
    r2.body = json.dumps({"some_record": {'created_at': '112'}})
    assert raised_error(f, MatchError)

    # test key path under list
    r1.body = json.dumps({"some_record": [{'created_at': '1123', 'id': '0'}]})
    r2.body = json.dumps({"some_record": [{'created_at': '112', 'id': '1'}]})
    assert raised_error(f, MatchError)
    r1.body = json.dumps({
        "some_record": [{
            'created_at': '1123',
            'id': '0'
        }, {
            'created_at': '3322'
        }]
    })
    r2.body = json.dumps({
        "some_record": [{
            'created_at': '112',
            'id': '1'
        }, {
            'created_at': 'tomorrow'
        }]
    })
    assert raised_error(f, MatchError)

    r1.body = json.dumps({
        "some_record": [{
            'created_at': '1123',
            'id': '1'
        }, {
            'created_at': '3322'
        }]
    })
    r2.body = json.dumps({
        "some_record": [{
            'created_at': '112',
            'id': '1'
        }, {
            'created_at': 'tomorrow'
        }]
    })
    assert not raised_error(f, MatchError)
def test_ignore_fields_compare():
    import json
    matcher = ResponseMatcher(ignore_fields=['some_record.created_at', 'id'])
    f = lambda: matcher.match_responses(r1, r2)
    r1 = Response(status=200, headers={}, body=json.dumps({"id": 1, "name": "test"}))
    r2 = Response(status=200, headers={}, body=json.dumps({"id": 2, "name": "test"}))
    # not work if not content-type
    assert raised_error(f, MatchError)
    r1.headers['content-type'] = 'application/json'
    r2.headers['CONTENT-TYPE'] = 'application/json'
    assert not raised_error(f, MatchError)

    r1.body = json.dumps({"some_record.created_at": 'now', 'record': {'created_at': '111'}})
    r2.body = json.dumps({"record": {'created_at': '111'}})
    assert not raised_error(f, MatchError)
    r1.body = json.dumps({"some_record.created_at": 'now', 'some_record': {'created_at': '111'}})
    r2.body = json.dumps({"some_record": {'created_at': '111'}})
    assert raised_error(f, MatchError)

    r1.body = json.dumps({"some_record.created_at": 'now'})
    r2.body = json.dumps({"some_record": {'created_at': '112'}})
    assert raised_error(f, MatchError)

    # test key path under list
    r1.body = json.dumps({"some_record": [{'created_at': '1123', 'id': '0'}]})
    r2.body = json.dumps({"some_record": [{'created_at': '112', 'id': '1'}]})
    assert raised_error(f, MatchError)
    r1.body = json.dumps({"some_record": [{'created_at': '1123', 'id': '0'}, {'created_at': '3322'}]})
    r2.body = json.dumps({"some_record": [{'created_at': '112', 'id': '1'}, {'created_at': 'tomorrow'}]})
    assert raised_error(f, MatchError)

    r1.body = json.dumps({"some_record": [{'created_at': '1123', 'id': '1'}, {'created_at': '3322'}]})
    r2.body = json.dumps({"some_record": [{'created_at': '112', 'id': '1'}, {'created_at': 'tomorrow'}]})
    assert not raised_error(f, MatchError)
Exemple #20
0
def test_response__str__():
    response = Response(200, {"just test": "hope pass"}, "happy test!")
    assert str(response) == """200
Exemple #21
0
def test_ignore_all_headers_compare():
    matcher = ResponseMatcher(ignore_all_headers=True)
    r1 = Response(status=200, headers={"a": "1", "c": "not"}, body="ok")
    r2 = Response(status=200, headers={"a": "1", "c": "same", "d": 3}, body="ok")
    matcher.match_responses(r1, r2)