How to use the mocket.mockhttp.Entry.single_register function in mocket

To help you get started, we’ve selected a few mocket examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mindflayer / python-mocket / tests / main / test_http.py View on Github external
def test_sendall(self):
        with mock.patch("time.gmtime") as tt:
            tt.return_value = time.struct_time((2013, 4, 30, 10, 39, 21, 1, 120, 0))
            Entry.single_register(
                Entry.GET, "http://testme.org/get/p/?a=1&b=2", body="test_body"
            )
        resp = urlopen("http://testme.org/get/p/?b=2&a=1", timeout=10)
        self.assertEqual(resp.code, 200)
        self.assertEqual(resp.read(), b"test_body")
        self.assertEqualHeaders(
            dict(resp.headers),
            {
                "Status": "200",
                "Content-length": "9",
                "Server": "Python/Mocket",
                "Connection": "close",
                "Date": "Tue, 30 Apr 2013 10:39:21 GMT",
                "Content-type": "text/plain; charset=utf-8",
            },
        )
github hasgeek / flask-lastuser / tests / test_mergeuser.py View on Github external
def test_user_get_userid(self):
        # Handler for `user2 = User.get(...)`
        Entry.single_register(
            Entry.POST,
            self.lastuser.endpoint_url(
                current_app.lastuser_config['getuser_userid_endpoint']
            ),
            body=json.dumps(
                {
                    "status": "ok",
                    "type": "user",
                    "buid": "1234567890123456789012",
                    "userid": "1234567890123456789012",
                    "name": "user1",
                    "title": "User 1",
                    "label": "User 1 (@user1)",
                    "oldids": ['0987654321098765432109'],
                    "timezone": "Asia/Kolkata",
                }
github mindflayer / python-mocket / tests / tests35 / test_http_aiohttp.py View on Github external
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 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

        loop = asyncio.get_event_loop()
github mindflayer / python-mocket / tests / main / test_http.py View on Github external
def test_sendall_json(self):
        with mock.patch("time.gmtime") as tt:
            tt.return_value = time.struct_time((2013, 4, 30, 10, 39, 21, 1, 120, 0))
            Entry.single_register(
                Entry.GET,
                "http://testme.org/get?a=1&b=2#test",
                body='{"a": "€"}',
                headers={"content-type": "application/json"},
            )

        response = urlopen("http://testme.org/get?b=2&a=1#test", timeout=10)
        self.assertEqual(response.code, 200)
        self.assertEqual(response.read(), b'{"a": "\xe2\x82\xac"}')
        self.assertEqualHeaders(
            dict(response.headers),
            {
                "status": "200",
                "content-length": "12",
                "server": "Python/Mocket",
                "connection": "close",
github mindflayer / python-mocket / tests / main / test_http.py View on Github external
def test_post_file_object(self):
        url = "http://github.com/fluidicon.png"
        Entry.single_register(Entry.POST, url, status=201)
        file_obj = open("tests/fluidicon.png", "rb")
        files = {"content": file_obj}
        r = requests.post(url, files=files, data={}, verify=False)
        self.assertEqual(r.status_code, 201)
github mindflayer / python-mocket / tests / main / test_http.py View on Github external
def test_same_url_different_methods(self):
        url = "http://bit.ly/fakeurl"
        response_to_mock = {"content": 0, "method": None}
        responses = []
        methods = [Entry.PUT, Entry.GET, Entry.POST]

        for m in methods:
            response_to_mock["method"] = m
            Entry.single_register(m, url, body=json.dumps(response_to_mock))
            response_to_mock["content"] += 1
        for m in methods:
            responses.append(requests.request(m, url).json())

        methods_from_responses = [r["method"] for r in responses]
        contents_from_responses = [r["content"] for r in responses]
        self.assertEqual(methods, methods_from_responses)
        self.assertEqual(list(range(len(methods))), contents_from_responses)
github mindflayer / python-mocket / tests / main / test_http.py View on Github external
def test_file_object(self):
        url = "http://github.com/fluidicon.png"
        filename = "tests/fluidicon.png"
        file_obj = open(filename, "rb")
        Entry.single_register(Entry.GET, url, body=file_obj)
        r = requests.get(url)
        remote_content = r.content
        local_file_obj = open(filename, "rb")
        local_content = local_file_obj.read()
        self.assertEqual(remote_content, local_content)
        self.assertEqual(len(remote_content), len(local_content))
        self.assertEqual(int(r.headers["Content-Length"]), len(local_content))
        self.assertEqual(r.headers["Content-Type"], "image/png")
github mindflayer / python-mocket / tests / main / test_https.py View on Github external
def test_json(response):
    url_to_mock = 'https://testme.org/json'

    Entry.single_register(
        Entry.GET,
        url_to_mock,
        body=json.dumps(response),
        headers={'content-type': 'application/json'})

    mocked_response = requests.get(url_to_mock).json()
    assert response == mocked_response

    mocked_response = json.loads(urlopen(url_to_mock).read().decode('utf-8'))
    assert response == mocked_response