Test Client
The Test Client is a comprehensive testing utility designed to simplify HTTP request testing in your application. It supports all standard HTTP methods, various request body formats, headers, query parameters, and file uploads.
Configuration
Section titled “Configuration”import { TestClient } from "harpiats";import { app } from "start/server";
Basic Usage
Section titled “Basic Usage”const client = new TestClient(app);const response = await client.get("/endpoint").execute();
HTTP Methods
Section titled “HTTP Methods”Method | Description | Example |
---|---|---|
.get() | Send GET request | .get("/users") |
.post() | Send POST request | .post("/users") |
.put() | Send PUT request | .put("/users/1") |
.delete() | Send DELETE request | .delete("/users/1") |
.patch() | Send PATCH request | .patch("/users/1") |
.options() | Send OPTIONS request | .options("/users") |
.head() | Send HEAD request | .head("/users") |
Request Configuration
Section titled “Request Configuration”Query Parameters
Section titled “Query Parameters”.query("param", "value")
Headers
Section titled “Headers”.set("Header-Name", "value")
Request Body
Section titled “Request Body”JSON Data
Section titled “JSON Data”.json({ key: "value" })
Form Data
Section titled “Form Data”.formData({ field: "value" })
File Uploads
Section titled “File Uploads”// Single file.file({ file: "path/to/file.jpg" })
// Multiple files.files({ files: ["file1.jpg", "file2.jpg"] })
Raw Data
Section titled “Raw Data”.send("raw data")
Response Handling
Section titled “Response Handling”The .execute()
method returns a response object with:
status
: HTTP status codeheaders
: Response headers- Methods to parse body:
.json()
.text()
.blob()
Error Handling
Section titled “Error Handling”- Mixing incompatible body types (e.g.,
.json()
after.formData()
) throws an error - Non-existent file paths throw an error
- Invalid file types throw an error
Examples
Section titled “Examples”Basic Requests
Section titled “Basic Requests”GET Request with Query Parameters
const response = await new TestClient(app) .get("/hello") .query("number", "123456") .execute();
POST Request with JSON
const response = await new TestClient(app) .post("/users") .json({ name: "John" }) .execute();
File Uploads Examples
Section titled “File Uploads Examples”Single File Upload
const response = await new TestClient(app) .post("/upload") .file({ avatar: "user.jpg" }) .execute();
Multiple Files Upload
const response = await new TestClient(app) .post("/upload") .files({ documents: ["doc1.pdf", "doc2.pdf"] }) .execute();
Advanced Usage
Section titled “Advanced Usage”Custom Headers
const response = await new TestClient(app) .get("/protected") .set("Authorization", "Bearer token123") .execute();
Form Data with File
const response = await new TestClient(app) .post("/profile") .formData({ username: "john_doe" }) .file({ avatar: "profile.jpg" }) .execute();
API Reference
Section titled “API Reference”Constructor
Section titled “Constructor”new TestClient(app: Application)
Methods
Section titled “Methods”- HTTP Methods:
.get()
,.post()
,.put()
,.delete()
,.patch()
,.options()
,.head()
- Configuration:
.query()
,.set()
- Body Methods:
.json()
,.formData()
,.file()
,.files()
,.send()
- Execution:
.execute()