Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Python 2 Unittest Request.content that is a .gz file in memory?

I am currently writing a piece of code that downloads a json gz file into memory/io, unzips it, and turns it into a dict. Here is a sample code (had to find a random json.gzip that was safe for others to test on)

response = requests.get('https://nvd.nist.gov/feeds/json/cve/1.0/nvdcve-1.0-2003.json.gz')
compressed_file = io.BytesIO(response.content)
json_data = json.loads(gzip.GzipFile(fileobj=compressed_file).read())

This issue is, I have no idea how to "mock" the response.content to be a gzip file. The host that does the unittesting lacks access to the internet. So I need to load a .gz file from the host, and place it as .gz so it can be loaded into memory to be decompressed and converted.

I looked at something like this, but I don't see how to apply it to be a .gz file.

def mock_response(payload, status_code):
    mock_response = mock.MagicMock(spec=requests.Response)
    mock_response.text = payload
    mock_response.status_code = status_code

So I am looking for some assistance on how to mock the response.content to be a .gz file.

This is some legacy code I am trying to improve so I am also sort of stuck on Py2

Comments