Esempio n. 1
0
async def test_correct_case_insensitive(bot):
    html = content("")
    with mock.patch(URLOPEN, return_value=MockResponse(data=html)):
        clazz = Typos(bot, start_tasks=False)
        clazz.corrections = {"poo": ["oops"]}
        correction = clazz.correct("PoO")
        assert correction == "oops"
def test_mapping_get():
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse()

        @RestClient(url="https://reqres.in")
        class Test():
            @Mapping(url="/api/users?page=2",method="GET")
            def get_users(self,*args,**kargs):
                pass
        logger.info("Test here")    
        def timestamp_middleware(request,response):
            if response is None:
                headers = request.get("headers")
                headers.update({
                    "timestamp1" : "121342423423"
                })
                logger.info({
                    "message":"in timestamp middleware",
                    "request":request
                })
            else:
                logger.info({
                    "message":"in timestamp middleware",
                    "response":response
                })
        RestClient.middlewares.append(timestamp_middleware)

        testClient = Test()
        response = testClient.get_users()
        logger.info(response)
        assert response.get("body",None) != None
def test_mapping_post_json():
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse()

        @RestClient(url="https://reqres.in")
        class Test():
            @Mapping(url="/api/users",method="POST")
            def create_user(self,*args,**kargs):
                self.json = {"message":"hi"}
        
        test_client = Test()
        response = test_client.create_user(1)
        assert response.get("body").get("message") == "Hello from the other side"
def test_mapping_dynamic_url():
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse()

        @RestClient(url="https://reqres.in")
        class Test():
            @Mapping(url="/api/users?page={page_number}",method="GET")
            def get_users(self,page_number,*args,**kargs):
                self._url = kargs.get("url")
        
        test_client = Test()
        response = test_client.get_users(1)
        assert test_client._url == "https://reqres.in/api/users?page=1"
def test_mapping_url_method():
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse()

        @RestClient(url="https://reqres.in")
        class Test():
            @Mapping(url="/api/users?page=2",method="GET")
            def get_users(self,*args,**kargs):
                pass

        test_client = Test()
        response = test_client.get_users()
        assert response.get("body").get("message") == "Hello from the other side"
def test_restclient_geturl():
    """Test for getUrl method
    """
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse(error_code=500)

        @RestClient(url="https://reqres.in")
        class Test():
            """ Test client
            """
            pass

        test_client = Test()
        assert test_client.getUrl() == "https://reqres.in"
def test_mapping_post_xml():
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse(content_type="xml")

        @RestClient(url="https://reqres.in")
        class Test():
            @Mapping(url="/api/users",method="POST")
            def create_user(self,*args,**kargs):
                self.data = "<xml><user></user></xml>"
        
        test_client = Test()
        test_client.addHeader("Content-Type","text/xml")
        response = test_client.create_user(1)
        assert response.get("body") == "<xml><message>Hello from the other side</message></xml>"
def test_restclient_json_content_type():
    """Test for json content type
    """
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse()

        @RestClient(url="https://reqres.in")
        class Test():
            """ Test Client
            """
            pass

        test_client = Test()
        response = test_client.send(url="https://abc.com/users", method="GET")
        assert response.get("body").get(
            "message") == "Hello from the other side"
def test_restclient_finalize():
    """Test for finalize method
    """
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse()

        @RestClient(url="https://reqres.in")
        class Test():
            """TestClient
            """
            def finalize(self):
                self.headers["trackingId"] = "3456"

        test_client = Test()
        response = test_client.send(url="https://abc.com/users", method="GET")
        assert test_client.headers.get("trackingId") == "3456"
def test_restclient_add_middleware():
    """Test for add_middleware method
    """
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse()

        @RestClient(url="https://reqres.in")
        class Test():
            """ Test client
            """
            pass

        def add_timestamp_middleware(request, response):
            headers = request.get("headers")
            headers["timestamp"] = "12345"
        test_client = Test()
        test_client.add_middleware(add_timestamp_middleware)
        response = test_client.send(url="https://abc.com/users", method="GET")
        assert test_client.headers.get("timestamp") == "12345"
def test_restclient_500_error():
    """Test for 500 error
    """
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse(error_code=500)

        @RestClient(url="https://reqres.in")
        class Test():
            """ Test client
            """
            pass

        test_client = Test()
        exception = False
        try:
            response = test_client.send(
                url="https://abc.com/users", method="GET")
        except HTTPError as e:
            exception = True
        assert exception == True
def test_mapping_query_string():
    """test query string set manually
    """
    with mock.patch("requests.request") as mock_get:
        mock_get.return_value = MockResponse()

        @RestClient(url="https://reqres.in")
        class Test():
            @Mapping(url="/api/users",method="GET")
            def get_users(self,page_number,*args,**kargs):
                self.queryString = "page=4"
        
        test_client = Test()
        def send_decorator(func_obj):
            def new_send(*args,**kargs):
                self = test_client
                self.kargs = kargs
                func_obj(*args,**kargs)
            return new_send
        test_client.send = send_decorator(test_client.send)
        response = test_client.get_users(1)
        assert test_client.kargs.get("url") == "https://reqres.in/api/users?page=4"
Esempio n. 13
0
def mocked_requests_get(*args, **kwargs):
    return MockResponse(args[0], int(args[1]))
Esempio n. 14
0
async def test_get_wiki_corrections(bot):
    html = content("poo->oops")
    with mock.patch(URLOPEN, return_value=MockResponse(data=html)):
        clazz = Typos(bot, start_tasks=False)
        corrections = clazz.get_wiki_corrections()
        assert corrections == {"poo": ["oops"]}