def test_stream_end_part_of_file():
    # setup
    file = get_file(FILE)
    range = (10, file.file_size - 1)
    clientsock = MockClientSocket(recv_data=HTTP_REQUEST_KNOWN_FILE)

    # run
    file.stream_to(clientsock, range, file_chunk_size=None)

    # assert
    tools.assert_equals(clientsock.sent_data, 'test 1')
def handle_request(clientsock):
    Log.info('gdz.log handle_request --begin')

    data = clientsock.recv(RECV_BUFSIZ)

    Log.debug('Request received:\n%s', data)

    request = parse_http_request(data)
    Log.debug('gdz.log request ist: %s', json.dumps(str(request)))

    params = {}
    i = 0
    for item in str(request.request_uri[1:]).split('/'):
        key, value = i, item
        params[key] = value
        i = i+1
    Log.debug('gdz.log params: %s', json.dumps(params))
    file = get_file(params[2])
    Log.debug('gdz.log file.info: %s', json.dumps(file.__dict__))

    if (file.exists == False) and (params[0] == 'api'):
        result = {}
        Log.debug('gdz.log 0. heute ist api')
        response = HttpResponse(protocol=request.protocol, status_code=200)
        response.headers['charset'] = 'utf-8'
        response.headers['Content-type'] = 'text/plain'
        result['code'] = 0
        result['msg'] = '请求主页成功'
        result['success'] = 'true'
        if params[2] == 'home':
            result['result'] = getHomeContent()
        elif params[2] == 'categoryList':
            result['result'] = getCategoryList()
        elif params[2] == 'categoryDetail':
            result['result'] = getCategoryDetail()
        else:
            result['success'] = 'false'
            result['code'] = -1
            result['msg'] = 'not fund'
        Log.info(json.dumps(result))
        response.content = json.dumps(result)

    elif file.exists and request.is_range_requested():
        Log.debug('gdz.log 1. ist range requested.')
        response = HttpResponse(protocol=request.protocol, status_code=206,
                                range=request.get_range())
        response.file = file

    elif file.exists:
        Log.debug('gdz.log 2. file exists.')
        response = HttpResponse(protocol=request.protocol, status_code=200)
        response.headers['charset'] = 'utf-8'
        response.file = file

    else:
        Log.debug('gdz.log 3. file ist not gefunden.')
        response = HttpResponse(protocol=request.protocol, status_code=404)
        response.headers['Content-type'] = 'text/plain'
        response.headers['charset'] = 'utf-8'
        response.content = 'This file does not exist!'

    Log.info('GET %s %s %s %s',
             request.request_uri, request.protocol, request.get_range(), response.status_code)

    response.write_to(clientsock)
    clientsock.close()
    Log.info('gdz.log handle_request --end')
def test_get_file_1():
    file = get_file(FILE)

    tools.assert_equals(file.exists, True)
    tools.assert_equals(file.mime_type, 'text/plain')