| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | """Tests for packaging.command.bdist.""" | 
					
						
							|  |  |  | import urllib.request | 
					
						
							|  |  |  | import urllib.parse | 
					
						
							|  |  |  | import urllib.error | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 15:26:59 +02:00
										 |  |  | try: | 
					
						
							|  |  |  |     import threading | 
					
						
							| 
									
										
										
										
											2011-09-18 20:11:48 +02:00
										 |  |  |     from packaging.tests.pypi_server import ( | 
					
						
							|  |  |  |         PyPIServer, PYPI_DEFAULT_STATIC_PATH) | 
					
						
							| 
									
										
										
										
											2011-05-19 15:26:59 +02:00
										 |  |  | except ImportError: | 
					
						
							|  |  |  |     threading = None | 
					
						
							|  |  |  |     PyPIServer = None | 
					
						
							|  |  |  |     PYPI_DEFAULT_STATIC_PATH = None | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | from packaging.tests import unittest | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-19 15:26:59 +02:00
										 |  |  | @unittest.skipIf(threading is None, "Needs threading") | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | class PyPIServerTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_records_requests(self): | 
					
						
							|  |  |  |         # We expect that PyPIServer can log our requests | 
					
						
							|  |  |  |         server = PyPIServer() | 
					
						
							|  |  |  |         server.default_response_status = 200 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             server.start() | 
					
						
							|  |  |  |             self.assertEqual(len(server.requests), 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             data = b'Rock Around The Bunker' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             headers = {"X-test-header": "Mister Iceberg"} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-09-18 20:11:48 +02:00
										 |  |  |             request = urllib.request.Request( | 
					
						
							|  |  |  |                 server.full_address, data, headers) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  |             urllib.request.urlopen(request) | 
					
						
							|  |  |  |             self.assertEqual(len(server.requests), 1) | 
					
						
							|  |  |  |             handler, request_data = server.requests[-1] | 
					
						
							|  |  |  |             self.assertIn(data, request_data) | 
					
						
							|  |  |  |             self.assertIn("x-test-header", handler.headers) | 
					
						
							| 
									
										
										
										
											2011-09-18 20:11:48 +02:00
										 |  |  |             self.assertEqual(handler.headers["x-test-header"], | 
					
						
							|  |  |  |                              "Mister Iceberg") | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             server.stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_serve_static_content(self): | 
					
						
							|  |  |  |         # PYPI Mocked server can serve static content from disk. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def uses_local_files_for(server, url_path): | 
					
						
							|  |  |  |             """Test that files are served statically (eg. the output from the
 | 
					
						
							|  |  |  |             server is the same than the one made by a simple file read. | 
					
						
							|  |  |  |             """
 | 
					
						
							|  |  |  |             url = server.full_address + url_path | 
					
						
							|  |  |  |             request = urllib.request.Request(url) | 
					
						
							|  |  |  |             response = urllib.request.urlopen(request) | 
					
						
							| 
									
										
										
										
											2011-05-19 15:51:27 +02:00
										 |  |  |             with open(PYPI_DEFAULT_STATIC_PATH + "/test_pypi_server" | 
					
						
							|  |  |  |                       + url_path) as file: | 
					
						
							|  |  |  |                 return response.read().decode() == file.read() | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         server = PyPIServer(static_uri_paths=["simple", "external"], | 
					
						
							|  |  |  |             static_filesystem_paths=["test_pypi_server"]) | 
					
						
							|  |  |  |         server.start() | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             # the file does not exists on the disc, so it might not be served | 
					
						
							|  |  |  |             url = server.full_address + "/simple/unexisting_page" | 
					
						
							|  |  |  |             request = urllib.request.Request(url) | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 urllib.request.urlopen(request) | 
					
						
							|  |  |  |             except urllib.error.HTTPError as e: | 
					
						
							|  |  |  |                 self.assertEqual(e.code, 404) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # now try serving a content that do exists | 
					
						
							|  |  |  |             self.assertTrue(uses_local_files_for(server, "/simple/index.html")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             # and another one in another root path | 
					
						
							| 
									
										
										
										
											2011-09-18 20:11:48 +02:00
										 |  |  |             self.assertTrue(uses_local_files_for(server, | 
					
						
							|  |  |  |                                                  "/external/index.html")) | 
					
						
							| 
									
										
										
										
											2011-05-19 13:07:25 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         finally: | 
					
						
							|  |  |  |             server.stop() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def test_suite(): | 
					
						
							|  |  |  |     return unittest.makeSuite(PyPIServerTest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     unittest.main(defaultTest="test_suite") |