test_client.py•2.54 kB
"""
Test client for the MCP Keyword Search Server
"""
import requests
import json
def test_search(file_path: str, keyword: str, case_sensitive: bool = False):
"""Test the search endpoint"""
url = "http://127.0.0.1:8000/search"
payload = {
"file_path": file_path,
"keyword": keyword,
"case_sensitive": case_sensitive
}
try:
response = requests.post(url, json=payload)
response.raise_for_status()
result = response.json()
print(f"\n{'='*60}")
print(f"Search Results")
print(f"{'='*60}")
print(f"File: {result['file_path']}")
print(f"Keyword: '{result['keyword']}'")
print(f"Case Sensitive: {case_sensitive}")
print(f"Total Matches: {result['total_matches']}")
print(f"{'='*60}\n")
if result['matches']:
for match in result['matches']:
print(f"Line {match['line_number']:3d} ({match['occurrences']} occurrence(s)): {match['content']}")
else:
print("No matches found.")
print(f"\n{'='*60}\n")
except requests.exceptions.ConnectionError:
print("Error: Could not connect to the server. Make sure it's running at http://127.0.0.1:8000")
except requests.exceptions.HTTPError as e:
print(f"HTTP Error: {e}")
print(f"Response: {response.text}")
except Exception as e:
print(f"Error: {e}")
def test_health():
"""Test the health endpoint"""
try:
response = requests.get("http://127.0.0.1:8000/health")
response.raise_for_status()
print(f"Health Check: {response.json()}")
except Exception as e:
print(f"Health check failed: {e}")
if __name__ == "__main__":
# Test health endpoint
print("Testing health endpoint...")
test_health()
# Test case-insensitive search
print("\n\n--- Test 1: Case-insensitive search for 'python' ---")
test_search("example.txt", "python", case_sensitive=False)
# Test case-sensitive search
print("\n--- Test 2: Case-sensitive search for 'Python' ---")
test_search("example.txt", "Python", case_sensitive=True)
# Test search for another keyword
print("\n--- Test 3: Search for 'search' ---")
test_search("example.txt", "search", case_sensitive=False)
# Test with non-existent file
print("\n--- Test 4: Search in non-existent file ---")
test_search("nonexistent.txt", "test", case_sensitive=False)