77
88import email .utils
99from collections .abc import Callable
10- from http import HTTPMethod
11- from typing import TYPE_CHECKING
10+ from http import HTTPMethod , HTTPStatus
11+
12+ from requests .models import PreparedRequest
1213
1314from mock_vws ._mock_common import Route
1415from mock_vws ._query_tools import (
2122from mock_vws .image_matchers import ImageMatcher
2223from mock_vws .target_manager import TargetManager
2324
24- if TYPE_CHECKING :
25- from requests_mock .request import Request
26- from requests_mock .response import Context
27-
28-
2925_ROUTES : set [Route ] = set ()
3026
31- _ResponseType = str
27+ _ResponseType = tuple [ int , dict [ str , str ], str ]
3228
3329
3430def route (
@@ -70,18 +66,22 @@ def decorator(
7066 return decorator
7167
7268
73- def _body_bytes (request : "Request" ) -> bytes :
69+ def _body_bytes (request : PreparedRequest ) -> bytes :
7470 """
7571 Return the body of a request as bytes.
7672 """
77- return request .body or b""
73+ if request .body is None :
74+ return b""
75+
76+ assert isinstance (request .body , bytes )
77+ return request .body
7878
7979
8080class MockVuforiaWebQueryAPI :
8181 """
8282 A fake implementation of the Vuforia Web Query API.
8383
84- This implementation is tied to the implementation of `requests_mock `.
84+ This implementation is tied to the implementation of ``responses` `.
8585 """
8686
8787 def __init__ (
@@ -103,28 +103,26 @@ def __init__(
103103 self ._query_match_checker = query_match_checker
104104
105105 @route (path_pattern = "/v1/query" , http_methods = {HTTPMethod .POST })
106- def query (self , request : "Request" , context : "Context" ) -> _ResponseType :
106+ def query (self , request : PreparedRequest ) -> _ResponseType :
107107 """
108108 Perform an image recognition query.
109109 """
110110 try :
111111 run_query_validators (
112- request_path = request .path ,
112+ request_path = request .path_url ,
113113 request_headers = request .headers ,
114114 request_body = _body_bytes (request = request ),
115- request_method = request .method ,
115+ request_method = request .method or "" ,
116116 databases = self ._target_manager .databases ,
117117 )
118118 except ValidatorError as exc :
119- context .headers = exc .headers
120- context .status_code = exc .status_code
121- return exc .response_text
119+ return exc .status_code , exc .headers , exc .response_text
122120
123121 response_text = get_query_match_response_text (
124122 request_headers = request .headers ,
125123 request_body = _body_bytes (request = request ),
126- request_method = request .method ,
127- request_path = request .path ,
124+ request_method = request .method or "" ,
125+ request_path = request .path_url ,
128126 databases = self ._target_manager .databases ,
129127 query_match_checker = self ._query_match_checker ,
130128 )
@@ -134,11 +132,11 @@ def query(self, request: "Request", context: "Context") -> _ResponseType:
134132 localtime = False ,
135133 usegmt = True ,
136134 )
137- context . headers = {
135+ headers = {
138136 "Connection" : "keep-alive" ,
139137 "Content-Type" : "application/json" ,
140138 "Server" : "nginx" ,
141139 "Date" : date ,
142140 "Content-Length" : str (len (response_text )),
143141 }
144- return response_text
142+ return HTTPStatus . OK , headers , response_text
0 commit comments