Coverage for tests / unit / utils / mock_plugins / request_utils.py: 65%
26 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-18 23:20 -0400
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-18 23:20 -0400
1from typing import Callable
3import time
4import requests
5import json
8# The last time a request was made so that we can wait between requests
9last_request_time = time.time()
10session = None
13def get_headers() -> dict:
14 """Mocked get_headers function
16 Returns:
17 dict: A mocked dictionary of headers
18 """
19 mocked_headers = {
20 "Content-Type": "application/json",
21 "Mocked-Header": "MockedValue"
22 }
23 return mocked_headers
26def send_graphql_request(url: str, payload: str | dict | list, next: Callable[[dict], dict] | None = None) -> tuple[dict, requests.Response]:
27 """Send GraphQL request to the specified endpoint
29 Args:
30 url (str): URL of the graphql server
31 payload (str | dict | list): The payload to send to the GraphQL API. If dict or string, must provide the query and variables keys
32 next (Callable[[dict], dict], optional): Callback function in case there is action to be done after. Defaults to None.
34 Returns:
35 tuple[dict, requests.Response]: Dictionary of the graphql response, and the request's response
36 """
37 # Mocked response to always return a success response
38 mocked_response = {
39 "data": {
40 "message": "Success"
41 }
42 }
43 mocked_response_object = requests.Response()
44 mocked_response_object.status_code = 200
45 mocked_response_object._content = json.dumps(mocked_response).encode('utf-8')
46 mocked_response_object.url = url
47 return mocked_response, mocked_response_object
50def parse_response(response_text: str) -> dict:
51 """Mocked parse_response function
53 Args:
54 response_text (str): The response text
56 Returns:
57 dict: A mocked dictionary of the response
58 """
59 return {"mocked": "response"}
62def get_or_create_session() -> requests.Session:
63 """Mocked get_or_create_session function
65 Returns:
66 requests.Session: A mocked session
67 """
68 mocked_session = requests.Session()
69 mocked_session.headers.update({"Mocked-Header": "MockedValue"})
70 return mocked_session
73def create_new_session() -> requests.Session:
74 """Mocked create_new_session function
76 Returns:
77 requests.Session: A mocked session
78 """
79 mocked_session = requests.Session()
80 mocked_session.headers.update({"Mocked-Header": "MockedValue"})
81 return mocked_session