I'm writing unit tests for a requests library. For most of my libraries, I run a .content.decode('utf-8') on the response to get to the actual text. However when I'm mocking this response from the api call, how do I mock a response object which is received by the requests call? is there a requests.content.encode('utf-8',data)) look alike process which can actually mock the response coming in (in encoded format) from the API call, and then I decode that object.
sample code:
def sample_fct(self, endpoint): try: request = requests.post (endpoint, verify=False) except requests.exceptions.RequestException as e: raise return request def get_something(self,test): try: response = self.sample_fct(test) resp_text = response_bare.content.decode('utf-8') print resp_text except: raise So for instance, if I wanted to unit test the get_something function, I need to mock the sample_fct function. to do this, i would have to set the sample_fct.return_value to a request object that it is returning.
So how do I create that object.
requestscalls.b'\xE2\x98\xBA'or by starting with the encoded string and callencodelike this:'☺'.encode('utf-8')requests.content.encode('utf-8',data))do?Mock, so it'd be something like:mock_fct_return = mock.Mock(content=b'some content you want to decode')