awaBerry Testing Guide -> Testing: httpservertest

Testing: httpservertest

Summary

This page outlines the process for testing the HTTP server component. It involves starting a local Node.js HTTP server and then executing a test client script. The test client (testHttpServer.js) connects to the server using projectKey, projectSecret, and deviceName from a .env file, and subsequently sends an execute command (defaulting to 'date') to retrieve information from a configured device.

Description

Prerequisites

Ensure your .env file contains the following environment variables:

  • projectKey: Your project's unique key.
  • projectSecret: Your project's secret.
  • deviceName: The name of the device to connect to.

The testHttpServer.js script will check for these variables and report an error if they are missing.

Starting the HTTP Server

First, navigate to the project directory and run the HTTP server using Node.js:

node httpserver/server.js

Upon successful execution, you should see output similar to:

MCP Server running on http://localhost:8080
Endpoints:
  POST /connect
  POST /execute

Running the Test Client

In a separate terminal, execute the test client script:

node test/httpservertest/testHttpServer.js

The expected output from this command will be the current date, as received from the configured device via the 'date' command.

Test Client (testHttpServer.js) Overview

The testHttpServer.js script performs the following actions:

  1. Configuration Loading: It loads environment variables (projectKey, projectSecret, deviceName, and an optional command) from the .env file located two directories up from the script's location.
  2. Connection Phase: It attempts to connect to the HTTP server (http://localhost:8090/connect) using the configured projectKey, projectSecret, and deviceName.
  3. Command Execution Phase: If the connection is successful (indicated by a 'connected' status in the response), it extracts the sessionToken and deviceuuid from the connection response. It then sends an execute command (defaulting to 'date' or using the command variable from .env) to the server's /execute endpoint, along with the sessionToken and deviceuuid.
  4. Error Handling: The script includes basic error handling for missing environment variables and network-related issues during the fetch operations.