istio.io/istio@v0.0.0-20240520182934-d79c90f27776/samples/bookinfo/src/productpage/tests/unit/test_productpage.py (about)

     1  #
     2  # Copyright 2018 Istio Authors
     3  #
     4  #   Licensed under the Apache License, Version 2.0 (the "License");
     5  #   you may not use this file except in compliance with the License.
     6  #   You may obtain a copy of the License at
     7  #
     8  #       http://www.apache.org/licenses/LICENSE-2.0
     9  #
    10  #   Unless required by applicable law or agreed to in writing, software
    11  #   distributed under the License is distributed on an "AS IS" BASIS,
    12  #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  #   See the License for the specific language governing permissions and
    14  #   limitations under the License.
    15  #
    16  # Run from the top level productpage directory with:
    17  #
    18  # pip install -r test-requirements.txt
    19  # python -m unittest discover tests/unit
    20  
    21  import unittest
    22  
    23  import requests_mock
    24  
    25  import productpage
    26  
    27  
    28  class ApplianceTest(unittest.TestCase):
    29  
    30      def setUp(self):
    31          self.app = productpage.app.test_client()
    32  
    33      @requests_mock.Mocker()
    34      def test_header_propagation_reviews(self, m):
    35          """ Check that tracing headers are forwarded correctly """
    36          product_id = 0
    37          # Register expected headers with the mock. If the headers
    38          # don't match, the mock won't fire, an E500 will be triggered
    39          # and the test will fail.
    40          expected_headers = {
    41              'x-request-id': '34eeb41d-d267-9e49-8b84-dde403fc5b72',
    42              'x-b3-traceid': '80f198ee56343ba864fe8b2a57d3eff7',
    43              'x-b3-spanid': 'e457b5a2e4d86bd1',
    44              'x-b3-sampled': '1',
    45              'sw8': '40c7fdf104e3de67'
    46          }
    47          m.get("http://reviews:9080/reviews/%d" % product_id, text='{}',
    48                request_headers=expected_headers)
    49  
    50          uri = "/api/v1/products/%d/reviews" % product_id
    51          headers = {
    52              'x-request-id': '34eeb41d-d267-9e49-8b84-dde403fc5b72',
    53              'x-b3-traceid': '80f198ee56343ba864fe8b2a57d3eff7',
    54              'x-b3-spanid': 'e457b5a2e4d86bd1',
    55              'x-b3-sampled': '1',
    56              'sw8': '40c7fdf104e3de67'
    57          }
    58          actual = self.app.get(uri, headers=headers)
    59          self.assertEqual(200, actual.status_code)
    60  
    61      @requests_mock.Mocker()
    62      def test_header_propagation_ratings(self, m):
    63          """ Check that tracing headers are forwarded correctly """
    64          product_id = 0
    65          # Register expected headers with the mock. If the headers
    66          # don't match, the mock won't fire, an E500 will be triggered
    67          # and the test will fail.
    68          expected_headers = {
    69              'x-request-id': '34eeb41d-d267-9e49-8b84-dde403fc5b73',
    70              'x-b3-traceid': '80f198ee56343ba864fe8b2a57d3eff7',
    71              'x-b3-spanid': 'e457b5a2e4d86bd1',
    72              'x-b3-sampled': '1',
    73              'sw8': '40c7fdf104e3de67'
    74          }
    75          m.get("http://ratings:9080/ratings/%d" % product_id, text='{}',
    76                request_headers=expected_headers)
    77  
    78          uri = "/api/v1/products/%d/ratings" % product_id
    79          headers = {
    80              'x-request-id': '34eeb41d-d267-9e49-8b84-dde403fc5b73',
    81              'x-b3-traceid': '80f198ee56343ba864fe8b2a57d3eff7',
    82              'x-b3-spanid': 'e457b5a2e4d86bd1',
    83              'x-b3-sampled': '1',
    84              'sw8': '40c7fdf104e3de67'
    85          }
    86          actual = self.app.get(uri, headers=headers)
    87          print(actual.data)
    88          self.assertEqual(200, actual.status_code)