示例#1
0
def test_echo():

    _server = Server().serve()

    _client = Client()
    _client.send("Hi there")
    assert _client.receive() == "Hi there"
示例#2
0
def test_echo():

    _server = Server().serve()

    _client = Client()
    _client.send("Hi there")
    assert _client.receive() == "Hi there"
def server_running(request):
    """Start the server and have it listening and ready to respond"""
    server = Server()
    t = threading.Thread(target=server.listen_continuously)
    t.daemon = True
    t.start()
    yield
    client = Client("SHUTDOWN")
    client.send_and_close()
    server.close()
示例#4
0
def test_405():

    _client = Client()
    _client.send("PUT / HTTP/1.1")
    assert _client.receive() == 'HTTP/1.1 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n405 Method Not Allowed'
    _client = Client()
    _client.send("HEAD / HTTP/1.1")
    assert _client.receive() == 'HTTP/1.1 405 Method Not Allowed\r\nContent-Type: text/plain\r\n\r\n405 Method Not Allowed'
def test_server_multiple_connections(server_running):
    client = Client()
    testfile = io.open("webroot/sample.txt")
    content = testfile.read()
    client.message = "GET /sample.txt HTTP/1.1\r\n"
    response = client.send_and_close()
    assert response == "HTTP/1.1 200 OK\r\n" + \
        "Content-Type: text/plain\r\n" + \
        "Content-Length: 96\r\n\r\n" +\
        content

    # The length above is python's idea; this might be inaccurate????

    client.message = "POST /sample.txt HTTP/1.1\r\n"
    response = client.send_and_close()
    assert response == "HTTP/1.1 405 Method Not Allowed\r\n"

    client.message = "GET /ind.html HTTP/1.1\r\n"
    response = client.send_and_close()
    assert response == "HTTP/1.1 404 Not Found\r\n"

    client.message = "GET /sample.txt HTTP/2.1\r\n"
    response = client.send_and_close()
    assert response == "HTTP/1.1 505 HTTP Version Not Supported\r\n"

    client.message = "Hi there how are you I am fine nice weather, yeah?"
    response = client.send_and_close()
    assert response == "HTTP/1.1 400 Bad Request\r\n"

    client.message = "GET /images HTTP/1.1\r\n"
    response = client.send_and_close()
    lines = response.splitlines()
    assert lines[0] == "HTTP/1.1 200 OK"
    assert lines[1] == "Content-Type: text/html"
    assert lines[2] == "Content-Length: " + str(len(lines[4]))

    client.message = "GET /images/sample_1.png HTTP/1.1\r\n"
    response = client.send_and_close()
    lines = response.splitlines()
    asset = \
        urllib2.urlopen("file:///Users/jbbrokaw/repositories/network-tools/" +
                        "webroot/images/sample_1.png")
    assert lines[0] == "HTTP/1.1 200 OK"
    assert lines[1] == "Content-Type: image/png"
    assert lines[2] == "Content-Length: " + asset.headers["Content-Length"]
示例#6
0
def test_200():

    _client = Client()
    _client.send("GET / HTTP/1.1")
    assert _client.receive() == 'HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n200 OK'
示例#7
0
def test_505():

    _client = Client()
    _client.send("GET / HTTP/1.0")
    assert _client.receive() == 'HTTP/1.1 505 HTTP Version Not Supported\r\nContent-Type: text/plain\r\n\r\n505 HTTP Version Not Supported'
示例#8
0
def test_400():

    _client = Client()
    _client.send("GET HTTP/1.1")
    assert _client.receive() == 'HTTP/1.1 400 Bad Request\r\nContent-Type: text/plain\r\n\r\n400 Bad Request'
def client_send_receive(message):
    _client = Client()
    _client.send(message)
    print _client.receive()
def client_send_receive(message):
    _client = Client()
    _client.send(message)
    print _client.receive()
def test_server_multiple_connections(server_running):
    client = Client()
    for i in xrange(10):
        client.message = "Test %i" % i
        response = client.send_and_close()
        assert response == "I heard you say: Test %i" % i
def test_client_send(server_running):
    client = Client("Hi there")
    assert client.send_and_close() == "I heard you say: Hi there"