示例#1
0
    def test_get_response_not_found(self):
        r1 = RouteResponse('id1', 'string', Delay())
        route = Route(
            id='id1',
            responses=[r1],
            response_selection=ResponseSelectionStrategy.random,
            path=re.compile(r'/test.*'),
            auth=NoAuth(),
            method='GET'
        )

        assert route.get_response('id3') is None
示例#2
0
    def test_select_response(self):
        response = RouteResponse('id1', 'string', Delay())
        route = Route(
            id='id1',
            responses=[response],
            response_selection=ResponseSelectionStrategy.greedy,
            path=re.compile(r'/test.*'),
            auth=NoAuth(),
            method='GET'
        )

        assert route.select_response() is response
示例#3
0
    def test_use_without_response(self):
        route = Route(
            id='id1',
            responses=[],
            response_selection=ResponseSelectionStrategy.random,
            path=re.compile(r'/test.*'),
            auth=NoAuth(),
            method='GET'
        )

        route.use(None)
        assert route.used_count == 1
示例#4
0
    def test_use_increases_counter(self):
        response = RouteResponse('id1', 'string', Delay())
        route = Route(
            id='id1',
            responses=[response],
            response_selection=ResponseSelectionStrategy.random,
            path=re.compile(r'/test.*'),
            auth=NoAuth(),
            method='GET'
        )

        route.use(response)
        assert route.used_count == 1
        assert response.used_count == 1
示例#5
0
 def test_authenticate(self):
     route = Route(
         id='id1',
         responses=[],
         response_selection=ResponseSelectionStrategy.random,
         path=re.compile(r'/test.*'),
         auth=NoAuth(),
         method='GET'
     )
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test_url',
         method='GET'
     )
     route.authenticate(request)
示例#6
0
    def test_deserialize_complete(self):
        route = Route.deserialize({
            'id': 'id1',
            'path': '/endpoint_\\w*',
            'method': 'GET',
            'auth': {
                'method': 'basic',
                'username': '******',
                'password': '******'
            },
            'response_selection': 'random',
            'responses': [
                {
                    'id': 'response_1',
                    'body': {
                        'works': True
                    }
                }
            ]
        })

        assert route.id == 'id1'
        assert isinstance(route.path, re.Pattern)
        assert route.method == 'GET'
        assert route.auth.method == 'basic'
        assert route.response_selection == ResponseSelectionStrategy.random
        assert route.is_active == True
示例#7
0
 def test_match_active(self):
     route = Route(
         id='id1',
         responses=[
             RouteResponse('id1', '', Delay(), weight=0.4)
         ],
         response_selection=ResponseSelectionStrategy.random,
         path=re.compile(r'/test.*'),
         auth=NoAuth(),
         method='GET'
     )
     request = IncomingTestRequest(
         base_url='http://localhost/',
         full_path='/test_url',
         method='GET'
     )
     assert route.match(request)
示例#8
0
    def test_deserialize(self):
        route = Route(
            id='id1',
            responses=[],
            response_selection=ResponseSelectionStrategy.random,
            path=re.compile(r'/test.*'),
            auth=NoAuth(),
            method='GET'
        )

        assert route.serialize() == {
            'id': 'id1',
            'responses': [],
            'response_selection': 'random',
            'path': '/test.*',
            'auth': None,
            'method': 'GET',
            'used_count': 0,
            'is_active': False
        }
示例#9
0
    def test_is_not_active_if_no_active_response(self):
        response = RouteResponse('id1', 'string', Delay(), repeat=0)
        route = Route(
            id='id1',
            responses=[response],
            response_selection=ResponseSelectionStrategy.greedy,
            path=re.compile(r'/test.*'),
            auth=NoAuth(),
            method='GET'
        )

        assert route.is_active == 0
示例#10
0
 def test_deserialize_duplicate_response_ids(self):
     with pytest.raises(RouteConfigurationError):
         route = Route.deserialize({
             'id': 'id1',
             'path': '/endpoint_\\w*',
             'responses': [
                 {
                     'id': 'duplicate',
                     'body': ''
                 },
                 {
                     'id': 'duplicate',
                     'body': ''
                 }
             ]
         })
示例#11
0
    def test_deserialize_minimal(self):
        route = Route.deserialize({
            'id': 'id1',
            'path': '/endpoint_\\w*',
            'responses': [
                {
                    'body': ''
                }
            ]
        })

        assert route.id == 'id1'
        assert isinstance(route.path, re.Pattern)
        assert route.method == 'GET'
        assert isinstance(route.auth, NoAuth)
        assert route.response_selection == ResponseSelectionStrategy.greedy
        assert route.is_active == True