1
0
mirror of https://github.com/httpie/cli.git synced 2025-08-10 22:42:05 +02:00

Implement basic metrics layout & total elapsed time (#1250)

* Initial metadata processing

* Dynamic coloring and other stuff

* Use -vv / --meta

* More testing

* Cleanup

* Tweek message

Co-authored-by: Jakub Roztocil <jakub@roztocil.co>
This commit is contained in:
Batuhan Taskaya
2021-12-23 23:13:25 +03:00
committed by GitHub
parent e0e03f3237
commit f3b500119c
23 changed files with 334 additions and 94 deletions

View File

@@ -1,6 +1,7 @@
import os
import tempfile
import time
import requests
from unittest import mock
from urllib.request import urlopen
@@ -14,7 +15,7 @@ from httpie.downloads import (
from .utils import http, MockEnvironment
class Response:
class Response(requests.Response):
# noinspection PyDefaultArgument
def __init__(self, url, headers={}, status_code=200):
self.url = url

7
tests/test_meta.py Normal file
View File

@@ -0,0 +1,7 @@
from .utils import http
def test_meta_elapsed_time(httpbin, monkeypatch):
r = http('--meta', httpbin + '/get')
for line in r.splitlines():
assert 'Elapsed time' in r

View File

@@ -17,7 +17,7 @@ from httpie.cli.argtypes import (
)
from httpie.cli.definition import parser
from httpie.encoding import UTF8
from httpie.output.formatters.colors import get_lexer
from httpie.output.formatters.colors import PIE_STYLES, get_lexer
from httpie.status import ExitStatus
from .fixtures import XML_DATA_RAW, XML_DATA_FORMATTED
from .utils import COLOR, CRLF, HTTP_OK, MockEnvironment, http, DUMMY_URL
@@ -227,6 +227,13 @@ def test_ensure_contents_colored(httpbin, endpoint):
assert COLOR in r
@pytest.mark.parametrize('style', PIE_STYLES.keys())
def test_ensure_meta_is_colored(httpbin, style):
env = MockEnvironment(colors=256)
r = http('--meta', '--style', style, 'GET', httpbin + '/get', env=env)
assert COLOR in r
class TestPrettyOptions:
"""Test the --pretty handling."""

View File

@@ -101,3 +101,18 @@ def test_verbose_chunked(httpbin_with_chunked_support):
def test_request_headers_response_body(httpbin):
r = http('--print=Hb', httpbin + '/get')
assert_output_matches(r, ExpectSequence.TERMINAL_REQUEST)
def test_request_single_verbose(httpbin):
r = http('-v', httpbin + '/post', 'hello=world')
assert_output_matches(r, ExpectSequence.TERMINAL_EXCHANGE)
def test_request_double_verbose(httpbin):
r = http('-vv', httpbin + '/post', 'hello=world')
assert_output_matches(r, ExpectSequence.TERMINAL_EXCHANGE_META)
def test_request_meta(httpbin):
r = http('--meta', httpbin + '/get')
assert_output_matches(r, [Expect.RESPONSE_META])

View File

@@ -7,6 +7,7 @@ from ...utils import CRLF
SEPARATOR_RE = re.compile(f'^{MESSAGE_SEPARATOR}')
KEY_VALUE_RE = re.compile(r'[\n]*((.*?):(.+)[\n]?)+[\n]*')
def make_headers_re(message_type: Expect):
@@ -43,6 +44,7 @@ BODY_ENDINGS = [
TOKEN_REGEX_MAP = {
Expect.REQUEST_HEADERS: make_headers_re(Expect.REQUEST_HEADERS),
Expect.RESPONSE_HEADERS: make_headers_re(Expect.RESPONSE_HEADERS),
Expect.RESPONSE_META: KEY_VALUE_RE,
Expect.SEPARATOR: SEPARATOR_RE,
}

View File

@@ -107,6 +107,29 @@ def test_assert_output_matches_headers_with_body_and_separator():
)
def test_assert_output_matches_response_meta():
assert_output_matches(
(
'Key: Value\n'
'Elapsed Time: 3.3s'
),
[Expect.RESPONSE_META]
)
def test_assert_output_matches_whole_response():
assert_output_matches(
(
f'HTTP/1.1{CRLF}'
f'AAA:BBB{CRLF}'
f'{CRLF}'
f'CCC{MESSAGE_SEPARATOR}'
'Elapsed Time: 3.3s'
),
[Expect.RESPONSE_HEADERS, Expect.BODY, Expect.RESPONSE_META]
)
def test_assert_output_matches_multiple_messages():
assert_output_matches(
(

View File

@@ -8,6 +8,7 @@ class Expect(Enum):
"""
REQUEST_HEADERS = auto()
RESPONSE_HEADERS = auto()
RESPONSE_META = auto()
BODY = auto()
SEPARATOR = auto()
@@ -45,6 +46,10 @@ class ExpectSequence:
*TERMINAL_REQUEST,
*TERMINAL_RESPONSE,
]
TERMINAL_EXCHANGE_META = [
*TERMINAL_EXCHANGE,
Expect.RESPONSE_META
]
TERMINAL_BODY = [
RAW_BODY,
Expect.SEPARATOR