httprest 0.1.1__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: httprest
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: A library for making simple REST-like HTTP requests
5
5
  Home-page: https://github.com/litteratum/httprest
6
6
  License: MIT
@@ -71,3 +71,16 @@ And then you simply use it in the API client:
71
71
  api = MyAPI(..., http_client=MyHTTPClient())
72
72
  ```
73
73
 
74
+ ### Fake client
75
+ The library provides the `http.fake_client` module containing `FakeHTTPClient` class.
76
+ That class may be used for API testing. Example:
77
+ ```python
78
+ from httprest.http.fake_client import FakeHTTPClient, HTTPResponse
79
+
80
+ http_client = FakeHTTPClient(responses=[HTTPResponse(200, b"", headers={})])
81
+ api = MyAPI(..., http_client=http_client)
82
+ api.operation()
83
+ # assert your expectations here
84
+ assert http_client.history == [...]
85
+ ```
86
+
@@ -42,3 +42,16 @@ And then you simply use it in the API client:
42
42
 
43
43
  api = MyAPI(..., http_client=MyHTTPClient())
44
44
  ```
45
+
46
+ ### Fake client
47
+ The library provides the `http.fake_client` module containing `FakeHTTPClient` class.
48
+ That class may be used for API testing. Example:
49
+ ```python
50
+ from httprest.http.fake_client import FakeHTTPClient, HTTPResponse
51
+
52
+ http_client = FakeHTTPClient(responses=[HTTPResponse(200, b"", headers={})])
53
+ api = MyAPI(..., http_client=http_client)
54
+ api.operation()
55
+ # assert your expectations here
56
+ assert http_client.history == [...]
57
+ ```
@@ -0,0 +1,43 @@
1
+ """Fake client."""
2
+
3
+ from typing import Any, Dict, List, Optional, Union
4
+
5
+ from httprest.http.base import HTTPResponse
6
+
7
+ from .base import HTTPClient, HTTPTimeoutError
8
+
9
+
10
+ class FakeHTTPClient(HTTPClient):
11
+ """Fake HTTP client."""
12
+
13
+ def __init__(
14
+ self, responses: Optional[List[Union[Exception, HTTPResponse]]] = None
15
+ ) -> None:
16
+ # pylint:disable=super-init-not-called
17
+ self.history: List[Dict[str, Optional[Any]]] = []
18
+ self._responses = responses
19
+
20
+ def _request(
21
+ self,
22
+ method: str,
23
+ url: str,
24
+ json: Optional[dict] = None,
25
+ headers: Optional[dict] = None,
26
+ ) -> HTTPResponse:
27
+ self.history.append(
28
+ {
29
+ "_method": "_request",
30
+ "method": method,
31
+ "url": url,
32
+ "json": json,
33
+ "headers": headers,
34
+ }
35
+ )
36
+ if not self._responses:
37
+ raise HTTPTimeoutError("No response provided")
38
+
39
+ response = self._responses.pop(0)
40
+ if isinstance(response, Exception):
41
+ raise response
42
+
43
+ return response
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "httprest"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "A library for making simple REST-like HTTP requests"
5
5
  authors = ["Andrii Nechaiev <andrewnech@gmail.com>"]
6
6
  readme = "README.md"
File without changes
File without changes
File without changes
File without changes