def test_file():
    conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")

    server.handle_connection(conn)

    assert 'HTTP/1.0 200 OK\r\n' in conn.sent, 'Got: %s' % (repr(conn.sent),)
    assert '<iframe src="/file.txt" type="text/plain"></iframe>' in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_content():
    conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")

    server.handle_connection(conn)

    assert 'HTTP/1.0 200 OK\r\n' in conn.sent, 'Got: %s' % (repr(conn.sent),)
    assert '<h1>Content page!</h1>' in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_submit_page_multipart():
    first = 'Caitlin'
    last = 'McDonald'

    header_message = 'POST /submit HTTP/1.0\r\n\
Content-Length: 374\r\n\
Content-Type: multipart/form-data;\
boundary=32452685f36942178a5f36fd94e34b63\r\n\r\n\
--32452685f36942178a5f36fd94e34b63\r\n\
Content-Disposition: form-data; name="lastname";\
 filename="lastname"\r\n\r\n\
{}\r\n\
--32452685f36942178a5f36fd94e34b63\r\n\
Content-Disposition: form-data; name="firstname";\
 filename="firstname"\r\n\r\n\
{}\r\n\
--32452685f36942178a5f36fd94e34b63\r\n\
Content-Disposition: form-data; name="key\
filename="key"\r\n\r\n\
value\r\n\
--32452685f36942178a5f36fd94e34b63--\r\n'.format(last, first)

    conn = FakeConnection(header_message)

    server.handle_connection(conn, '3748')

    assert conn.sent.startswith('HTTP/1.0 200 OK\r\n')
    assert 'Hello, Ms. {} {}'.format(first, last) in conn.sent
def test_404_error():
    conn = FakeConnection("GET /blah HTTP/1.0\r\n\r\n")

    server.handle_connection(conn)

    assert 'HTTP/1.0 404 NOT FOUND\r\n' in conn.sent, 'Got: %s' % (repr(conn.sent),)
    assert '<h1>404 NOT FOUND</h1>' in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_image():
    conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")

    server.handle_connection(conn)

    assert 'HTTP/1.0200 OK\r\n' in conn.sent, 'Got: %s' % (repr(conn.sent),)
    assert '<h1>Image Page!</h1>' in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_form_get():
    conn =  FakeConnection("GET /submit?firstname=hello&lastname=world HTTP/1.0\r\n\r\n")


    server.handle_connection(conn)

    assert "Mr. hello world" in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_index_page():
    conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")

    server.handle_connection(conn, '3748')
    
    assert conn.sent.startswith('HTTP/1.0 200 OK\r\n')
    assert 'This is mcdonaldca\'s web server' in conn.sent
def test_handle_connection():
    conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")
    expected_return = 'HTTP/1.0 200 OK'
    server.handle_connection(conn)
    splitconn = conn.sent.split('\r\n')[0]

    assert splitconn == expected_return, 'Got: %s' %(repr(conn.sent),)
def test_handle_not_found():
    conn = FakeConnection("GET /fake HTTP/1.0\r\n\r\n")

    app = make_app()
    server.handle_connection(conn, app)
    assert 'HTTP/1.0 404' in conn.sent , \
    'Got: %s' % (repr(conn.sent),)
def test_handle_empty_request():
    conn = FakeConnection("\r\n\r\n")

    server.handle_connection(conn)

    assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_handle_connenction_file():
    conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
    
    server.handle_connection(conn)
 
    assert 'HTTP/1.0 200' in conn.sent and 'file' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_handle_connection():
    conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")

    server.handle_connection(conn, "arctic.cse.msu.edu", "9943")

    assert 'HTTP/1.0 200' in conn.sent and 'form' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_handle_not_found():
    conn = FakeConnection("GET /poop HTTP/1.0\r\n\r\n")

    server.handle_connection(conn, "arctic.cse.msu.edu", "9943")

    assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_form_get():
    conn = FakeConnection("GET /submit?firstname=Usman&lastname=Majeed HTTP/1.0\r\n\r\n")
    
    server.handle_connection(conn)

    assert 'HTTP/1.0200 OK\r\n' in conn.sent, 'Got: %s' % (repr(conn.sent),)
    assert 'Hello Usman Majeed!' in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_handle_connection():
    conn = FakeConnection("GET / HTTP/1.0\r\n\r\n")
    server.handle_connection(conn, 0, APP)

    assert 'HTTP/1.0 200' in conn.sent
    assert 'ettemaet' in conn.sent, \
        'Got: %s' % (repr(conn.sent),)
def test_submit_post_urlencoded():
    conn = FakeConnection("POST /submit HTTP/1.0\r\n" + \
                           "Content-Length: 26\r\n" + \
                           "Content-Type: application/x-www-form-urlencoded\r\n\r\n" + \
                           "firstname=Matt&lastname=Ao\r\n")
    server.handle_connection(conn, 80, "myapp")
    assert conn.sent[:len(expected_OK)] == expected_OK, 'Got: %s' % (repr(conn.sent),)
def test_submit_post_multipart():
    conn = FakeConnection("POST /submit HTTP/1.0\r\n" + \
                          "Content-Length: 374\r\n" + \
                          "Content-Type: multipart/form-data; " + \
                          "boundary=32452685f36942178a5f36fd94e34b63\r\n\r\n" + \
                          "--32452685f36942178a5f36fd94e34b63\r\n" + \
                          "Content-Disposition: form-data; name=\"lastname\";" + \
                          " filename=\"lastname\"\r\n\r\n" + \
                          "taylor\r\n" + \
                          "--32452685f36942178a5f36fd94e34b63\r\n" + \
                          "Content-Disposition: form-data; name=\"firstname\";" + \
                          " filename=\"firstname\"\r\n\r\n" + \
                          "ben\r\n" + \
                          "--32452685f36942178a5f36fd94e34b63\r\n" + \
                          "Content-Disposition: form-data; name=\"key\";" + \
                          " filename=\"key\"\r\n\r\n" + \
                          "value\r\n" + \
                          "--32452685f36942178a5f36fd94e34b63--\r\n"
                    )
    fname = 'ben'
    lname = 'taylor'
    er = 'HTTP/1.0 200 OK\r\n'

    app = make_app()
    server.handle_connection(conn, 80, app)

    assert conn.sent[:len(er)] == er, 'Got: %s' % (repr(conn.sent),)
def test_form_page_get():
    conn = FakeConnection("GET /form HTTP/1.0\r\n\r\n")

    server.handle_connection(conn)

    assert 'HTTP/1.0200 OK\r\n' in conn.sent, 'Got: %s' % (repr(conn.sent),)
    assert '<form action="/submit" method="GET">' in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_handle_file_get():
    conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
    header = 'HTTP/1.0 200 OK\r\n' + \
             'Content-type: text/plain\r\n' + \
             '\r\n'
    server.handle_connection(conn, 8500)
    assert conn.sent.startswith(header), 'Got: %s' % (repr(conn.sent),)
def test_PostgenerateNotFound():
    conn = FakeConnection("POST /applesauce HTTP/1.0\r\n" +\
			  "Content-Length: 0\r\n\r\n")
    server.handle_connection(conn, "arctic.cse.msu.edu", "1234")

    assert 'HTTP/1.0 404 Not Found' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_handle_image_get():
    conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")
    header = 'HTTP/1.0 200 OK\r\n' + \
             'Content-type: image/jpeg\r\n' + \
             '\r\n'
    server.handle_connection(conn, 8500)
    assert conn.sent.startswith(header), 'Got: a long string' 
def test_generateGetContent():
    conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")
    server.handle_connection(conn, "arctic.cse.msu.edu", "1234")

    assert 'HTTP/1.0 200' in conn.sent and \
	   'Content ^_^' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_handle_connection_content():
    conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")

    server.handle_connection(conn)
 
    assert 'HTTP/1.0 200' in conn.sent and 'content' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_generateGetFile():
    conn = FakeConnection("GET /file HTTP/1.0\r\n\r\n")
    server.handle_connection(conn, "arctic.cse.msu.edu", "1234")

    assert 'HTTP/1.0 200' in conn.sent and \
	   'text/plain' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_handle_not_found():
    conn = FakeConnection("GET /nada HTTP/1.0\r\n\r\n")

    server.handle_connection(conn)

    assert 'HTTP/1.0 404' in conn.sent and 'want' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
def test_generateGetImage():
    conn = FakeConnection("GET /image HTTP/1.0\r\n\r\n")
    server.handle_connection(conn, "arctic.cse.msu.edu", "1234")

    assert 'HTTP/1.0 200' in conn.sent and \
	   'image/jpeg' in conn.sent, \
    'Got: %s' % (repr(conn.sent),)
Exemple #27
0
def test_submit_post_multipart():
    conn = FakeConnection(
        "POST /submit HTTP/1.0\r\n"
        + "Content-Length: 369\r\n"
        + "Content-Type: multipart/form-data; "
        + "boundary=32452685f36942178a5f36fd94e34b63\r\n\r\n"
        + "--32452685f36942178a5f36fd94e34b63\r\n"
        + 'Content-Disposition: form-data; name="lastname";'
        + ' filename="lastname"\r\n\r\n'
        + "K\r\n"
        + "--32452685f36942178a5f36fd94e34b63\r\n"
        + 'Content-Disposition: form-data; name="firstname";'
        + ' filename="firstname"\r\n\r\n'
        + "Suh\r\n"
        + "--32452685f36942178a5f36fd94e34b63\r\n"
        + 'Content-Disposition: form-data; name="key";'
        + ' filename="key"\r\n\r\n'
        + "value\r\n"
        + "--32452685f36942178a5f36fd94e34b63--\r\n"
    )
    firstname = "K"
    lastname = "Suh"

    server.handle_connection(conn)

    assert status_code(conn) == "200", "Got: %s" % (repr(conn.sent),)
def test_non_existant_file():
    conn = FakeConnection("GET /ddd.txt HTTP/1.0\r\n\r\n")

    server.handle_connection(conn)

    assert 'HTTP/1.0 404 NOT FOUND' in conn.sent, 'Got: %s' % (repr(conn.sent),)
    assert '<h1>404 NOT FOUND</h1>' in conn.sent, 'Got: %s' % (repr(conn.sent),)
def test_form_post__multi():
    reqString = 'POST /submit HTTP/1.1\r\n' +\
	'Host: arctic.cse.msu.edu:9853\r\n' +\
	'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131030 Firefox/17.0 Iceweasel/17.0.10\r\n' +\
	'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n' +\
	'Accept-Language: en-US,en;q=0.5\r\n' +\
	'Accept-Encoding: gzip, deflate\r\n' +\
	'Connection: keep-alive\r\n' +\
	'Referer: http://arctic.cse.msu.edu:9853/formPost\r\n'+\
	'Content-Type: multipart/form-data; boundary=---------------------------10925359777073771901781915428\r\n' +\
	'Content-Length: 422\r\n' +\
	'\r\n' +\
	'-----------------------------10925359777073771901781915428\r\n' +\
	'Content-Disposition: form-data; name="firstname"\r\n' +\
	'\r\n' +\
	'hello\r\n' +\
	'-----------------------------10925359777073771901781915428\r\n' +\
	'Content-Disposition: form-data; name="lastname"\r\n' +\
	'\r\n' +\
	'world\r\n' +\
	'-----------------------------10925359777073771901781915428\r\n' +\
	'Content-Disposition: form-data; name="submit"\r\n' +\
	'\r\n' +\
	'Submit Query\r\n' +\
	'-----------------------------10925359777073771901781915428--\r\n'
    conn = FakeConnection(reqString)
    server.handle_connection(conn)

    assert "Mr. hello world" in conn.sent, 'Wrong page: %s' % (repr(conn.sent),)
def test_content_page():
    conn = FakeConnection("GET /content HTTP/1.0\r\n\r\n")

    server.handle_connection(conn, '3748')

    assert conn.sent.startswith('HTTP/1.0 200 OK\r\n')
    assert 'For a "content page" there appears to be very little content...' in conn.sent
Exemple #31
0
 def test_none_or_empty_messages(self, capsys):
     for fake_reader in [FakeReader(['']), FakeReader(None)]:
         self.loop.run_until_complete(
             server.handle_connection(fake_reader, self.fake_writer))
         assert len(self.fake_writer.getvalue()) == 0
         assert len(self.other_fake_writer.getvalue()) == 0
         assert self.fake_writer.is_closed()
         assert not self.other_fake_writer.is_closed()
         self.fake_writer.open()
         assert not self.fake_writer.is_closed()
Exemple #32
0
 def test_happy_path_one_message(self, capsys):
     message = 'Hello World!'
     fake_reader = FakeReader([message])
     self.loop.run_until_complete(
         server.handle_connection(fake_reader, self.fake_writer))
     assert len(self.fake_writer.getvalue()) == 0
     assert message.encode() in self.other_fake_writer.getvalue()
     assert self.fake_writer.get_extra_info('').encode() \
                             in self.other_fake_writer.getvalue()
     expected_out_msg1 = 'Received {} from {}'.format(
         message, self.fake_writer.get_extra_info(''))
     expected_out_msg2 = 'Close the client socket'
     out, err = capsys.readouterr()
     assert expected_out_msg1 in out
     assert expected_out_msg2 in out
     assert len(err) == 0
Exemple #33
0
 def test_happy_path_several_messages(self, capsys):
     message1 = 'Hello World!'
     message2 = 'That\'s all folks'
     messages = [message1, message2]
     fake_reader = FakeReader(messages)
     self.loop.run_until_complete(
         server.handle_connection(fake_reader, self.fake_writer))
     assert len(self.fake_writer.getvalue()) == 0
     assert message1.encode() in self.other_fake_writer.getvalue()
     assert message2.encode() in self.other_fake_writer.getvalue()
     assert self.fake_writer.get_extra_info('').encode() \
                              in self.other_fake_writer.getvalue()
     assert self.fake_writer not in server.all_clients
     assert len(server.all_clients) == 1
     expected_out_msg1 = 'Received {} from {}'.format(
         message1, self.fake_writer.get_extra_info(''))
     expected_out_msg2 = 'Received {} from {}'.format(
         message2, self.fake_writer.get_extra_info(''))
     expected_out_msg3 = 'Close the client socket'
     out, err = capsys.readouterr()
     assert expected_out_msg1 in out
     assert expected_out_msg2 in out
     assert expected_out_msg3 in out
     assert len(err) == 0
Exemple #34
0
# from source import server
import server

if __name__ == "__main__":
    host = 'localhost'
    port = 50001
    data_size = 1024
    server = server.GameServer(host, port, data_size)
    server.handle_connection()
Exemple #35
0
def connection(socket, address):
    """Handle new connections to stream server."""
    print('New connection from {}:{}'.format(address[0], address[1]))
    server.handle_connection(socket)