SarahJessica
SarahJessica

Reputation: 524

Python 3 Pytest: How to mock request urlopen response and headers?

I have broken my code down into separate functions for unit testing. I am trying to pick up Pytest testing framework and so far only know the basics.

I don't actually want to send a request to the internet -- this would be a fruitless test since I'd only be testing the urllib library anyway. I do, however, want to test how the response is handled.

I have this function to make the request

def request_url(url):
    return request.urlopen(url)

And then I am checking the content type:

def get_document_type(req):
    """ checks if document is html or plain text """
    doc_type = req.info().get_content_type()
    if doc_type == "text/html":
        return "html"
    elif doc_type == "text/plain":
        return "txt"
    else: 
        return error_message["unsupported_document_type"]

Then I will need to test and I will need to mock each outcome. If this was Node.js I could use something like rewire or a sinon stub.

def get_content(req):
    doc_type_response = get_document_type(req)
    if doc_type_response == "html":
        # handle html content
    elif get_type_response == "txt":
        # handle plain text
    else:
        return doc_type_response

This unit test works but I don't want to make the real call.

def test_request_url():
    url = request_url(url_to_try).info().get_content_type() 
    assert url == "text/plain"

Does anybody know the best way to do this please?

Upvotes: 3

Views: 8089

Answers (1)

Dmytro
Dmytro

Reputation: 41

There is a package requests-mock https://pypi.org/project/requests-mock/ which can be used for mocking API calls but for requests library. This is an example with headers/text

import unittest
import requests_mock
import requests

class TestImplementations(unittest.TestCase):

    @requests_mock.mock()
    def test_get_project(self, mock_for_requests):
        API_URL = "https://someapi.com"

        #mocking your request(s)
        expected_headers = {'Content-Type': 'text/plain'}
        expected = 'some_text'
        mock_for_requests.get(API_URL + "/someendpoint", headers=expected_headers, text=expected)

        #running your code with requests
        response = requests.get(API_URL + "/someendpoint")

        #comparing output
        self.assertEqual(response.headers, expected_headers)
        self.assertEqual(response.text, expected)

Upvotes: 4

Related Questions