|
6 | 6 | import pytest |
7 | 7 | from napalm.base.test import conftest as parent_conftest |
8 | 8 | from napalm.base.test.double import BaseTestDouble |
| 9 | +from requests.models import HTTPError |
9 | 10 | from napalm_servertech_pro2 import pro2 |
10 | 11 |
|
11 | 12 |
|
@@ -50,21 +51,47 @@ def open(self): |
50 | 51 | class FakePRO2Api(BaseTestDouble): |
51 | 52 | """ServerTech fake API.""" |
52 | 53 |
|
| 54 | + REQUESTS = { |
| 55 | + "control/outlets/XX99": { |
| 56 | + "headers": {"Content-Type": "text/html"}, |
| 57 | + "status_code": 404, |
| 58 | + } |
| 59 | + } |
| 60 | + |
53 | 61 | def request(self, method, **kwargs): |
54 | | - filename = f'{self.sanitize_text(kwargs["url"].split("/jaws/")[1])}.json' |
55 | | - path = self.find_file(filename) |
56 | | - return FakeRequest(method, path) |
| 62 | + address = kwargs["url"].split("/jaws/")[1] |
| 63 | + if self.REQUESTS.get(address): |
| 64 | + return FakeRequest( |
| 65 | + method, |
| 66 | + address, |
| 67 | + self.REQUESTS[address]["headers"], |
| 68 | + self.REQUESTS[address]["status_code"], |
| 69 | + ) |
| 70 | + else: |
| 71 | + filename = f"{self.sanitize_text(address)}.json" |
| 72 | + path = self.find_file(filename) |
| 73 | + headers = { |
| 74 | + "Content-Type": "application/json" if method == "GET" else "text/html" |
| 75 | + } |
| 76 | + status_code = 200 if method == "GET" else 204 |
| 77 | + return FakeRequest(method, path, headers, status_code) |
57 | 78 |
|
58 | 79 |
|
59 | 80 | class FakeRequest: |
60 | 81 | """A fake API request.""" |
61 | 82 |
|
62 | | - def __init__(self, method, path): |
| 83 | + def __init__(self, method, path, headers, status_code): |
63 | 84 | self.method = method |
64 | 85 | self.path = path |
| 86 | + self.headers = headers |
| 87 | + self.status_code = status_code |
| 88 | + self.text = "" |
65 | 89 |
|
66 | 90 | def raise_for_status(self): |
67 | | - return True |
| 91 | + if self.status_code >= 400: |
| 92 | + raise HTTPError |
| 93 | + else: |
| 94 | + return True |
68 | 95 |
|
69 | 96 | def json(self): |
70 | 97 | with open(self.path, "r") as file: |
|
0 commit comments