0

I am using python requests to to access some apis, recently I learned requests_mock for mocking http responses for testing. The responses from api that I am using are quire large

adapter.register_uri('GET', 'http://api.gateway/payment/, text='VERY LARGE TEXT')

What is the proper way of passing large response text?

Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53

1 Answers1

1

There's not really a magic here, at the end of the day unless you're doing streaming requests is going to load all the data up into memory anyway.

If you're really worried about the memory (and it's python, you probably shouldn't) you could always load from a file on demand or something, but then i think it's much more about how you want to structure your tests than something requests_mock will help you with.

import requests
import requests_mock


def fileLoader(filename):
    def loader(req, context):
        with open(filename, 'r') as f:
            return f.read()

    return loader


with requests_mock.mock() as m:
    m.get('http://test.com', text=fileLoader('abc.txt'))
    resp = requests.get('http://test.com')
    assert resp.text.strip() == 'ABC'
jamielennox
  • 368
  • 1
  • 2
  • 9