示例#1
0
def test_validate_query_param():
    assert utils.validate_query_param(1) == '1'
    assert utils.validate_query_param(True) == 'true'
    assert utils.validate_query_param(None) == ''
    assert utils.validate_query_param('') == ''
    assert utils.validate_query_param([1, 2, 3]) == ['1', '2', '3']
    with pytest.raises(exceptions.ParameterError):
        utils.validate_query_param({})
    with pytest.raises(exceptions.ParameterError):
        utils.validate_query_param([1, 2, {}])
def _get_params(method, encoding, fields, params=None):
    """
    Separate the params into the various types.
    """
    if params is None:
        return empty_params

    field_map = {field.name: field for field in fields}

    path = {}
    query = {}
    data = {}
    files = {}

    errors = {}

    # Ensure graceful behavior in edge-case where both location='body' and
    # location='form' fields are present.
    seen_body = False

    for key, value in params.items():
        if key not in field_map or not field_map[key].location:
            # Default is 'query' for 'GET' and 'DELETE', and 'form' for others.
            location = 'query' if method in ('GET', 'DELETE') else 'form'
        else:
            location = field_map[key].location

        if location == 'form' and encoding == 'application/octet-stream':
            # Raw uploads should always use 'body', not 'form'.
            location = 'body'

        try:
            if location == 'path':
                path[key] = utils.validate_path_param(value)
            elif location == 'query':
                query[key] = utils.validate_query_param(value)
            elif location == 'body':
                data = utils.validate_body_param(value, encoding=encoding)
                seen_body = True
            elif location == 'form':
                if not seen_body:
                    data[key] = utils.validate_form_param(value,
                                                          encoding=encoding)
        except exceptions.ParameterError as exc:
            errors[key] = exc.message

    if errors:
        raise exceptions.ParameterError(errors)

    # Move any files from 'data' into 'files'.
    if isinstance(data, dict):
        for key, value in list(data.items()):
            if is_file(data[key]):
                files[key] = data.pop(key)

    return Params(path, query, data, files)