示例#1
0
def test_update_sends_whole_response_to_client():
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'test'
    connection.send(response)
    connection.update()

    assert fake_socket.recv_buffer == response
示例#2
0
def test_update_empties_send_buffer_after_sending_whole_response():
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'test'
    connection.send(response)
    connection.update()

    assert not connection.send_buffer
示例#3
0
def test_connection_updates_raises_error_if_socket_does_not_send_anything():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.send_buffer = b''
    connection.state = Connection.SENDING_RESPONSE

    with pytest.raises(Exception, match='Socket connection broken'):
        connection.update()
示例#4
0
def test_connection_receives_first_4_bytes_of_longer_request():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.receive(Mock(), BUFFER_SIZE)
    fake_socket.send_buffer = b'longer test request'
    connection.update()

    assert connection.recv_buffer == b'long'
示例#5
0
def test_stores_received_request_in_recv_buffer():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.receive(Mock(), BUFFER_SIZE)
    request = fake_socket.send_buffer
    connection.update()

    assert connection.recv_buffer == request
示例#6
0
def test_body_is_read_only_the_amount_of_content_length():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 100
    connection.receive(Mock(), buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\nHost: www.w3.org\r\naccept: text/html\r\ncontent-length: 3\r\n\r\ntest'
    connection.update()

    assert connection.request_body == b'tes'
示例#7
0
def test_no_request_body_read_without_content_length_header():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 78
    connection.receive(Mock(), buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\nHost: www.w3.org\r\naccept: text/html\r\n\r\ntest'
    connection.update()

    assert connection.request_body == None
示例#8
0
def test_connection_is_closed_after_the_whole_response_is_sent():
    close_callback = Mock()
    connection = Connection(ADDR, fake_socket, close_callback)
    response = b'test'
    connection.send(response)
    connection.update()

    close_callback.assert_called_with(connection.socket)
示例#9
0
def test_connection_is_closed_if_nothing_received_from_the_client():
    close_callback = Mock()
    connection = Connection(ADDR, fake_socket, close_callback)
    connection.receive(Mock(), BUFFER_SIZE)
    fake_socket.send_buffer = b''
    connection.update()

    close_callback.assert_called_with(connection.socket)
示例#10
0
def test_connection_receives_longer_request_in_two_chunks():
    connection = Connection(ADDR, fake_socket, Mock())
    connection.receive(Mock(), BUFFER_SIZE)
    fake_socket.send_buffer = b'longer test request'
    connection.update()
    connection.update()

    assert connection.recv_buffer == b'longer t'
示例#11
0
def test_update_called_twice_sends_the_whole_response():
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'testing'
    connection.send(response)
    connection.update()
    connection.update()

    assert connection.send_buffer == b''
    assert fake_socket.recv_buffer == b'testing'
示例#12
0
def test_request_received_callback_is_called_when_no_body():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 57
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 60\r\n\r\n'
    connection.update()
    connection.update()
    received_callback.assert_called_with(connection)
示例#13
0
def test_update_removes_beginning_of_response_from_send_buffer_with_longer_response(
):
    connection = Connection(ADDR, fake_socket, Mock())
    response = b'test longer response'
    connection.send(response)
    connection.update()

    assert connection.send_buffer == b' longer response'
    assert fake_socket.recv_buffer == b'test'
示例#14
0
def test_body_is_not_read_as_a_whole_because_content_length_is_shorter():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 57
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 4\r\n\r\ntesting a bit longer body that is not read in its entirety'
    connection.update()
    connection.update()
    assert connection.request_body == b'test'
示例#15
0
def test_request_received_callback_called_after_startline_and_headers_read_but_body_is_not(
):
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 56
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 4\r\n\r\ntest'
    connection.update()

    assert connection.state == Connection.RECEIVING_BODY
示例#16
0
def test_received_callback_is_called_with_connection_as_parameter_after_whole_request_is_received(
):
    connection = Connection(ADDR, fake_socket, Mock())
    received_callback = Mock()
    connection.receive(received_callback, 70)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\n\r\n'
    connection.update()
    request = Request('GET', '/path/to/example.com', 'HTTP/1.1', None, None)

    received_callback.assert_called_with(connection, request)
示例#17
0
def test_request_received_callback_is_called_after_a_longer_body_is_read():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 58
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 60\r\n\r\ntesting a somewhat longer body that is not read in its xxxxx'
    connection.update()
    connection.update()
    connection.update()
    received_callback.assert_called_with(connection)
示例#18
0
def test_update_reads_request_body_after_headers_received():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 56
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 4\r\n\r\ntest'
    connection.update()
    connection.update()

    assert connection.request_body == b'test'
示例#19
0
def test_update_reads_longer_request_body_after_headers_received():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 56
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 105\r\n\r\nthis is a very long request body that will be read in two parts because it is longer than the buffer size'
    connection.update()
    connection.update()
    connection.update()

    assert connection.request_body == b'this is a very long request body that will be read in two parts because it is longer than the buffer size'
示例#20
0
def test_part_of_the_body_read_when_headers_and_rest_with_another_update_call(
):
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 57
    received_callback = Mock()
    connection.receive(received_callback, buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\ncontent-length: 4\r\n\r\ntest'
    connection.update()
    assert connection.request_body == b't'

    connection.update()
    assert connection.request_body == b'test'
示例#21
0
def test_parsed_request_is_stored_in_the_connection():
    connection = Connection(ADDR, fake_socket, Mock())
    buffer_size = 100
    connection.receive(Mock(), buffer_size)
    fake_socket.send_buffer = b'GET /path/to/example.com HTTP/1.1\r\nHost: www.w3.org\r\naccept: text/html\r\n\r\n'
    connection.update()

    assert connection.parsed_request.method == 'GET'
    assert connection.parsed_request.uri == '/path/to/example.com'
    assert connection.parsed_request.http_version == 'HTTP/1.1'
    assert connection.parsed_request.headers == {
        'host': 'www.w3.org',
        'accept': 'text/html'
    }