-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make the API-server return HTTP status 404 on non-existing endpoints …
…instead of 200. Fixes #9860.
- Loading branch information
Showing
19 changed files
with
222 additions
and
152 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Health end-point.""" | ||
|
||
import bottle | ||
|
||
|
||
@bottle.get("/api/internal/health", authentication_required=False) | ||
def get_health(): | ||
"""Return the API-server health.""" | ||
return {"healthy": True} # For now, server being up means it is healthy |
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,13 @@ | ||
"""Unit tests for the health route.""" | ||
|
||
import unittest | ||
|
||
from routes import get_health | ||
|
||
|
||
class HealthTest(unittest.TestCase): | ||
"""Unit tests for the health route.""" | ||
|
||
def test_health(self): | ||
"""Test that the health status can be retrieved.""" | ||
self.assertEqual({"healthy": True}, get_health()) |
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 |
---|---|---|
@@ -1,21 +1,46 @@ | ||
"""Unit tests for the API-server healthcheck script.""" | ||
|
||
import sys | ||
import unittest | ||
from http import HTTPStatus | ||
from unittest.mock import patch, MagicMock | ||
|
||
|
||
@patch("sys.exit") | ||
@patch("urllib.request.urlopen") | ||
class APIServerHealthcheckTestCase(unittest.TestCase): | ||
"""Unit tests for the API-server healthcheck.""" | ||
|
||
@patch("sys.exit") | ||
@patch("urllib.request.urlopen") | ||
def test_healthy(self, mock_urlopen, mock_exit): | ||
"""Test that the server is healthy.""" | ||
def create_response(self, healthy: bool = True, status=HTTPStatus.OK) -> MagicMock: | ||
"""Create a API-server response.""" | ||
response = MagicMock() | ||
response.status = HTTPStatus.OK | ||
response.status = status | ||
response.read.return_value = b'{"healthy": true}' if healthy else b'{"healthy": false}' | ||
response.__enter__.return_value = response | ||
mock_urlopen.return_value = response | ||
return response | ||
|
||
def run_haelthcheck(self) -> None: | ||
"""Run the healthcheck.""" | ||
import healthcheck # noqa: F401 | ||
|
||
def tearDown(self) -> None: | ||
"""Remove the healthcheck module.""" | ||
del sys.modules["healthcheck"] | ||
|
||
def test_healthy(self, mock_urlopen, mock_exit): | ||
"""Test that the server is healthy.""" | ||
mock_urlopen.return_value = self.create_response() | ||
self.run_haelthcheck() | ||
mock_exit.assert_called_once_with(0) | ||
|
||
def test_unhealthy_json(self, mock_urlopen, mock_exit): | ||
"""Test that the server is unhealthy.""" | ||
mock_urlopen.return_value = self.create_response(healthy=False) | ||
self.run_haelthcheck() | ||
mock_exit.assert_called_once_with(1) | ||
|
||
def test_unhealthy_status(self, mock_urlopen, mock_exit): | ||
"""Test that the server is unhealthy.""" | ||
mock_urlopen.return_value = self.create_response(status=HTTPStatus.INTERNAL_SERVER_ERROR) | ||
self.run_haelthcheck() | ||
mock_exit.assert_called_once_with(1) |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,15 @@ | ||
Feature: server info | ||
Information about the server can be retrieved | ||
|
||
Scenario: get the server information | ||
When the client gets the server information | ||
Then the server information is returned | ||
|
||
Scenario: healthcheck API-server | ||
Given a healthy server | ||
When a client checks the server health | ||
Then the server answers | ||
|
||
Scenario: API-endpoints documentation | ||
When the client gets the server documentation | ||
Then the server documentation is returned |
Oops, something went wrong.