-
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45a761a
commit 2d0b6a6
Showing
6 changed files
with
79 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from contextlib import contextmanager | ||
from typing import Dict, Generator, Optional, Tuple | ||
from urllib.parse import urlparse | ||
|
||
import click | ||
|
||
|
||
def validate_schema(ctx: click.core.Context, param: click.core.Option, raw_value: str) -> str: | ||
if not urlparse(raw_value).netloc: | ||
raise click.UsageError("Invalid SCHEMA, must be a valid URL.") | ||
return raw_value | ||
|
||
|
||
def validate_auth( | ||
ctx: click.core.Context, param: click.core.Option, raw_value: Optional[str] | ||
) -> Optional[Tuple[str, str]]: | ||
if raw_value is not None: | ||
with reraise_format_error(raw_value): | ||
user, password = tuple(raw_value.split(":")) | ||
return user, password | ||
return None | ||
|
||
|
||
def validate_headers(ctx: click.core.Context, param: click.core.Option, raw_value: Tuple[str, ...]) -> Dict[str, str]: | ||
headers = {} | ||
for header in raw_value: | ||
with reraise_format_error(header): | ||
key, value = header.split(":") | ||
headers[key] = value.lstrip() | ||
return headers | ||
|
||
|
||
@contextmanager | ||
def reraise_format_error(raw_value: str) -> Generator: | ||
try: | ||
yield | ||
except ValueError: | ||
raise click.BadParameter(f"Should be in KEY:VALUE format. Got: {raw_value}") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import click | ||
import pytest | ||
from hypothesis import given | ||
from hypothesis import strategies as st | ||
|
||
from schemathesis.cli import validators | ||
|
||
|
||
@given(value=st.text()) | ||
def test_validate_schema(value): | ||
with pytest.raises(click.UsageError): | ||
validators.validate_schema(None, None, value) | ||
|
||
|
||
@given(value=st.text()) | ||
def test_validate_auth(value): | ||
with pytest.raises(click.BadParameter): | ||
validators.validate_auth(None, None, value) | ||
|
||
|
||
@given(value=st.lists(st.text(), min_size=1).map(tuple)) | ||
def test_validate_header(value): | ||
with pytest.raises(click.BadParameter): | ||
validators.validate_headers(None, None, value) | ||
|
||
|
||
def test_reraise_format_error(): | ||
with pytest.raises(click.BadParameter, match="Should be in KEY:VALUE format. Got: bla"): | ||
with validators.reraise_format_error("bla"): | ||
raise ValueError |