SUPER REQUEST
Supported Protocols

Supported Protocols

SuperRequest supports the main HTTP methods, allowing you to test all types of requests.

Get Protocol

Simulates a GET request to retrieve data.

const app = v.App()
 
app.get("/data", (req: v.Request, res: v.Response) => {
  res.status(200).send({ message: "Hello, GET!" })
})
 
describe("Get protocol", () => {
  it("Should return data with GET", async () => {
    const response = await v.superRequest(app).get("/data")
 
    expect(response.statusCode).toEqual(200)
    expect(response.data).toEqual({ message: "Hello, GET!" })
 
    app.close() // closes all app processes
  })
})

Post Protocol

Used to send data to the server.

const app = v.App()
 
app.post("/submit", (req: v.Request, res: v.Response) => {
  res.status(201).send(req.body)
})
 
describe("Post protocol", () => {
  it("Should send data with POST", async () => {
    const data = { title: "New Post" }
 
    const response = await v.superRequest(app).post("/submit", data)
 
    expect(response.statusCode).toEqual(201)
    expect(response.data).toEqual(data)
 
    app.close() // closes all app processes
  })
})

Put Protocol

Used for data update.

const app = v.App()
 
app.put("/update", (req: v.Request, res: v.Response) => {
  res.status(200).send(req.body)
})
 
describe("Put protocol", () => {
  it("Should update data with PUT", async () => {
    const data = { id: 1, name: "Updated Name" }
 
    const response = await v.superRequest(app).put("/update", data)
 
    expect(response.statusCode).toEqual(200)
    expect(response.data).toEqual(data)
 
    app.close() // closes all app processes
  })
})

Patch Protocol

Used for partial data updates.

const app = v.App()
 
app.patch("/modify", (req: v.Request, res: v.Response) => {
  res.status(200).send(req.body)
})
 
describe("Patch protocol", () => {
  it("Should partially update with PATCH", async () => {
    const data = { name: "Partially Updated" }
 
    const response = await v.superRequest(app).patch("/modify", data)
 
    expect(response.statusCode).toEqual(200)
    expect(response.data).toEqual(data)
 
    app.close() // closes all app processes
  })
})

Delete Protocol

Simulates a DELETE request to remove a resource.

const app = v.App()
 
app.delete("/delete/:id", (req: v.Request, res: v.Response) => {
  res.status(200).send({ deletedId: req.params.id })
})
 
describe("Delete protocol", () => {
  it("Should delete resource with DELETE", async () => {
    const response = await v.superRequest(app).delete("/delete/1")
 
    expect(response.statusCode).toEqual(200)
    expect(response.data).toEqual({ deletedId: "1" })
 
    app.close() // closes all app processes
  })
})

Head Protocol

Used to retrieve only the headers of a response, without the body.

const app = v.App()
 
app.head("/info", (req: v.Request, res: v.Response) => {
  res.status(200).setHeader("X-Custom-Header", "CustomValue").end()
})
 
describe("Head protocol", () => {
  it("Should return headers with HEAD", async () => {
    const response = await v.superRequest(app).head("/info")
 
    expect(response.statusCode).toEqual(200)
    expect(response.headers["x-custom-header"]).toEqual("CustomValue")
 
    app.close() // closes all app processes
  })
})

Options Protocol

Allows checking the supported methods for a route.

const app = v.App()
 
app.options("/options", (req: v.Request, res: v.Response) => {
  res.setHeader("Allow", "GET,POST,OPTIONS").status(204).end()
})
 
describe("Options protocol", () => {
  it("Should return supported methods with OPTIONS", async () => {
    const response = await v.superRequest(app).options("/options")
 
    expect(response.statusCode).toEqual(204)
    expect(response.headers.allow).toEqual("GET,POST,OPTIONS")
 
    app.close() // closes all app processes
  })
})
Copyright © 2024 - 2024 MIT by Mario Elvio