-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
171 additions
and
5 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
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,128 @@ | ||
import sys | ||
from threading import Thread | ||
import json | ||
import warnings | ||
|
||
import stripe | ||
import pytest | ||
|
||
if sys.version_info[0] < 3: | ||
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | ||
else: | ||
from http.server import BaseHTTPRequestHandler, HTTPServer | ||
|
||
|
||
class TestIntegration(object): | ||
@pytest.fixture(autouse=True) | ||
def close_mock_server(self): | ||
yield | ||
if self.mock_server: | ||
self.mock_server.shutdown() | ||
self.mock_server.server_close() | ||
self.mock_server_thread.join() | ||
|
||
@pytest.fixture(autouse=True) | ||
def setup_stripe(self): | ||
orig_attrs = { | ||
"api_base": stripe.api_base, | ||
"api_key": stripe.api_key, | ||
"default_http_client": stripe.default_http_client, | ||
"proxy": stripe.proxy, | ||
} | ||
stripe.api_base = "http://localhost:12111" # stripe-mock | ||
stripe.api_key = "sk_test_123" | ||
stripe.default_http_client = None | ||
stripe.proxy = None | ||
yield | ||
stripe.api_base = orig_attrs["api_base"] | ||
stripe.api_key = orig_attrs["api_key"] | ||
stripe.default_http_client = orig_attrs["default_http_client"] | ||
stripe.proxy = orig_attrs["proxy"] | ||
|
||
def setup_mock_server(self, handler): | ||
# Configure mock server. | ||
# Passing 0 as the port will cause a random free port to be chosen. | ||
self.mock_server = HTTPServer(("localhost", 0), handler) | ||
_, self.mock_server_port = self.mock_server.server_address | ||
|
||
# Start running mock server in a separate thread. | ||
# Daemon threads automatically shut down when the main process exits. | ||
self.mock_server_thread = Thread(target=self.mock_server.serve_forever) | ||
self.mock_server_thread.setDaemon(True) | ||
self.mock_server_thread.start() | ||
|
||
def test_hits_api_base(self): | ||
class MockServerRequestHandler(BaseHTTPRequestHandler): | ||
num_requests = 0 | ||
|
||
def do_GET(self): | ||
self.__class__.num_requests += 1 | ||
|
||
self.send_response(200) | ||
self.send_header( | ||
"Content-Type", "application/json; charset=utf-8" | ||
) | ||
self.end_headers() | ||
self.wfile.write(json.dumps({}).encode("utf-8")) | ||
return | ||
|
||
self.setup_mock_server(MockServerRequestHandler) | ||
|
||
stripe.api_base = "http://localhost:%s" % self.mock_server_port | ||
stripe.Balance.retrieve() | ||
assert MockServerRequestHandler.num_requests == 1 | ||
|
||
def test_hits_proxy_through_default_http_client(self): | ||
class MockServerRequestHandler(BaseHTTPRequestHandler): | ||
num_requests = 0 | ||
|
||
def do_GET(self): | ||
self.__class__.num_requests += 1 | ||
|
||
self.send_response(200) | ||
self.send_header( | ||
"Content-Type", "application/json; charset=utf-8" | ||
) | ||
self.end_headers() | ||
self.wfile.write(json.dumps({}).encode("utf-8")) | ||
return | ||
|
||
self.setup_mock_server(MockServerRequestHandler) | ||
|
||
stripe.proxy = "http://localhost:%s" % self.mock_server_port | ||
stripe.Balance.retrieve() | ||
assert MockServerRequestHandler.num_requests == 1 | ||
|
||
stripe.proxy = "http://bad-url" | ||
|
||
with warnings.catch_warnings(record=True) as w: | ||
stripe.Balance.retrieve() | ||
assert len(w) == 1 | ||
assert "stripe.proxy was updated after sending a request" in str( | ||
w[0].message | ||
) | ||
|
||
assert MockServerRequestHandler.num_requests == 2 | ||
|
||
def test_hits_proxy_through_custom_client(self): | ||
class MockServerRequestHandler(BaseHTTPRequestHandler): | ||
num_requests = 0 | ||
|
||
def do_GET(self): | ||
self.__class__.num_requests += 1 | ||
|
||
self.send_response(200) | ||
self.send_header( | ||
"Content-Type", "application/json; charset=utf-8" | ||
) | ||
self.end_headers() | ||
self.wfile.write(json.dumps({}).encode("utf-8")) | ||
return | ||
|
||
self.setup_mock_server(MockServerRequestHandler) | ||
|
||
stripe.default_http_client = stripe.http_client.new_default_http_client( | ||
proxy="http://localhost:%s" % self.mock_server_port | ||
) | ||
stripe.Balance.retrieve() | ||
assert MockServerRequestHandler.num_requests == 1 |