这个GET请求的预期行为是什么?

时间:2016-12-04 19:32:39

标签: get network-programming

对于学校作业,我创建了一个基本的Web服务器,而且我很难理解我应该如何在测试脚本中回应这个请求。

程序:写入" GET / loadavg HTTP / 1.0 \ r \ n"然后到服务器                    检查没有任何已经返回,并完成了                    额外" \ r \ n"并检查从发回的数据                    服务器

AssertionError:http响应在额外之前太早返回  线。

我只是想了解测试实际测试的内容,如果这是有道理的。通常,我会去TA,但是周日。 / loadavg部分我很好,但我对\ r \ n如何影响这个测试甚至如何处理它感到困惑。

这是相关的测试代码。如果有人能够对此有所了解并解释这项测试的作用,我将非常感激。

    #Make HTTP connection for the server
    sock = server_check.get_socket_connection(self.hostname, self.port)

    sock.send("GET /loadavg HTTP/1.0\r\n")
    sock.send("Host: " + self.hostname + "\r\n")
    sock.settimeout(1)
    time.sleep(.1)
    try:
        if sock.recv(4096, socket.MSG_PEEK) != '':
            self.fail("The http response was returned too early, before" +\
            " the extra \r\n line.")

    except socket.timeout:
        pass

    sock.send("\r\n")
    #If there is a HTTP response, it should be a valid /loadavg
    #response.
    data = ""

    time.sleep(0.1)
    try:
        while sock.recv(4096, socket.MSG_PEEK) != '':
            msg_buffer = sock.recv(4096)
            data = data + msg_buffer

    #Connections close after responses for HTTP/1.0 , therefore a timeout
    #should not occur.
    except socket.timeout:
        self.fail("The server did not respond and close the connection in sufficient time.")


    data = data.split("\r\n\r\n")
    assert len(data) == 2, \
        "The response could not be parsed, check your use of \\r\\n"

    assert server_check.check_loadavg_response(data[1]), \
        "The /loadavg object was not properly returned."

0 个答案:

没有答案