-
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.
fix: handle bad response format (#232)
* fix(client): handle bad response formats test(client): add unit tests for bad repsonse format handling chore(client): created mock expressjs app for testing timeouts and bad response formats * chore(compose): remove the express-app from docker compose * chore(precommit): disable eslint temporarily
- Loading branch information
1 parent
9ea833f
commit f0a0876
Showing
14 changed files
with
1,367 additions
and
45 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,20 @@ | ||
# Use the official Node.js 16 image as a parent image | ||
FROM node:16 | ||
|
||
# Set the working directory in the container | ||
WORKDIR /usr/src/app | ||
|
||
# Copy package.json and package-lock.json (if available) to the working directory | ||
COPY package*.json ./ | ||
|
||
# Install any dependencies | ||
RUN npm install | ||
|
||
# Bundle your app's source code inside the Docker image | ||
COPY . . | ||
|
||
# Your app binds to port 3000 so you'll use the EXPOSE instruction to have it mapped by the docker daemon | ||
EXPOSE 3000 | ||
|
||
# Define the command to run your app using CMD which defines your runtime | ||
CMD [ "npm", "start" ] |
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 @@ | ||
node_modules |
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 @@ | ||
db.json |
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,7 @@ | ||
# Testing app | ||
|
||
This an ExpressJS app that was designed specifically for testing how the client reacts with bad response formats and request timeouts. | ||
|
||
You can run it locally. | ||
|
||
In a sense, it's a test-mock of the SCON API. |
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,82 @@ | ||
const { v4 } = require("uuid"); | ||
const db = require("./db"); | ||
|
||
const DELAYS = { | ||
// setConsents: true, | ||
// getConsents: true, | ||
// timeout: true, | ||
// origins: true, | ||
}; | ||
|
||
const DELAY_MS = 11000; | ||
|
||
module.exports = function (app) { | ||
app.get("/", (req, res) => { | ||
res.send("<h1>Hello world</h1>"); | ||
}); | ||
|
||
app.get("/timeout", (req, res) => { | ||
if (DELAYS.timeout) { | ||
setTimeout(() => { | ||
res.send("<h1>Timeout</h1>"); | ||
}, DELAY_MS); | ||
} else { | ||
res.send("<h1>Timeout</h1>"); | ||
} | ||
}); | ||
|
||
app.get("/api/v1/origins", (req, res) => { | ||
if (DELAYS.origins) { | ||
setTimeout(() => { | ||
res.send(["http://localhost:3000"]); | ||
}, DELAY_MS); | ||
} else { | ||
res.send(["http://localhost:3000"]); | ||
} | ||
}); | ||
|
||
app.get("/api/v1/consent/:uid", (req, res) => { | ||
const { uid } = req.params; | ||
const consents = db.getConsents(uid); | ||
|
||
if (DELAYS.getConsents) { | ||
setTimeout(() => { | ||
res.send({ consents }); | ||
}, DELAY_MS); | ||
} else { | ||
res.send({ consents }); | ||
} | ||
}); | ||
|
||
app.post("/api/v1/consent/:uid", (req, res) => { | ||
const { uid: paramId } = req.params; | ||
const consents = JSON.parse(req.body.status); | ||
|
||
const uid = paramId || v4(); | ||
|
||
db.updateConsents(uid, consents); | ||
|
||
if (DELAYS.setConsents) { | ||
setTimeout(() => { | ||
res.send({ uid, consents }); | ||
}, DELAY_MS); | ||
} else { | ||
res.send({ uid, consents }); | ||
} | ||
}); | ||
|
||
app.post("/api/v1/consent", (req, res) => { | ||
const consents = JSON.parse(req.body.status); | ||
const uid = v4(); | ||
|
||
db.createConsents(uid, consents); | ||
|
||
if (DELAYS.setConsents) { | ||
setTimeout(() => { | ||
res.send({ uid, consents }); | ||
}, DELAY_MS); | ||
} else { | ||
res.send({ uid, consents }); | ||
} | ||
}); | ||
}; |
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 @@ | ||
const fs = require("fs"); | ||
|
||
const DB_FILE = "db.json"; | ||
|
||
const getDb = () => { | ||
if (fs.existsSync(DB_FILE)) { | ||
return JSON.parse(fs.readFileSync(DB_FILE, "utf-8")); | ||
} else { | ||
// create the db | ||
fs.writeFileSync(DB_FILE, "{}"); | ||
} | ||
}; | ||
|
||
const createConsents = (uid, consents) => { | ||
const db = getDb(); | ||
console.log("db", db); | ||
db[uid] = consents; | ||
fs.writeFileSync(DB_FILE, JSON.stringify(db)); | ||
}; | ||
|
||
const updateConsents = (uid, consents) => { | ||
const db = getDb(); | ||
db[uid] = consents; | ||
fs.writeFileSync(DB_FILE, JSON.stringify(db)); | ||
}; | ||
|
||
const getConsents = (uid) => { | ||
const db = getDb(); | ||
return db[uid] || null; | ||
}; | ||
|
||
module.exports = { | ||
createConsents, | ||
updateConsents, | ||
getConsents, | ||
}; |
Oops, something went wrong.