|
| 1 | +# Copyright 2020, OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from http import HTTPStatus |
| 16 | + |
| 17 | +from opentelemetry.instrumentation.utils import http_status_to_canonical_code |
| 18 | +from opentelemetry.test.test_base import TestBase |
| 19 | +from opentelemetry.trace.status import StatusCanonicalCode |
| 20 | + |
| 21 | + |
| 22 | +class TestUtils(TestBase): |
| 23 | + def test_http_status_to_canonical_code(self): |
| 24 | + for status_code, expected in ( |
| 25 | + (HTTPStatus.OK, StatusCanonicalCode.OK), |
| 26 | + (HTTPStatus.ACCEPTED, StatusCanonicalCode.OK), |
| 27 | + (HTTPStatus.IM_USED, StatusCanonicalCode.OK), |
| 28 | + (HTTPStatus.MULTIPLE_CHOICES, StatusCanonicalCode.OK), |
| 29 | + (HTTPStatus.BAD_REQUEST, StatusCanonicalCode.INVALID_ARGUMENT), |
| 30 | + (HTTPStatus.UNAUTHORIZED, StatusCanonicalCode.UNAUTHENTICATED), |
| 31 | + (HTTPStatus.FORBIDDEN, StatusCanonicalCode.PERMISSION_DENIED), |
| 32 | + (HTTPStatus.NOT_FOUND, StatusCanonicalCode.NOT_FOUND), |
| 33 | + ( |
| 34 | + HTTPStatus.UNPROCESSABLE_ENTITY, |
| 35 | + StatusCanonicalCode.INVALID_ARGUMENT, |
| 36 | + ), |
| 37 | + ( |
| 38 | + HTTPStatus.TOO_MANY_REQUESTS, |
| 39 | + StatusCanonicalCode.RESOURCE_EXHAUSTED, |
| 40 | + ), |
| 41 | + (HTTPStatus.NOT_IMPLEMENTED, StatusCanonicalCode.UNIMPLEMENTED), |
| 42 | + (HTTPStatus.SERVICE_UNAVAILABLE, StatusCanonicalCode.UNAVAILABLE), |
| 43 | + ( |
| 44 | + HTTPStatus.GATEWAY_TIMEOUT, |
| 45 | + StatusCanonicalCode.DEADLINE_EXCEEDED, |
| 46 | + ), |
| 47 | + ( |
| 48 | + HTTPStatus.HTTP_VERSION_NOT_SUPPORTED, |
| 49 | + StatusCanonicalCode.INTERNAL, |
| 50 | + ), |
| 51 | + (600, StatusCanonicalCode.UNKNOWN), |
| 52 | + (99, StatusCanonicalCode.UNKNOWN), |
| 53 | + ): |
| 54 | + with self.subTest(status_code=status_code): |
| 55 | + actual = http_status_to_canonical_code(int(status_code)) |
| 56 | + self.assertEqual(actual, expected, status_code) |
0 commit comments