Coverage for tests/test_npmproxy.py: 100%
26 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 18:52 -0500
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 18:52 -0500
1"""Unit tests for Npm Proxy"""
3import os
4import sys
5import unittest
6import tempfile
7from unittest.mock import patch
9from urllib3._collections import HTTPHeaderDict
11from mocks import MockedLogger, MockedPoolManager
13sys.path.insert(0, os.path.abspath(
14 os.path.join(os.path.dirname(__file__), "../creepo")))
16from npmproxy import NpmProxy # pylint: disable=wrong-import-position
19class TestNpmProxy(unittest.TestCase):
20 """Unit tests for npmproxy"""
22 def test_npm(self):
23 """Test npm proxy"""
25 def start_ok_response(status, headers): # pylint: disable=unused-argument
26 self.assertEqual(status, '200 OK')
27 content = b'{"versions": []}'
28 request = {
29 'REQUEST_URI': '/m2/org/apache/maven/plugins/maven-clean-plugin/2.5/maven-clean-plugin-2.5.pom',
30 'log': []
31 }
33 config = {
34 'log': [],
35 }
37 config['logger'] = MockedLogger(request['log'])
39 with patch('urllib3.PoolManager') as mock_poolmanager:
41 with tempfile.TemporaryDirectory() as tmpdirname:
42 with unittest.mock.patch.dict('os.environ', {'HOME': tmpdirname}):
44 mock_poolmanager.return_value = MockedPoolManager(
45 status_code=200, response_headers=HTTPHeaderDict(), content=content)
47 proxy = NpmProxy(config)
49 result = list(proxy.proxy(request, start_ok_response))
50 print(
51 f" Actual type: {type(result)}: value: {result}")
52 print(
53 f"Leave this in place to capture MockedLogger output\n{request}")
55 self.assertEqual(
56 content, result[0], "Expected the mocked response")