forked from pex-tool/pex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This was added in pex-tool#152 to fix pex-tool#101 but has since been lost to vendored Pip taking over for ~all network requests. Pex will be fetching artifacts recorded in its lockfiles now; so it's time to restore this. Work towards pex-tool#1583.
- Loading branch information
Showing
2 changed files
with
76 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import print_function | ||
|
||
from threading import Thread | ||
|
||
import pytest | ||
|
||
from pex.compatibility import PY2 | ||
from pex.fetcher import URLFetcher | ||
from pex.typing import TYPE_CHECKING | ||
from pex.version import __version__ | ||
|
||
if PY2: | ||
from BaseHTTPServer import BaseHTTPRequestHandler | ||
from SocketServer import TCPServer | ||
else: | ||
from http.server import BaseHTTPRequestHandler | ||
from socketserver import TCPServer | ||
|
||
if TYPE_CHECKING: | ||
from typing import Tuple | ||
|
||
|
||
@pytest.fixture | ||
def server_address(): | ||
class GETRequestHandler(BaseHTTPRequestHandler): | ||
def do_GET(self): | ||
body = self.headers["User-Agent"].encode("utf-8") | ||
self.send_response(200) | ||
self.send_header("Content-Type", "application/octet-stream") | ||
self.send_header("Content-Length", str(len(body))) | ||
self.end_headers() | ||
self.wfile.write(body) | ||
|
||
server = TCPServer(("127.0.0.1", 0), GETRequestHandler) | ||
host, port = server.server_address | ||
|
||
server_thread = Thread(target=server.serve_forever) | ||
server_thread.daemon = True | ||
server_thread.start() | ||
try: | ||
yield host, port | ||
finally: | ||
server.shutdown() | ||
server_thread.join() | ||
|
||
|
||
def test_user_agent(server_address): | ||
# type: (Tuple[str, int]) -> None | ||
|
||
host, port = server_address | ||
url = "http://{host}:{port}".format(host=host, port=port) | ||
url_fetcher = URLFetcher() | ||
with url_fetcher.get_body_stream(url) as fp: | ||
assert "pex/{version}".format(version=__version__) == fp.read().decode("utf-8") | ||
|