示例#1
0
    def test_endheaders_sends_outstanding_data(self):
        conn = spdypy.SPDYConnection('www.google.com')
        mock = MockConnection()
        conn._sck = mock
        conn.putrequest(b'GET', b'/')
        conn.endheaders()

        assert mock.called == 1
示例#2
0
    def test_endheaders_can_add_data(self):
        conn = spdypy.SPDYConnection('www.google.com')
        mock = MockConnection()
        conn._sck = mock
        conn.putrequest(b'GET', b'/')
        conn.endheaders(message_body=b'TestTestTest')

        assert mock.called == 2
        assert mock.buffer.endswith(b'TestTestTest')
示例#3
0
    def test_endheaders_can_specify_a_stream(self):
        conn = spdypy.SPDYConnection('www.google.com')
        mock = MockConnection()
        conn._sck = mock
        stream_id = conn.putrequest(b'GET', b'/')
        conn.putrequest(b'POST', b'/post')

        conn.endheaders(stream_id=stream_id)

        assert mock.called == 1
        assert len(conn._streams[stream_id]._queued_frames) == 0
示例#4
0
    def test_new_streams_use_new_stream_id(self):
        conn = spdypy.SPDYConnection('www.google.com')
        conn._sck = MockConnection()
        stream_id = conn.putrequest(b'GET', b'/')

        assert len(conn._streams) == 1
        assert stream_id == 1
        assert conn._streams[stream_id]

        second_stream_id = conn.putrequest(b'POST', b'other')

        assert len(conn._streams) == 2
        assert second_stream_id == 3
        assert conn._streams[second_stream_id]
示例#5
0
    def test_putrequest_accepts_strings_as_well_as_bytes(self):
        conn = spdypy.SPDYConnection('www.google.com')
        conn._sck = MockConnection()
        stream_id = conn.putrequest('GET', '/')

        headers = {
            b':method': b'GET',
            b':path': b'/',
            b':version': b'HTTP/1.1',
            b':host': b'www.google.com',
            b':scheme': b'https'
        }

        stream = conn._streams[stream_id]

        assert stream._queued_frames[0].headers == headers
示例#6
0
    def test_putheader_can_add_headers(self):
        conn = spdypy.SPDYConnection('www.google.com')
        conn._sck = MockConnection()
        stream_id = conn.putrequest(b'GET', b'/')
        conn.putheader(b'Key', b'Value')

        headers = {
            b':method': b'GET',
            b':path': b'/',
            b':version': b'HTTP/1.1',
            b':host': b'www.google.com',
            b':scheme': b'https',
            b'Key': b'Value',
        }

        stream = conn._streams[stream_id]

        assert stream._queued_frames[0].headers == headers
示例#7
0
    def test_spec_mandatory_headers_are_present(self):
        conn = spdypy.SPDYConnection('www.google.com')
        conn._sck = MockConnection()
        stream_id = conn.putrequest(b'GET', b'/')

        mandatory_headers = {
            b':method': b'GET',
            b':path': b'/',
            b':version': b'HTTP/1.1',
            b':host': b'www.google.com',
            b':scheme': b'https'
        }

        stream = conn._streams[stream_id]

        # We probably need a better way to test this than reaching the whole
        # way through the stack.
        assert stream._queued_frames[0].headers == mandatory_headers
示例#8
0
    def test_connect(self):
        # We need to stub out a ton of stuff.
        import socket

        # We need a mock socket to give the procedure.
        mock_sock = MagicMock()

        # getaddrinfo needs to return some controlled data.
        old_addrinfo = socket.getaddrinfo
        socket.getaddrinfo = MagicMock(return_value=(('', '', '', '',
                                                      ('127.0.0.1', 443)), ))

        # The socket constructor should always return our mock.
        old_socket = socket.socket
        socket.socket = MagicMock(return_value=mock_sock)

        conn = spdypy.SPDYConnection('www.google.com')

        # The SSLContext should be fake too.
        conn._context = MagicMock()
        conn._context.wrap_socket = MagicMock(return_value=mock_sock)

        # Ok, call the method.
        conn._connect()

        # We should have called getaddrinfo.
        socket.getaddrinfo.assert_called_once_with('www.google.com', 443)

        # We should have gotten one socket.
        socket.socket.assert_called_once_with()

        # We should have wrapped the socket.
        conn._context.wrap_socket.assert_called_once_with(
            mock_sock, server_hostname='www.google.com')

        # We should then have called connect.
        mock_sock.connect.assert_called_once_with(('127.0.0.1', 443))

        # Put the old stuff back.
        socket.getaddrinfo = old_addrinfo
        socket.socket = old_socket
示例#9
0
    def test_putheader_does_most_recent_stream_by_default(self):
        conn = spdypy.SPDYConnection('www.google.com')
        conn._sck = MockConnection()
        stream_id = conn.putrequest(b'GET', b'/')
        stream_id2 = conn.putrequest(b'POST', b'/post')

        conn.putheader(b'Key', b'Value')

        headers = {
            b':method': b'POST',
            b':path': b'/post',
            b':version': b'HTTP/1.1',
            b':host': b'www.google.com',
            b':scheme': b'https',
            b'Key': b'Value',
        }

        first_stream = conn._streams[stream_id]
        second_stream = conn._streams[stream_id2]

        assert first_stream._queued_frames[0].headers != headers
        assert second_stream._queued_frames[0].headers == headers
示例#10
0
 def test_initial_connection_state_is_new(self):
     conn = spdypy.SPDYConnection(None)
     assert conn._state == spdypy.connection.NEW
示例#11
0
 def test_connection_has_state(self):
     conn = spdypy.SPDYConnection(None)
     assert hasattr(conn, '_state')
示例#12
0
 def test_can_create_connection(self):
     conn = spdypy.SPDYConnection(None)
     assert conn
示例#13
0
文件: basic.py 项目: profiles/spdypy
# -*- coding: utf-8 -*-
"""
A basic example of how to use SPDY.

This example is currently incomplete, but should remain current to the state of
the library. This means that it enumerates the full state of everything
SPDYPy can do.
"""
import sys
sys.path.append('.')
import spdypy

conn = spdypy.SPDYConnection('www.google.com')
conn.putrequest('GET', '/')
conn.putheader('user-agent', 'spdypy')
conn.putheader('accept', '*/*')
conn.putheader('accept-encoding', 'gzip,deflate')
conn.endheaders()

# Debugging output for now.
frame_list = conn._read_outstanding(timeout=0.5)
while frame_list:
    for frame in frame_list:
        print(frame)

    frame_list = conn._read_outstanding(timeout=0.5)