-
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.
mock api endpoints added for testcases
- Loading branch information
Showing
7 changed files
with
602 additions
and
19 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
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
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,24 @@ | ||
import express from "express"; | ||
import router from "./routes"; | ||
|
||
const app = express(); | ||
|
||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
|
||
app.use("/", router); | ||
|
||
// error handler | ||
app.use(function (err: any, _req: any, res: any, _next: () => any) { | ||
// set locals, only providing error in development | ||
res.locals.message = err.message; | ||
res.locals.error = err; | ||
|
||
// render the error page | ||
res.status(err.status || 500); | ||
res.json({ error: err }); | ||
}); | ||
|
||
const port = 8080; | ||
app.set("port", port); | ||
app.listen(port, () => console.log("Server ready on port:", port)); |
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,117 @@ | ||
import express from "express"; | ||
import { | ||
GoogleJobMediaIndexingPayload, | ||
IndexNowPayload, | ||
} from "../../lib/types"; | ||
const router = express.Router(); | ||
|
||
//PUT | ||
router.put( | ||
"/webmaster/*", | ||
function (req: any, res: any, _next: () => void) { | ||
try { | ||
let [hostname, _, sitemapUrl] = req.url.split("/").slice(-3); | ||
hostname = decodeURIComponent(hostname); | ||
sitemapUrl = decodeURIComponent(sitemapUrl); | ||
|
||
const isSameOrginMap = hostname | ||
? sitemapUrl | ||
? hostname.includes(new URL(sitemapUrl).hostname) | ||
: false | ||
: false; | ||
|
||
const authtoken = req.headers["authorization"]; | ||
const validAuthToken = authtoken && authtoken.length > 150; | ||
|
||
if (validAuthToken && isSameOrginMap) { | ||
res.status(200).json({ msg: "Success" }); | ||
} else { | ||
res.status(400).json({ msg: "Bad request" }); | ||
} | ||
} catch (err) { | ||
res.status(500).json({ msg: err }); | ||
} | ||
}, | ||
); | ||
|
||
//POST | ||
router.post( | ||
"/google-jobmedia-indexing", | ||
function (req: any, res: any, _next: () => void) { | ||
try { | ||
const payload: GoogleJobMediaIndexingPayload = req.body; | ||
|
||
const { url, type } = payload; | ||
|
||
const validPayload = url | ||
? new URL(url)?.hostname | ||
? type | ||
? true | ||
: false | ||
: false | ||
: false; | ||
|
||
const validType = type === "URL_UPDATED"; | ||
|
||
const authtoken = req.headers["authorization"]; | ||
const validAuthToken = authtoken && authtoken.length > 150; | ||
|
||
if (validPayload && validType && validAuthToken) { | ||
res.status(200).json({ msg: "Good request" }); | ||
} else { | ||
res.status(400).json({ msg: "Required field missing" }); | ||
} | ||
} catch (err) { | ||
res.status(500).json({ error: err }); | ||
} | ||
}, | ||
); | ||
|
||
//POST | ||
router.post("/indexnow", function (req: any, res: any, _next: () => void) { | ||
try { | ||
const payload: IndexNowPayload = req.body; | ||
|
||
console.log("Received Payload:", payload); | ||
|
||
const { host, key, keyLocation, urlList } = payload; | ||
|
||
const isNotFound: string | false = !host | ||
? "Host" | ||
: !key | ||
? "Key" | ||
: !keyLocation | ||
? "Key location" | ||
: !urlList | ||
? "Urlist" | ||
: false; | ||
|
||
const keyIsValid = key.length >= 32; | ||
|
||
const keyLocationIsValid = | ||
(new URL(keyLocation).pathname || false) && //check if its valid url and has path | ||
(new URL(host).hostname || false) && //check if its valid url | ||
keyLocation.includes(host); // check if host intersect with keylocation | ||
|
||
const allUrlsAreValid = Array.isArray(urlList) //check if urlist is valid array | ||
? urlList.every((url) => new URL(url).hostname || false) //check if all given url are valid | ||
: false; | ||
|
||
if ( | ||
!isNotFound && | ||
keyIsValid && | ||
keyLocationIsValid && | ||
allUrlsAreValid | ||
) { | ||
res.status(200).json({ msg: "Good request" }); | ||
} else { | ||
res.status(400).json({ | ||
msg: "Required field missing or urlList must be array or failed at validation", | ||
}); | ||
} | ||
} catch (err) { | ||
res.status(500).json({ error: err }); | ||
} | ||
}); | ||
|
||
export default router; |
Oops, something went wrong.