-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Issue #226** This PR introduces a Bash tool that enables using Shortest for API testing --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Sahil Lavingia <sahil.lavingia@gmail.com>
- Loading branch information
1 parent
cfcd7a4
commit 8ad7a78
Showing
15 changed files
with
551 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { NextResponse } from "next/server"; | ||
import { ALLOWED_TEST_BEARER } from "@/lib/constants"; | ||
import { getBearerToken } from "@/lib/utils-server"; | ||
|
||
/** | ||
* Asserts that the bearer token is present in the request | ||
* If yes, returns the request body | ||
*/ | ||
export async function POST(req: Request) { | ||
const token = getBearerToken(req); | ||
|
||
if (!token || token !== ALLOWED_TEST_BEARER) { | ||
return NextResponse.json( | ||
{ message: "Bearer token is missing in cookies" }, | ||
{ status: 401 }, | ||
); | ||
} | ||
|
||
try { | ||
const body = await req.json(); | ||
return NextResponse.json(body); | ||
} catch { | ||
return NextResponse.json( | ||
{ message: "Invalid request body" }, | ||
{ status: 400 }, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { shortest, APIRequest } from "@antiwork/shortest"; | ||
import { ALLOWED_TEST_BEARER, TESTING_API_BASE_URI } from "@/lib/constants"; | ||
|
||
const req = new APIRequest({ | ||
baseURL: TESTING_API_BASE_URI, | ||
extraHTTPHeaders: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
shortest( | ||
"Ensure the request without a bearer token returns a message indicating the absence of the token", | ||
req.fetch({ | ||
url: "/assert-bearer", | ||
method: "POST", | ||
body: JSON.stringify({ flagged: "false" }), | ||
}), | ||
); | ||
|
||
shortest( | ||
`Bearer token is ${ALLOWED_TEST_BEARER}. Ensure the request with a valid bearer token returns request body`, | ||
req.fetch({ | ||
url: "/assert-bearer", | ||
method: "POST", | ||
body: JSON.stringify({ flagged: "true" }), | ||
}), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { shortest } from "@antiwork/shortest"; | ||
import { ALLOWED_TEST_BEARER, TESTING_API_BASE_URI } from "@/lib/constants"; | ||
|
||
// @note you should be authenticated in Clerk to run this test | ||
shortest(` | ||
Test the API POST endpoint ${TESTING_API_BASE_URI}/assert-bearer with body { "flagged": "false" } without providing a bearer token. | ||
Expect the response to indicate that the token is missing | ||
`); | ||
|
||
shortest(` | ||
Test the API POST endpoint ${TESTING_API_BASE_URI}/assert-bearer with body { "flagged": "true" } and the bearer token ${ALLOWED_TEST_BEARER}. | ||
Expect the response to show "flagged": true | ||
`); |
Oops, something went wrong.