Beispiel #1
0
def generate_response_header_validator(headers, context, **kwargs):
    validators = ValidationDict()
    for key, header_definition in headers.items():
        # generate a function that will attempt to cast the header to the
        # appropriate type.
        header_processor = generate_value_processor(
            context=context,
            **header_definition
        )
        # generate a function that will validate the header.
        header_validator = functools.partial(
            validate_object,
            field_validators=construct_header_validators(header_definition, context=context),
        )
        # Chain the type casting function, the individual header validation
        # function with a methodcaller that will fetch the header with
        # `response.headers.get(header_name, EMPTY)` and then feed that into
        # the type casting function and then into the validation function.
        validators.add_validator(key, chain_reduce_partial(
            methodcaller('get', key, EMPTY),
            header_processor,
            header_validator,
        ))
    return chain_reduce_partial(
        attrgetter('headers'),
        functools.partial(validate_object, field_validators=validators),
    )
def generate_response_header_validator(headers, context, **kwargs):
    validators = ValidationDict()
    for key, header_definition in headers.items():
        # generate a function that will attempt to cast the header to the
        # appropriate type.
        header_processor = generate_value_processor(
            context=context,
            **header_definition
        )
        # generate a function that will validate the header.
        header_validator = functools.partial(
            validate_object,
            field_validators=construct_header_validators(header_definition, context=context),
        )
        # Chain the type casting function, the individual header validation
        # function with a methodcaller that will fetch the header with
        # `response.headers.get(header_name, EMPTY)` and then feed that into
        # the type casting function and then into the validation function.
        validators.add_validator(key, chain_reduce_partial(
            methodcaller('get', key, EMPTY),
            header_processor,
            header_validator,
        ))
    return chain_reduce_partial(
        attrgetter('headers'),
        functools.partial(validate_object, field_validators=validators),
    )
Beispiel #3
0
def generate_header_validator(headers, context, **kwargs):
    """
    Generates a validation function that will validate a dictionary of headers.
    """
    validators = ValidationDict()
    for header_definition in headers:
        header_processor = generate_value_processor(context=context, **header_definition)
        header_validator = generate_object_validator(
            field_validators=construct_header_validators(header_definition, context=context)
        )
        validators.add_property_validator(
            header_definition["name"], chain_reduce_partial(header_processor, header_validator)
        )
    return generate_object_validator(field_validators=validators)
def test_header_type_validation_for_invalid_values(type_, value):
    header_definition = single_header_validator({
        'type': type_,
    })
    validators = construct_header_validators(header_definition=header_definition, context={})

    with pytest.raises(ValidationError) as err:
        validate_object(value, validators)

    assert 'type' in err.value.detail
    assert_error_message_equal(
        err.value.detail['type'][0],
        MESSAGES['type']['invalid'],
    )
Beispiel #5
0
def test_header_type_validation_for_invalid_values(type_, value):
    header_definition = single_header_validator({
        'type': type_,
    })
    validators = construct_header_validators(
        header_definition=header_definition, context={})

    with pytest.raises(ValidationError) as err:
        validate_object(value, validators)

    assert 'type' in err.value.detail
    assert_error_message_equal(
        err.value.detail['type'][0],
        MESSAGES['type']['invalid'],
    )
Beispiel #6
0
def generate_header_validator(headers, context, **kwargs):
    validators = {}
    for header_definition in headers:
        header_validator = functools.partial(
            validate_object,
            validators=construct_header_validators(header_definition, context=context),
            inner=True,
        )
        validators[header_definition['name']] = chain_reduce_partial(
            operator.methodcaller('get', header_definition['name'], EMPTY),
            header_validator,
        )
    return chain_reduce_partial(
        operator.attrgetter('headers'),
        functools.partial(validate_object, validators=validators, inner=True),
    )
Beispiel #7
0
def generate_header_validator(headers, context, **kwargs):
    """
    Generates a validation function that will validate a dictionary of headers.
    """
    validators = ValidationDict()
    for header_definition in headers:
        header_processor = generate_value_processor(context=context,
                                                    **header_definition)
        header_validator = generate_object_validator(
            field_validators=construct_header_validators(header_definition,
                                                         context=context), )
        validators.add_property_validator(
            header_definition['name'],
            chain_reduce_partial(
                header_processor,
                header_validator,
            ),
        )
    return generate_object_validator(field_validators=validators)
def test_header_type_validation_for_invalid_values(type_, value):
    from django.core.exceptions import ValidationError
    serializer = HeaderSerializer(
        data={
            'type': type_,
        }
    )
    assert serializer.is_valid()
    header_definition = serializer.object
    validators = construct_header_validators(header_definition=header_definition, context={})

    with pytest.raises(ValidationError) as err:
        validate_object(value, validators, inner=True)

    assert 'type' in err.value.messages[0]
    assert_error_message_equal(
        err.value.messages[0]['type'][0],
        MESSAGES['type']['invalid'],
    )
Beispiel #9
0
def generate_response_header_validator(headers, context, **kwargs):
    validators = {}
    for key, header_definition in headers.items():
        header_validator = functools.partial(
            validate_object,
            validators=construct_header_validators(header_definition, context=context),
            inner=True,
        )
        # Chain the individual header validation function with a methodcaller
        # that will fetch the header with
        # `response.headers.get(header_name, EMPTY)`
        # and then feed that into the validation function.
        validators[key] = chain_reduce_partial(
            operator.methodcaller('get', key, EMPTY),
            header_validator,
        )
    return chain_reduce_partial(
        operator.attrgetter('headers'),
        functools.partial(validate_object, validators=validators, inner=True),
    )