-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { NextRequest } from "next/server"; | ||
|
||
type IndexNowParams = { | ||
keyFile: string; | ||
}; | ||
|
||
interface IndexNowProps { | ||
params: IndexNowParams; | ||
} | ||
|
||
const BING_INDEXNOW_KEY = process.env.BING_INDEXNOW_KEY; | ||
|
||
export async function GET( | ||
request: NextRequest, | ||
{ params: { keyFile } }: IndexNowProps | ||
): Promise<Response> { | ||
const [fileName, fileType] = keyFile.split("."); | ||
|
||
if (!BING_INDEXNOW_KEY) { | ||
return new Response("No key found", { status: 500 }); | ||
} | ||
|
||
if (fileType !== "txt") { | ||
return new Response("Invalid file type", { status: 400 }); | ||
} | ||
|
||
if (fileName !== BING_INDEXNOW_KEY) { | ||
return new Response("Invalid key", { status: 400 }); | ||
} | ||
|
||
return new Response(BING_INDEXNOW_KEY, { | ||
headers: { | ||
"Content-Type": "text/plain", | ||
}, | ||
}); | ||
} |
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,38 @@ | ||
const indexNowKey = Cypress.env("BING_INDEXNOW_KEY"); | ||
const baseUrl = "/api/indexnow/"; | ||
const keyFile = `${indexNowKey}.txt`; | ||
const invalidFileName = `123.txt`; | ||
const invalidFileType = `${indexNowKey}.pdf`; | ||
|
||
context("GET /api/indexnow/[keyFile]", () => { | ||
it("rejects requests with incorrect file type", () => { | ||
cy.request({ | ||
url: `${baseUrl}${invalidFileType}`, | ||
method: "GET", | ||
failOnStatusCode: false, | ||
}).then(({ status, body }) => { | ||
expect(body).to.eq("Invalid file type"); | ||
expect(status).to.eq(400); | ||
}); | ||
}); | ||
it("rejects requests with incorrect filename", () => { | ||
cy.request({ | ||
url: `${baseUrl}${invalidFileName}`, | ||
method: "GET", | ||
failOnStatusCode: false, | ||
}).then(({ status, body }) => { | ||
expect(body).to.eq("Invalid key"); | ||
expect(status).to.eq(400); | ||
}); | ||
}); | ||
it("returns an indexNow key", () => { | ||
cy.request({ | ||
url: `${baseUrl}${keyFile}`, | ||
method: "GET", | ||
}).then(({ status, body, headers }) => { | ||
expect(body).to.eq(indexNowKey); | ||
expect(headers["content-type"]).to.eq("text/plain"); | ||
expect(status).to.eq(200); | ||
}); | ||
}); | ||
}); |