Exemplo n.º 1
0
        async def main(l):
            async with aiohttp.ClientSession(loop=l) as session:
                with async_timeout.timeout(3):
                    async with session.get(url) as get_response:
                        assert get_response.status == 404
                        assert await get_response.text() == body

                with async_timeout.timeout(3):
                    async with session.post(url, data=body * 6) as post_response:
                        assert post_response.status == 201
                        assert await post_response.text() == body * 2
                        assert Mocket.last_request().method == 'POST'
                        assert Mocket.last_request().body == body * 6
Exemplo n.º 2
0
 def test_multipart(self):
     url = 'http://httpbin.org/post'
     data = '--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="content"\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 68\r\n\r\nAction: comment\nText: Comment with attach\nAttachment: x1.txt, x2.txt\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_2"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_1"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz--\r\n'
     headers = {
         'Content-Length': '495',
         'Content-Type': 'multipart/form-data; boundary=xXXxXXyYYzzz',
         'Accept': 'text/plain',
         'User-Agent': 'Mocket',
         'Accept-encoding': 'identity',
     }
     Entry.register(Entry.POST, url)
     response = requests.post(url, data=data, headers=headers)
     self.assertEqual(response.status_code, 200)
     last_request = Mocket.last_request()
     self.assertEqual(last_request.method, 'POST')
     self.assertEqual(last_request.path, '/post')
     self.assertEqual(last_request.body, data)
     sent_headers = dict(last_request.headers)
     self.assertEqualHeaders(
         sent_headers, {
             'accept': 'text/plain',
             'accept-encoding': 'identity',
             'content-length': '495',
             'content-type': 'multipart/form-data; boundary=xXXxXXyYYzzz',
             'host': 'httpbin.org',
             'user-agent': 'Mocket'
         })
Exemplo n.º 3
0
 def test_multipart(self):
     url = 'http://httpbin.org/post'
     data = '--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="content"\r\nContent-Type: text/plain; charset=utf-8\r\nContent-Length: 68\r\n\r\nAction: comment\nText: Comment with attach\nAttachment: x1.txt, x2.txt\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_2"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz\r\nContent-Disposition: form-data; name="attachment_1"; filename="x.txt"\r\nContent-Type: text/plain\r\nContent-Length: 4\r\n\r\nbye\n\r\n--xXXxXXyYYzzz--\r\n'
     headers = {
         'Content-Length': '495',
         'Content-Type': 'multipart/form-data; boundary=xXXxXXyYYzzz',
         'Accept': 'text/plain',
         'User-Agent': 'Mocket',
         'Accept-encoding': 'identity',
     }
     Entry.register(Entry.POST, url)
     response = requests.post(url, data=data, headers=headers)
     self.assertEqual(response.status_code, 200)
     last_request = Mocket.last_request()
     self.assertEqual(last_request.method, 'POST')
     self.assertEqual(last_request.path, '/post')
     self.assertEqual(last_request.body, data)
     sent_headers = dict(last_request.headers)
     self.assertEqualHeaders(
         sent_headers,
         {
             'accept': 'text/plain',
             'accept-encoding': 'identity',
             'content-length': '495',
             'content-type': 'multipart/form-data; boundary=xXXxXXyYYzzz',
             'host': 'httpbin.org',
             'user-agent': 'Mocket',
             'connection': 'keep-alive',
         }
     )
Exemplo n.º 4
0
    async def test_http_session(self):
        url = "http://httpbin.org/ip"
        body = "asd" * 100
        Entry.single_register(Entry.GET, url, body=body, status=404)
        Entry.single_register(Entry.POST, url, body=body * 2, status=201)

        async with aiohttp.ClientSession() as session:
            with async_timeout.timeout(3):
                async with session.get(url) as get_response:
                    assert get_response.status == 404
                    assert await get_response.text() == body

            with async_timeout.timeout(3):
                async with session.post(url, data=body * 6) as post_response:
                    assert post_response.status == 201
                    assert await post_response.text() == body * 2
                    assert Mocket.last_request().method == "POST"
                    assert Mocket.last_request().body == body * 6

        self.assertEqual(len(Mocket.request_list()), 2)
    def test_client_unsubscribes_to_event(self):

        ack = C.ACTIONS_ACK + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_ACK + C.MESSAGE_SEPARATOR
        unsubscribe_ack = C.TOPIC_EVENT + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_ACK + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_UNSUBSCRIBE + C.MESSAGE_PART_SEPARATOR + "test1" + C.MESSAGE_SEPARATOR
        unsubscribe = C.TOPIC_EVENT + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_UNSUBSCRIBE + C.MESSAGE_PART_SEPARATOR + "test1" + C.MESSAGE_SEPARATOR
        Mocket.register(MocketEntry(('localhost', 8989), [str.encode(ack), str.encode(unsubscribe_ack)]))
        ds = DeepStreamClient('localhost', 8989)
        ds.login({"username": "******", "password": "******"}, None)
        time.sleep(0.1)
        ds.event.unsubscribe("test1", None)
        time.sleep(0.1)
        assert Mocket.last_request() == (str(str.encode(unsubscribe)))
Exemplo n.º 6
0
 def test_client_sends_login_credentials(self):
     """
     Scenario: The client sends login credentials
         Given the test server is ready
             And the client is initialised
         When the client logs in with username "XXX" and password "YYY"
         Then the last message the server recieved is A|REQ|{"username":"******","password":"******"}+
             And the clients connection state is "AUTHENTICATING"
     """
     Mocket.register(MocketEntry(('localhost', 8989), 0))
     ds = DeepStreamClient('localhost', 8989)
     ds.login({"username": "******", "password": "******"}, None)
     time.sleep(0.1)
     auth_msg = C.TOPIC_AUTH + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_REQUEST + C.MESSAGE_PART_SEPARATOR + "{\"password\": \"YYY\", \"username\": \"XXX\"}" + C.MESSAGE_SEPARATOR
     assert (Mocket.last_request()) == (str(str.encode(auth_msg)))
     assert ds.get_connection_state() == C.CONNECTION_STATE_AUTHENTICATING
 def test_client_subscribes_to_event(self):
     """
     Scenario: The client subscribes to an event
         Given the client subscribes to an event named "test1"
         Then the last message the server received is E|S|test1+
     	Then the server sends the message E|A|S|test1+
     """
     ack = C.ACTIONS_ACK + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_ACK + C.MESSAGE_SEPARATOR
     subscribe_ack = C.TOPIC_EVENT + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_ACK + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_SUBSCRIBE + C.MESSAGE_PART_SEPARATOR + "test1" + C.MESSAGE_SEPARATOR
     subscribe = C.TOPIC_EVENT + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_SUBSCRIBE + C.MESSAGE_PART_SEPARATOR + "test1" + C.MESSAGE_SEPARATOR
     Mocket.register(MocketEntry(('localhost', 8989), [str.encode(ack), str.encode(subscribe_ack)]))
     ds = DeepStreamClient('localhost', 8989)
     ds.login({"username": "******", "password": "******"}, None)
     time.sleep(0.1)
     ds.event.subscribe("test1", None)
     time.sleep(0.1)
     assert Mocket.last_request() == (str(str.encode(subscribe)))
 def test_client_listens_to_event_prefix(self):
     '''
     Scenario: The client listens to eventPrefix
         When the client listens to events matching "eventPrefix/.*"
         Then the last message the server received is E|L|eventPrefix/.*+
         Then the server sends the message E|A|L|eventPrefix/.*+
     '''
     ack = C.ACTIONS_ACK + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_ACK + C.MESSAGE_SEPARATOR
     listen_ack = C.TOPIC_EVENT + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_ACK + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_LISTEN + C.MESSAGE_PART_SEPARATOR + "regex/\*" + C.MESSAGE_SEPARATOR
     listen = C.TOPIC_EVENT + C.MESSAGE_PART_SEPARATOR + C.ACTIONS_LISTEN + C.MESSAGE_PART_SEPARATOR + "regex/\*" + C.MESSAGE_SEPARATOR
     Mocket.register(MocketEntry(('localhost', 8989), [str.encode(ack), str.encode(listen_ack)]))
     ds = DeepStreamClient('localhost', 8989)
     ds.login({"username": "******", "password": "******"}, None)
     time.sleep(0.1)
     ds.event.listen("regex/\*", None)
     time.sleep(0.1)
     assert Mocket.last_request() == (str(str.encode(listen)))
Exemplo n.º 9
0
 def test_collect(self):
     request = 'GET /get/p/?b=2&a=1 HTTP/1.1\r\nAccept-Encoding: identity\r\nHost: testme.org\r\nConnection: close\r\nUser-Agent: Python-urllib/2.6\r\n\r\n'
     Mocket.collect(request)
     self.assertEqual(Mocket.last_request(), request)
     self.assertEqual(Mocket._requests, [request])
Exemplo n.º 10
0
 def test_err(self):
     Entry.register_response('INCRBY counter one', ERROR('ERR value is not an integer or out of range'))
     self.assertRaises(redis.ResponseError, self.rclient.incr, 'counter', 'one')
     self.assertEqual(len(Mocket._requests), 1)
     self.assertEqual(Mocket.last_request().data, b'*3\r\n$6\r\nINCRBY\r\n$7\r\ncounter\r\n$3\r\none\r\n')
Exemplo n.º 11
0
 def test_lastrequest(self):
     self.assertEqual(Mocket.last_request(), None)
     Mocket._requests.extend([1, 2, 3])
     self.assertEqual(Mocket.last_request(), 3)
Exemplo n.º 12
0
 def test_get_unicode(self):
     Entry.register_response('GET snowman', '\u2603')
     self.assertEqual(self.rclient.get('snowman'), b'\xe2\x98\x83')
     self.assertEqual(len(Mocket._requests), 1)
     self.assertEqual(Mocket.last_request().data, b'*2\r\n$3\r\nGET\r\n$7\r\nsnowman\r\n')
Exemplo n.º 13
0
 def test_lrange(self):
     l = [b'one', b'two', b'three']
     Entry.register_response('LRANGE list 0 -1', l)
     self.assertEqual(self.rclient.lrange('list', 0, -1), l)
     self.assertEqual(len(Mocket._requests), 1)
     self.assertEqual(Mocket.last_request().data, b'*4\r\n$6\r\nLRANGE\r\n$4\r\nlist\r\n$1\r\n0\r\n$2\r\n-1\r\n')
Exemplo n.º 14
0
 def test_set(self):
     Entry.register_response('SET mocket "is awesome!"', OK)
     self.assertTrue(self.rclient.set('mocket', 'is awesome!'))
     self.assertEqual(len(Mocket._requests), 1)
     self.assertEqual(Mocket.last_request().data, b'*3\r\n$3\r\nSET\r\n$6\r\nmocket\r\n$11\r\nis awesome!\r\n')
Exemplo n.º 15
0
 def test_set(self):
     Entry.register_response('SET mocket "is awesome!"', OK)
     self.assertTrue(self.rclient.set('mocket', 'is awesome!'))
     self.assertEqual(len(Mocket._requests), 1)
     self.assertEqual(Mocket.last_request().data, b'*3\r\n$3\r\nSET\r\n$6\r\nmocket\r\n$11\r\nis awesome!\r\n')
Exemplo n.º 16
0
 def test_get_unicode(self):
     Entry.register_response('GET snowman', '\u2603')
     self.assertEqual(self.rclient.get('snowman'), b'\xe2\x98\x83')
     self.assertEqual(len(Mocket._requests), 1)
     self.assertEqual(Mocket.last_request().data, b'*2\r\n$3\r\nGET\r\n$7\r\nsnowman\r\n')
Exemplo n.º 17
0
 def test_lrange(self):
     l = [b'one', b'two', b'three']
     Entry.register_response('LRANGE list 0 -1', l)
     self.assertEqual(self.rclient.lrange('list', 0, -1), l)
     self.assertEqual(len(Mocket._requests), 1)
     self.assertEqual(Mocket.last_request().data, b'*4\r\n$6\r\nLRANGE\r\n$4\r\nlist\r\n$1\r\n0\r\n$2\r\n-1\r\n')
Exemplo n.º 18
0
 def test_err(self):
     Entry.register_response('INCRBY counter one', ERROR('ERR value is not an integer or out of range'))
     self.assertRaises(redis.ResponseError, self.rclient.incr, 'counter', 'one')
     self.assertEqual(len(Mocket._requests), 1)
     self.assertEqual(Mocket.last_request().data, b'*3\r\n$6\r\nINCRBY\r\n$7\r\ncounter\r\n$3\r\none\r\n')