-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from dispatchrun/http-tests
test: decouple test components from httpx
- Loading branch information
Showing
12 changed files
with
116 additions
and
47 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
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,10 @@ | ||
from fastapi import FastAPI | ||
from fastapi.testclient import TestClient | ||
|
||
import dispatch.test.httpx | ||
from dispatch.test.client import HttpClient | ||
|
||
|
||
def http_client(app: FastAPI) -> HttpClient: | ||
"""Build a client for a FastAPI app.""" | ||
return dispatch.test.httpx.Client(TestClient(app)) |
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 @@ | ||
from dataclasses import dataclass | ||
from typing import Mapping, Protocol | ||
|
||
|
||
@dataclass | ||
class HttpResponse(Protocol): | ||
status_code: int | ||
body: bytes | ||
|
||
def raise_for_status(self): | ||
"""Raise an exception on non-2xx responses.""" | ||
... | ||
|
||
|
||
class HttpClient(Protocol): | ||
"""Protocol for HTTP clients.""" | ||
|
||
def get(self, url: str, headers: Mapping[str, str] = {}) -> HttpResponse: | ||
"""Make a GET request.""" | ||
... | ||
|
||
def post( | ||
self, url: str, body: bytes, headers: Mapping[str, str] = {} | ||
) -> HttpResponse: | ||
"""Make a POST request.""" | ||
... | ||
|
||
def url_for(self, path: str) -> str: | ||
"""Get the fully-qualified URL for a path.""" | ||
... |
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,39 @@ | ||
from typing import Mapping | ||
|
||
import httpx | ||
|
||
from dispatch.test.http import HttpClient, HttpResponse | ||
|
||
|
||
class Client(HttpClient): | ||
def __init__(self, client: httpx.Client): | ||
self.client = client | ||
|
||
def get(self, url: str, headers: Mapping[str, str] = {}) -> HttpResponse: | ||
response = self.client.get(url, headers=headers) | ||
return Response(response) | ||
|
||
def post( | ||
self, url: str, body: bytes, headers: Mapping[str, str] = {} | ||
) -> HttpResponse: | ||
response = self.client.post(url, content=body, headers=headers) | ||
return Response(response) | ||
|
||
def url_for(self, path: str) -> str: | ||
return str(httpx.URL(self.client.base_url).join(path)) | ||
|
||
|
||
class Response(HttpResponse): | ||
def __init__(self, response: httpx.Response): | ||
self.response = response | ||
|
||
@property | ||
def status_code(self): | ||
return self.response.status_code | ||
|
||
@property | ||
def body(self): | ||
return self.response.content | ||
|
||
def raise_for_status(self): | ||
self.response.raise_for_status() |
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
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