Browse Source

feat(friend-support_test): update tests

pull/1526/head
nprimo 2 years ago committed by Niccolò Primo
parent
commit
380ad0dc3e
  1. 131
      js/tests/friend-support_test.mjs
  2. 11
      subjects/friend-support/README.md

131
js/tests/friend-support_test.mjs

@ -1,26 +1,42 @@
import { once } from 'node:events' import { once } from 'node:events'
import * as cp from 'node:child_process' import * as cp from 'node:child_process'
import fs from 'node:fs/promises' import { mkdir, writeFile, chmod } from 'fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'path'
export const tests = [] export const tests = []
const fetch = _fetch // to redefine the real fetch const fetch = _fetch // to redefine the real fetch
const port = 5000 const port = 5000
// tell-me-who_test setup -- see how to have same files in testing directories export const setup = async ({ randStr }) => {
// TO BE IMPLEMENTED const dir = '.'
const sendRequest = async (path, options) => { await mkdir(`${dir}/guests`, { recursive: true })
const response = await fetch(`http://localhost:${port}${path}`, options)
const { status, statusText, ok } = response const randLastName = randStr()
const headers = Object.fromEntries(response.headers)
let body = '' const createFilesIn = ({ files, dirPath }) =>
try { Promise.all(
body = await response.json() files.map(([fileName, content]) =>
} catch (err) { writeFile(`${dirPath}/${fileName}`, JSON.stringify(content)),
body = err ),
)
const sendRequest = async (path, options) => {
const response = await fetch(`http://localhost:${port}${path}`, options)
const { status, statusText, ok } = response
const headers = Object.fromEntries(response.headers)
let body = ''
try {
body = await response.json()
} catch (err) {
body = err
}
return { status, body, headers, randLastName }
} }
return { status, body, headers }
return { tmpPath: dir, createFilesIn, sendRequest }
} }
tests.push(async ({ path, ctx }) => { tests.push(async ({ path, ctx }) => {
@ -35,13 +51,20 @@ tests.push(async ({ path, ctx }) => {
return message[0].toString().includes(port) return message[0].toString().includes(port)
}) })
tests.push(async ({ eq }) => { tests.push(async ({ eq, ctx, randStr }) => {
const { status, body, headers } = await sendRequest('/Zoe_Sierra', { // test for one guest
method: 'GET', const expBody = { message: 'ciao' }
}) const files = [[`mario_${ctx.randLastName}.json`, expBody]]
let cont = await fs.readFile('./guests/Zoe_Sierra.json', { const dirName = `guests`
encoding: 'utf-8', const dirPath = join(ctx.tmpPath, dirName)
}) await ctx.createFilesIn({ dirPath, files })
const { status, body, headers } = await ctx.sendRequest(
`/mario_${ctx.randLastName}`,
{
method: 'GET',
},
)
return eq( return eq(
{ {
status: status, status: status,
@ -50,39 +73,53 @@ tests.push(async ({ eq }) => {
}, },
{ {
status: 200, status: 200,
body: JSON.parse(cont), body: expBody,
contentType: 'application/json', contentType: 'application/json',
}, },
) )
}) })
tests.push(async ({ eq }) => { tests.push(async ({ eq, ctx, randStr }) => {
const { status, body, headers } = await sendRequest('/Zoe_Sierra1', { // test server failed
method: 'GET', // change permission for existing file
}) await chmod(`${ctx.tmpPath}/guests/mario_${ctx.randLastName}.json`, 0)
console.log(body.error) const { status, body, headers } = await ctx.sendRequest(
return eq( `/mario_${ctx.randLastName}`,
{ {
status: status, method: 'GET',
body: body.error.trim(), },
contentType: headers['content-type'], )
}, return eq(
{ {
status: 404, status: status,
body: "guest not found", body: body,
contentType: 'application/json', contentType: headers['content-type'],
}, },
) {
}) status: 500,
body: { error: 'server failed' },
contentType: 'application/json',
},
)
})
// this test must always run to shutdown server tests.push(async ({ eq, ctx }) => {
/* currently breaking if one test fails - it might not be a problem // test guest not there
since the tests are run in a separate Docker image... const { status, body, headers } = await ctx.sendRequest('/andrea_bianchi', {
*/ method: 'GET',
tests.push(({ ctx }) => { })
const { server } = ctx return eq(
server.kill('SIGQUIT') {
return true status: status,
body: body,
contentType: headers['content-type'],
},
{
status: 404,
body: { error: 'guest not found' },
contentType: 'application/json',
},
)
}) })
Object.freeze(tests) Object.freeze(tests)

11
subjects/friend-support/README.md

@ -2,14 +2,15 @@
### Instructions ### Instructions
The workload to organize this party is becoming too much to be handled by a single person. It is time to let a friend support you. The workload to organize this party is becoming too much to be handled by a single person. It is time to let a friend support you.
Create a `friend-support.mjs` program that will open a server to remotely access the guest list stored on your computer. Your program will need to handle HTTP `GET` request. Create a `friend-support.mjs` program that will open a server to remotely access the guest list stored on your computer. Your program will need to handle HTTP `GET` request.
Here below are your program/server's expected behaviors: Here below are your program/server's expected behaviors:
- It has to listen on port `5000` and it will have to print a simple message on the console, specifying the listening port;
- Its HTTP response should always contains a coherent status code depending on the handling of the received HTTP request. More specifically, your server should be able to respond with the following status codes: `200`, `404` and `500`; - It has to listen on port `5000`, and it will have to print a simple message on the console, specifying the listening port;
- The responses will always be JSON and this information should be explicit in the HTTP response; - Its HTTP response should always contain a coherent status code depending on the handling of the received HTTP request. More specifically, your server should be able to respond with the following status codes: `200`, `404` and `500`;
- The responses will always be JSON and this information should be included in the HTTP response;
- For each HTTP request, your program should try to open the corresponding guest JSON file and provide the content as JSON in the HTTP response, if possible. When the guess specified in the request is not found, the server should return an object with the attribute `error` defined as `guest not found`; - For each HTTP request, your program should try to open the corresponding guest JSON file and provide the content as JSON in the HTTP response, if possible. When the guess specified in the request is not found, the server should return an object with the attribute `error` defined as `guest not found`;
- If for any reason the server fails, the response should be an object with an attribute `error` specified as `server failed`. - If for any reason the server fails, the response should be an object with an attribute `error` specified as `server failed`.
@ -34,4 +35,4 @@ curl localhost:5000/Elis_Galindo
### Provided files ### Provided files
Download [`guests.zip`](https://assets.01-edu.org/tell-me-how-many/guests.zip) to have at your disposal the `guests` directory containing the files with the guests information. You must save it in your `friend-support` exercise directory to test your program on it. Download [`guests.zip`](https://assets.01-edu.org/tell-me-how-many/guests.zip) to have at your disposal the `guests` directory containing the files with the guest information. You must save it in your `friend-support` exercise directory to test your program on it.

Loading…
Cancel
Save