Skip to content

Commit 068cad6

Browse files
fix: add browser header for test case (#1889)
**Issue number:** ### PR Type **What kind of change does this PR introduce?** * [ ] Feature * [ ] Bug Fix * [x] Refactoring (no functional or API changes) * [ ] Documentation Update * [ ] Maintenance (dependency updates, CI, etc.) ## Summary ### Changes Added a try except block for the unit test case `test_const.py` to handle 403 Forbidden errors, which occur when Splunk documentation blocks automated requests. Now, while directly accessing the `url` if it throws an exception, the test case will automatically retry the request by adding a User-Agent header to mimic browser behaviour. ### User experience No change in user experience. ## Checklist If an item doesn't apply to your changes, leave it unchecked. ### Review * [x] self-review - I have performed a self-review of this change according to the [development guidelines](https://splunk.github.io/addonfactory-ucc-generator/contributing/#development-guidelines) * [ ] Changes are documented. The documentation is understandable, examples work [(more info)](https://splunk.github.io/addonfactory-ucc-generator/contributing/#documentation-guidelines) * [x] PR title and description follows the [contributing principles](https://splunk.github.io/addonfactory-ucc-generator/contributing/#pull-requests) * [ ] meeting - I have scheduled a meeting or recorded a demo to explain these changes (if there is a video, put a link below and in the ticket) ### Tests See [the testing doc](https://splunk.github.io/addonfactory-ucc-generator/contributing/#build-and-test). * [x] Unit - tests have been added/modified to cover the changes * [ ] Smoke - tests have been added/modified to cover the changes * [ ] UI - tests have been added/modified to cover the changes * [x] coverage - I have checked the code coverage of my changes [(see more)](https://splunk.github.io/addonfactory-ucc-generator/contributing/#checking-the-code-coverage) **Demo/meeting:** *Reviewers are encouraged to request meetings or demos if any part of the change is unclear*
1 parent 533fc6a commit 068cad6

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

tests/unit/test_const.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
11
import re
22
import urllib.request
3+
import urllib.error
34

45
from splunk_add_on_ucc_framework.const import SPLUNK_COMMANDS
56

67

78
def test_command_list_up_to_date():
8-
with urllib.request.urlopen(
9-
"https://docs.splunk.com/Documentation/Splunk/latest/SearchReference"
10-
) as resp:
11-
content = resp.read().decode()
9+
url = "https://docs.splunk.com/Documentation/Splunk/latest/SearchReference"
10+
try:
11+
with urllib.request.urlopen(url) as resp:
12+
content = resp.read().decode()
13+
except urllib.error.HTTPError:
14+
# passing an imitation of browser header to make this a request from web browser
15+
headers = {
16+
"User-Agent": (
17+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
18+
"AppleWebKit/537.36 (KHTML, like Gecko) "
19+
"Chrome/115.0.0.0 Safari/537.36"
20+
)
21+
}
22+
23+
req = urllib.request.Request(url, headers=headers)
24+
25+
with urllib.request.urlopen(req) as resp:
26+
content = resp.read().decode()
1227

1328
match = re.search(r"Search\s+Commands.+?<ul.+?>(.+?)</ul>", content, re.S)
1429
if match:

0 commit comments

Comments
 (0)