Skip to main content

Mock requests in python3

Project description

reqMock

Mock requests library like HTTPToolkit in python3

Installation

pip install reqMock

Example:

Original code

import requests
print(requests.get("https://google.com").text)

Import in first line of code

from reqMock import mockControl as mock

Enable mock

# To enable mock
mock.set(enable = True)

# To disable mock
mock.set(enable = False)

Mock https://google.com to another website

mock.add(
	url = "https://google.com",	# mock 'from'
	method = "GET",			# method 'from'
	mockCheck = "url",			# method mock ('host', 'url', 'match')
	mockMethod = "url",			# mock to 'url' or 'text'
	to = "https://youtube.com",	# data
	# headers = None, 			# change header of this request
	# data = None, 				# change data of this request
	# status_code = 200 		# change status code of this request
)

Mock https://youtube.com to custom result

mock.add(
	url = "https://youtube.com",
	method = "GET",	
	mockCheck = "url",	
	mockMethod = "text",
	to = "Hello World!",
	# headers = None, 
	# data = None, 
	# status_code = 200
)

Enable showQuery to print info from requests

mock.set(showQuery = True)		# Default is False

# You will see output requests like this 
# ">> requests: GET: https://google.com (headers={'abc': 'def'})" and below is result output

Enable showResult to print result output from requests

mock.set(showResult = True)		# Default is False

# You will see output requests like this 
# ">> result: GET: https://google.com" and below is result output

Enable showTrace to print call stack trace

mock.set(showTrace = True)		# Default is False

# You will see detailed call stack trace like this:
# >> trace:
#    1. <module> of <main> at line 10 -> main()
#    2. main() of <main> at line 5 -> result = func_a()
#    3. func_a(
#           param1='test_value',
#           param2='custom_value'
#       ) of <main> at line 2 -> return func_b()
#    4. func_b(
#           arg1='test_value',
#           arg2='custom_value'
#       ) of <main> at line 3 -> return requests.get("https://httpbin.org/get")
#    5. requests.get(url='https://httpbin.org/get', params={'key': 'value'})

Redirect output of show to file

mock.set(stdout = open('out.txt', 'w'))

# Output of 'show' option will write to file instead of write to output

If you want to rename this module for hide from check

# Open a python3 shell
from reqMock import mockControl as mock

# Example change module name from 'reqMock' to 'Khanh'
mock.set(moduleName = "Khanh")
# Choose 'y' and new name will applied in new session

# For new session, import it by using new name
from Khanh import mockControl as mock

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reqmock-0.0.0.5.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reqmock-0.0.0.5-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file reqmock-0.0.0.5.tar.gz.

File metadata

  • Download URL: reqmock-0.0.0.5.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for reqmock-0.0.0.5.tar.gz
Algorithm Hash digest
SHA256 69028c3a0d5c8014788cf3d0a047ac66e8889b6e83089648d39f60433225d5fe
MD5 13c7e7ac35fd5e5418ef3274487ad7f0
BLAKE2b-256 161b772ee7e7ef1521eb308a49760461ffc2b32b1877da6972a9fe912a34ca65

See more details on using hashes here.

File details

Details for the file reqmock-0.0.0.5-py3-none-any.whl.

File metadata

  • Download URL: reqmock-0.0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for reqmock-0.0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 ab3db9c7e01a6c260337df541098722c7306362e5de772da195f79dfe157f52b
MD5 36dfb4d575746b5ff3c2c3ae358dd699
BLAKE2b-256 93fcca128aea0e46576f1f70a15255e6af0cf326b2957b3b6463d446a89428ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page