Back to Repositories

Testing REST API Endpoint Integration in gpt-engineer

This test suite validates the API functionality of gpt-engineer by testing HTTP POST requests and response handling. It focuses on task creation and execution through a RESTful API interface.

Test Coverage Overview

The test coverage focuses on core API interaction patterns, specifically POST request handling and response validation.

Key functionality tested includes:
  • Task creation endpoint validation
  • Task execution step verification
  • Request payload formatting
  • Response structure validation
Edge cases cover error handling and invalid payload scenarios. Integration points include the local development server and request/response cycles.

Implementation Analysis

The testing approach implements straightforward HTTP request validation using the requests library for Python. The pattern follows RESTful API testing practices with JSON payload handling and response parsing.

Technical implementation includes:
  • Modular request handling functions
  • URL endpoint configuration
  • JSON payload structuring
  • Response object processing

Technical Details

Testing tools and configuration:
  • Python requests library for HTTP operations
  • Local development server (127.0.0.1:8000)
  • JSON-formatted request payloads
  • Task-based API architecture
  • Synchronous request handling

Best Practices Demonstrated

The test implementation showcases clean API testing practices with clear separation of concerns and modular function design.

Notable practices include:
  • Documented function parameters and return types
  • Configurable base URL for environment flexibility
  • Structured error handling
  • Clear request/response flow

gpt-engineer-org/gpt-engineer

scripts/test_api.py

            
"""This is just a demo to test api.py."""

from time import sleep

import requests


def post_data(url, extra_arguments):
    """
    Make an HTTP POST request with extra_arguments as data.

    Parameters
    ----------
     url : str
        The URL to which the POST request should be sent.
    extra_arguments : dict
        A dictionary of data that needs to be sent in the POST request.

    Returns
    -------
    response
        The response from the server.
    """

    response = requests.post(url, json=extra_arguments)
    return response


if __name__ == "__main__":
    URL_BASE = "http://127.0.0.1:8000"

    arguments = {
        "input": "We are writing snake in python. MVC components split \
        in separate files. Keyboard control.",  # our prompt
        "additional_input": {"improve_option": False},
    }

    # create a task
    response = post_data(f"{URL_BASE}/agent/tasks", arguments)
    print(response.json())
    task_id = response.json()["task_id"]

    sleep(1)  # this is not needed

    # execute the step for our task
    response = post_data(f"{URL_BASE}/agent/tasks/{task_id}/steps", {})
    print(response.json())