Skip to content

Commit

Permalink
remove simulate from client calls
Browse files Browse the repository at this point in the history
- updated test cases to use direct `get` and `post` methods instead of `simulate_get` and `simulate_post`.
- improves the clarity of the test code and aligns with the updated API usage.
- generalizes implementation
  • Loading branch information
dtrai2 committed Dec 6, 2024
1 parent b8349d3 commit 2544fcc
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions tests/unit/connector/test_http_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,83 +115,83 @@ def test_not_first_pipeline(self):
assert connector.http_server is None

def test_get_method_returns_200(self):
resp = self.client.simulate_get("/json")
resp = self.client.get("/json")
assert resp.status_code == 200

def test_get_method_returns_200_with_authentication(self):
resp = self.client.simulate_get("/auth-json-secret")
resp = self.client.get("/auth-json-secret")
assert resp.status_code == 200

def test_get_method_returns_429_if_queue_is_full(self):
self.object.messages.full = mock.MagicMock()
self.object.messages.full.return_value = True
resp = self.client.simulate_get("/json")
resp = self.client.get("/json")
assert resp.status_code == 429

def test_get_error_code_too_many_requests(self):
data = {"message": "my log message"}
self.object.messages.put = mock.MagicMock()
self.object.messages.put.side_effect = queue.Full()
resp = self.client.simulate_post("/json", json=data)
resp = self.client.post("/json", json=data)
assert resp.status_code == 429

def test_json_endpoint_accepts_post_request(self):
data = {"message": "my log message"}
resp = self.client.simulate_post("/json", json=data)
resp = self.client.post("/json", json=data)
assert resp.status_code == 200

def test_json_endpoint_match_wildcard_route(self):
data = {"message": "my log message"}
resp = self.client.simulate_post("/json", json=data)
resp = self.client.post("/json", json=data)
assert resp.status_code == 200

def test_json_endpoint_not_match_wildcard_route(self):
data = {"message": "my log message"}
resp = self.client.simulate_post("/api/wildcard_path/json/another_path", json=data)
resp = self.client.post("/api/wildcard_path/json/another_path", json=data)
assert resp.status_code == 404

data = {"message": "my log message"}
resp = self.client.simulate_post("/json", json=data)
resp = self.client.post("/json", json=data)
assert resp.status_code == 200

event_from_queue = self.object.messages.get(timeout=0.001)
assert event_from_queue == data

def test_plaintext_endpoint_accepts_post_request(self):
data = "my log message"
resp = self.client.simulate_post("/plaintext", json=data)
resp = self.client.post("/plaintext", json=data)
assert resp.status_code == 200

def test_plaintext_message_is_put_in_queue(self):
data = "my log message"
resp = self.client.simulate_post("/plaintext", body=data)
resp = self.client.post("/plaintext", body=data)
assert resp.status_code == 200
event_from_queue = self.object.messages.get(timeout=0.001)
assert event_from_queue.get("message") == data

def test_jsonl_endpoint_match_regex_route(self):
data = {"message": "my log message"}
resp = self.client.simulate_post("/first/jsonl", json=data)
resp = self.client.post("/first/jsonl", json=data)
assert resp.status_code == 200

def test_jsonl_endpoint_not_match_regex_route(self):
data = {"message": "my log message"}
resp = self.client.simulate_post("/firs/jsonl", json=data)
resp = self.client.post("/firs/jsonl", json=data)
assert resp.status_code == 404

def test_jsonl_endpoint_not_match_before_start_regex(self):
data = {"message": "my log message"}
resp = self.client.simulate_post("/api/first/jsonl", json=data)
resp = self.client.post("/api/first/jsonl", json=data)
assert resp.status_code == 404

def test_jsonl_endpoint_match_wildcard_regex_mix_route(self):
data = {"message": "my log message"}
resp = self.client.simulate_post("/third/jsonl/another_path/last_path", json=data)
resp = self.client.post("/third/jsonl/another_path/last_path", json=data)
assert resp.status_code == 200

def test_jsonl_endpoint_not_match_wildcard_regex_mix_route(self):
data = {"message": "my log message"}
resp = self.client.simulate_post("/api/third/jsonl/another_path", json=data)
resp = self.client.post("/api/third/jsonl/another_path", json=data)
assert resp.status_code == 404

def test_jsonl_messages_are_put_in_queue(self):
Expand All @@ -200,7 +200,7 @@ def test_jsonl_messages_are_put_in_queue(self):
{"message": "my second log message"}
{"message": "my third log message"}
"""
resp = self.client.simulate_post("/jsonl", body=data)
resp = self.client.post("/jsonl", body=data)
assert resp.status_code == 200
assert self.object.messages.qsize() == 3
event_from_queue = self.object.messages.get(timeout=1)
Expand All @@ -212,7 +212,7 @@ def test_jsonl_messages_are_put_in_queue(self):

def test_get_next_returns_message_from_queue(self):
data = {"message": "my log message"}
self.client.simulate_post("/json", json=data)
self.client.post("/json", json=data)
assert self.object.get_next(0.001) == data

def test_get_next_returns_first_in_first_out(self):
Expand All @@ -236,9 +236,9 @@ def test_get_next_returns_first_in_first_out_for_mixed_endpoints(self):
for message in data:
endpoint, post_data = message.values()
if endpoint == "json":
self.client.simulate_post("/json", json=post_data)
self.client.post("/json", json=post_data)
if endpoint == "plaintext":
self.client.simulate_post("/plaintext", body=post_data)
self.client.post("/plaintext", body=post_data)
assert self.object.get_next(0.001) == data[0].get("data")
assert self.object.get_next(0.001) == {"message": data[1].get("data")}
assert self.object.get_next(0.001) == data[2].get("data")
Expand All @@ -253,7 +253,7 @@ def test_server_starts_threaded_server(self):
message = {"message": "my message"}
for i in range(100):
message["message"] = f"message number {i}"
self.client.simulate_post("/json", json=message)
self.client.post("/json", json=message)
assert self.object.messages.qsize() == 100, "messages are put to queue"

def test_get_metadata(self):
Expand Down Expand Up @@ -306,7 +306,7 @@ def test_get_next_with_hmac_of_raw_message(self):
connector.pipeline_index = 1
connector.setup()
test_event = "the content"
self.client.simulate_post("/plaintext", body=test_event)
self.client.post("/plaintext", body=test_event)

expected_event = {
"message": "the content",
Expand Down Expand Up @@ -395,17 +395,17 @@ def test_messages_is_multiprocessing_queue(self):

def test_all_endpoints_share_the_same_queue(self):
data = {"message": "my log message"}
self.client.simulate_post("/json", json=data)
self.client.post("/json", json=data)
assert self.object.messages.qsize() == 1
data = "my log message"
self.client.simulate_post("/plaintext", json=data)
self.client.post("/plaintext", json=data)
assert self.object.messages.qsize() == 2
data = """
{"message": "my first log message"}
{"message": "my second log message"}
{"message": "my third log message"}
"""
self.client.simulate_post("/jsonl", body=data)
self.client.post("/jsonl", body=data)
assert self.object.messages.qsize() == 5

def test_sets_target_to_https_schema_if_ssl_options(self):
Expand All @@ -432,21 +432,21 @@ def test_enpoints_count_requests(self):
self.object.setup()
random_number = random.randint(1, 100)
for number in range(random_number):
self.client.simulate_post("/json", json={"message": f"my message{number}"})
self.client.post("/json", json={"message": f"my message{number}"})
assert self.object.metrics.number_of_http_requests == random_number

@pytest.mark.parametrize("endpoint", ["json", "plaintext", "jsonl"])
def test_endpoint_handles_gzip_compression(self, endpoint):
data = {"message": "my log message"}
data = gzip.compress(json.dumps(data).encode())
headers = {"Content-Encoding": "gzip"}
resp = self.client.simulate_post(f"/{endpoint}", body=data, headers=headers)
resp = self.client.post(f"/{endpoint}", body=data, headers=headers)
assert resp.status_code == 200

@pytest.mark.parametrize("endpoint", ["json", "jsonl"])
def test_raises_http_bad_request_on_decode_error(self, endpoint):
data = "this is not a valid json nor jsonl"
resp = self.client.simulate_post(f"/{endpoint}", body=data)
resp = self.client.post(f"/{endpoint}", body=data)
assert resp.status_code == 400

@responses.activate
Expand Down

0 comments on commit 2544fcc

Please sign in to comment.