def pretty_prepared_request(request, ctx): from requests.hooks import default_hooks kwargs = [ ('method', request.method), ('url', request.url), ] if request.headers: kwargs.append(('headers', request.headers)) if request.body is not None: count_bytes = len(request.body) count_display_bytes = 10 count_bytes = len(request.body) if count_bytes > count_display_bytes: truncated_body = comment( request.body[:count_display_bytes], '... and {} more bytes'.format(count_bytes - count_display_bytes)) else: truncated_body = request.body kwargs.append(('body', truncated_body)) if request.hooks != default_hooks(): kwargs.append(('hooks', request.hooks)) return pretty_call_alt(ctx, 'requests.PreparedRequest', kwargs=kwargs)
def pretty_response(resp, ctx): content_consumed = bool(resp._content_consumed) if not content_consumed: return comment( pretty_call_alt(ctx, 'requests.Response', kwargs=[('status_code', comment(resp.status_code, resp.reason)), ('url', resp.url), ('elapsed', resp.elapsed), ('headers', resp.headers)]), 'Response content not loaded yet') kwargs = [ ('url', resp.url), ('status_code', comment(resp.status_code, resp.reason)), ('elapsed', resp.elapsed), ('headers', resp.headers), ] has_valid_json_payload = False if resp.headers.get('Content-Type', 'text/plain').startswith('application/json'): try: data = resp.json() except ValueError: pass else: has_valid_json_payload = True kwargs.append(('json', comment(data, 'Access with .json()'))) if not has_valid_json_payload: text = resp.text count_chars_truncated = max(0, len(text) - MAX_CONTENT_CHARS) if count_chars_truncated: truncated = text[:MAX_CONTENT_CHARS] kwargs.append( ('text', comment( truncated, '{} characters truncated'.format(count_chars_truncated)))) else: kwargs.append(('text', text)) return pretty_call_alt(ctx, 'requests.Response', kwargs=kwargs)
def _strategy(draw): value = draw(strategy) add_trailing_comment = False if isinstance(value, (list, tuple, set)): add_trailing_comment = draw(st.booleans()) add_comment = draw(st.booleans()) if add_trailing_comment: comment_text = draw(st.text(alphabet='abcdefghijklmnopqrstuvwxyz #\\\'"')) value = trailing_comment(value, comment_text) if add_comment: comment_text = draw(st.text(alphabet='abcdefghijklmnopqrstuvwxyz #\\\'"')) value = comment(value, comment_text) return value
def test_gh_issue_59(): out = pformat({'a': comment('abcde' * 20, 'Comment'), 'b': 1}) assert out == """\