|
63 | 63 | server_thread = None |
64 | 64 | server_instances = [] |
65 | 65 | shutdown_event = threading.Event() |
| 66 | +UNSET = object() |
66 | 67 |
|
67 | 68 |
|
68 | 69 | def _sleep_interruptible(seconds): |
@@ -115,51 +116,51 @@ def reset_http_server_state(): |
115 | 116 | ) |
116 | 117 |
|
117 | 118 |
|
118 | | -def configure_http_response(*, status_code=None, body=None, content_type=None, |
119 | | - delay_seconds=None, stream_fragments=None, |
120 | | - fragment_delay_seconds=None, |
121 | | - hang_before_response=None, |
122 | | - hang_after_fragment_index=None): |
123 | | - if status_code is not None: |
| 119 | +def configure_http_response(*, status_code=UNSET, body=UNSET, content_type=UNSET, |
| 120 | + delay_seconds=UNSET, stream_fragments=UNSET, |
| 121 | + fragment_delay_seconds=UNSET, |
| 122 | + hang_before_response=UNSET, |
| 123 | + hang_after_fragment_index=UNSET): |
| 124 | + if status_code is not UNSET: |
124 | 125 | response_config["status_code"] = status_code |
125 | | - if body is not None: |
| 126 | + if body is not UNSET: |
126 | 127 | response_config["body"] = body |
127 | | - if content_type is not None: |
| 128 | + if content_type is not UNSET: |
128 | 129 | response_config["content_type"] = content_type |
129 | | - if delay_seconds is not None: |
| 130 | + if delay_seconds is not UNSET: |
130 | 131 | response_config["delay_seconds"] = delay_seconds |
131 | | - if stream_fragments is not None: |
132 | | - response_config["stream_fragments"] = list(stream_fragments) |
133 | | - if fragment_delay_seconds is not None: |
| 132 | + if stream_fragments is not UNSET: |
| 133 | + response_config["stream_fragments"] = None if stream_fragments is None else list(stream_fragments) |
| 134 | + if fragment_delay_seconds is not UNSET: |
134 | 135 | response_config["fragment_delay_seconds"] = fragment_delay_seconds |
135 | | - if hang_before_response is not None: |
| 136 | + if hang_before_response is not UNSET: |
136 | 137 | response_config["hang_before_response"] = hang_before_response |
137 | | - if hang_after_fragment_index is not None: |
| 138 | + if hang_after_fragment_index is not UNSET: |
138 | 139 | response_config["hang_after_fragment_index"] = hang_after_fragment_index |
139 | 140 |
|
140 | 141 |
|
141 | | -def configure_oauth_token_response(*, status_code=None, body=None, |
142 | | - content_type=None, |
143 | | - delay_seconds=None, |
144 | | - hang_before_response=None, |
145 | | - stream_fragments=None, |
146 | | - fragment_delay_seconds=None, |
147 | | - hang_after_fragment_index=None): |
148 | | - if status_code is not None: |
| 142 | +def configure_oauth_token_response(*, status_code=UNSET, body=UNSET, |
| 143 | + content_type=UNSET, |
| 144 | + delay_seconds=UNSET, |
| 145 | + hang_before_response=UNSET, |
| 146 | + stream_fragments=UNSET, |
| 147 | + fragment_delay_seconds=UNSET, |
| 148 | + hang_after_fragment_index=UNSET): |
| 149 | + if status_code is not UNSET: |
149 | 150 | oauth_token_response["status_code"] = status_code |
150 | | - if body is not None: |
| 151 | + if body is not UNSET: |
151 | 152 | oauth_token_response["body"] = body |
152 | | - if content_type is not None: |
| 153 | + if content_type is not UNSET: |
153 | 154 | oauth_token_response["content_type"] = content_type |
154 | | - if delay_seconds is not None: |
| 155 | + if delay_seconds is not UNSET: |
155 | 156 | oauth_token_response["delay_seconds"] = delay_seconds |
156 | | - if hang_before_response is not None: |
| 157 | + if hang_before_response is not UNSET: |
157 | 158 | oauth_token_response["hang_before_response"] = hang_before_response |
158 | | - if stream_fragments is not None: |
159 | | - oauth_token_response["stream_fragments"] = list(stream_fragments) |
160 | | - if fragment_delay_seconds is not None: |
| 159 | + if stream_fragments is not UNSET: |
| 160 | + oauth_token_response["stream_fragments"] = None if stream_fragments is None else list(stream_fragments) |
| 161 | + if fragment_delay_seconds is not UNSET: |
161 | 162 | oauth_token_response["fragment_delay_seconds"] = fragment_delay_seconds |
162 | | - if hang_after_fragment_index is not None: |
| 163 | + if hang_after_fragment_index is not UNSET: |
163 | 164 | oauth_token_response["hang_after_fragment_index"] = hang_after_fragment_index |
164 | 165 |
|
165 | 166 |
|
@@ -284,7 +285,16 @@ def oauth_token(): |
284 | 285 | return Response(status=503) |
285 | 286 | if oauth_token_response["stream_fragments"] is not None: |
286 | 287 | return _build_streaming_response(oauth_token_response) |
287 | | - return jsonify(oauth_token_response["body"]), oauth_token_response["status_code"] |
| 288 | + |
| 289 | + body = oauth_token_response["body"] |
| 290 | + if isinstance(body, (dict, list)) and oauth_token_response["content_type"] is None: |
| 291 | + return jsonify(body), oauth_token_response["status_code"] |
| 292 | + |
| 293 | + return Response( |
| 294 | + body, |
| 295 | + status=oauth_token_response["status_code"], |
| 296 | + content_type=oauth_token_response.get("content_type"), |
| 297 | + ) |
288 | 298 |
|
289 | 299 |
|
290 | 300 | @app.route('/ping', methods=['GET']) |
|
0 commit comments