def test_simple_response():
    #setup
    response = HttpResponse(protocol='HTTP/1.1', status_code=200)
    response.headers['Date'] = 'Fri, 31 Dec 1999 23:59:59 GMT'
    response.headers['Content-type'] = 'text/plain'
    response.content = 'This is a test'

    expected_response_msg = \
    '''HTTP/1.1 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-type: text/plain

This is a test'''

    #run
    clientsock = MockClientSocket()
    response.write_to(clientsock)

    #assert
    tools.assert_equals(clientsock.sent_data, expected_response_msg)
Пример #2
0
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')