|
2 | 2 | import requests
|
3 | 3 | import json
|
4 | 4 |
|
| 5 | +from .fixtures import Environ |
| 6 | + |
5 | 7 | from readme_metrics import MetricsApiConfig
|
6 | 8 | from readme_metrics import MetricsMiddleware
|
7 | 9 |
|
8 |
| - |
9 | 10 | # for this, I'm not exactly sure how to test the __call__ function
|
10 | 11 | # possible options I considered was making a mock server inside this test case
|
11 | 12 | # connected to the middleware somehow
|
@@ -36,21 +37,83 @@ def sendRequestsAtOnce(self, requestQueue):
|
36 | 37 | return requestQueue
|
37 | 38 |
|
38 | 39 |
|
| 40 | +# Mock middleware config |
| 41 | +def mockMiddlewareConfig(): |
| 42 | + return MetricsApiConfig( |
| 43 | + "koSyKkViOR5gD6yjBxlsprHfjAIlWOh6", |
| 44 | + lambda req: { "id": "123", "label": "testuser", "email": "[email protected]"}, |
| 45 | + buffer_length=1, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +# Mock callback for handling middleware response |
| 50 | +class MetricsCoreMock: |
| 51 | + def process(self, req, res): |
| 52 | + self.req = req |
| 53 | + self.res = res |
| 54 | + |
| 55 | + |
| 56 | +# Mock application |
| 57 | +class MockApplication: |
| 58 | + def __init__(self, responseObjectString): |
| 59 | + self.responseObjectString = responseObjectString |
| 60 | + |
| 61 | + def __call__(self, environ, start_response): |
| 62 | + self.environ = environ |
| 63 | + self.start_response = start_response |
| 64 | + return [self.responseObjectString.encode("utf-8")] |
| 65 | + |
| 66 | + def mockStartResponse(self, status, headers): |
| 67 | + self.status = status |
| 68 | + self.headers = headers |
| 69 | + |
| 70 | + |
39 | 71 | class TestMetricsMiddleware:
|
40 | 72 | def setUp(self):
|
41 | 73 | self.mockserver = MockServer()
|
42 | 74 |
|
43 |
| - @pytest.mark.skip(reason="@todo") |
| 75 | + # @pytest.mark.skip(reason="@todo") |
44 | 76 | def testNoRequest(self):
|
45 |
| - # Test no request (None) but the function is called |
46 |
| - # Test no request ([]) but the function is called |
47 | 77 | pass
|
48 | 78 |
|
49 |
| - @pytest.mark.skip(reason="@todo") |
50 |
| - def testSingleRequest(self): |
51 |
| - # Test if a single request got through and processed |
52 |
| - # Test if a single request is sent but with trash data(?) |
53 |
| - pass |
| 79 | + def testGetRequest(self): |
| 80 | + emptyByteString = b"" |
| 81 | + responseObjectString = "{ responseObject: 'value' }" |
| 82 | + environ = Environ.MockEnviron().getEnvironForRequest(emptyByteString, "GET") |
| 83 | + app = MockApplication(responseObjectString) |
| 84 | + metrics = MetricsCoreMock() |
| 85 | + middleware = MetricsMiddleware(app, mockMiddlewareConfig()) |
| 86 | + middleware.metrics_core = metrics |
| 87 | + next(middleware(environ, app.mockStartResponse)) |
| 88 | + assert metrics.req.data == emptyByteString |
| 89 | + assert metrics.req.method == "GET" |
| 90 | + assert metrics.res.body == responseObjectString |
| 91 | + |
| 92 | + def testEmptyPostRequest(self): |
| 93 | + jsonString = b"" |
| 94 | + responseObjectString = "{ responseObject: 'value' }" |
| 95 | + environ = Environ.MockEnviron().getEnvironForRequest(jsonString, "POST") |
| 96 | + app = MockApplication(responseObjectString) |
| 97 | + metrics = MetricsCoreMock() |
| 98 | + middleware = MetricsMiddleware(app, mockMiddlewareConfig()) |
| 99 | + middleware.metrics_core = metrics |
| 100 | + next(middleware(environ, app.mockStartResponse)) |
| 101 | + assert metrics.req.data == jsonString |
| 102 | + assert metrics.req.method == "POST" |
| 103 | + assert metrics.res.body == responseObjectString |
| 104 | + |
| 105 | + def testNonEmptyPostRequest(self): |
| 106 | + jsonString = b"{abc: 123}" |
| 107 | + responseObjectString = "{ responseObject: 'value' }" |
| 108 | + environ = Environ.MockEnviron().getEnvironForRequest(jsonString, "POST") |
| 109 | + app = MockApplication(responseObjectString) |
| 110 | + metrics = MetricsCoreMock() |
| 111 | + middleware = MetricsMiddleware(app, mockMiddlewareConfig()) |
| 112 | + middleware.metrics_core = metrics |
| 113 | + next(middleware(environ, app.mockStartResponse)) |
| 114 | + assert metrics.req.data == jsonString |
| 115 | + assert metrics.req.method == "POST" |
| 116 | + assert metrics.res.body == responseObjectString |
54 | 117 |
|
55 | 118 | @pytest.mark.skip(reason="@todo")
|
56 | 119 | def testMultipleRequests(self):
|
|
0 commit comments