diff options
| author | Emmanuel Gaillot <emmanuel.gaillot@gmail.com> | 2025-12-20 22:27:27 +0100 |
|---|---|---|
| committer | Emmanuel Gaillot <emmanuel.gaillot@gmail.com> | 2025-12-20 22:27:27 +0100 |
| commit | 2ac732dc527d53974d65a5afbb33c452a567797b (patch) | |
| tree | be422f4d47652dd92552302c9bcb5bd954839cf5 /lambda-calcul | |
| parent | 1b1877e3328170fe839d2f8ca4d108ec2b0a0454 (diff) | |
| download | lambda-nantes-2ac732dc527d53974d65a5afbb33c452a567797b.tar.gz | |
Post solution
Diffstat (limited to 'lambda-calcul')
| -rw-r--r-- | lambda-calcul/javascript/index.js | 3 | ||||
| -rw-r--r-- | lambda-calcul/javascript/src/client.js | 10 | ||||
| -rw-r--r-- | lambda-calcul/javascript/test/client.test.js | 26 |
3 files changed, 37 insertions, 2 deletions
diff --git a/lambda-calcul/javascript/index.js b/lambda-calcul/javascript/index.js index c01c39e..d8b5e68 100644 --- a/lambda-calcul/javascript/index.js +++ b/lambda-calcul/javascript/index.js @@ -15,7 +15,8 @@ const solveNextChallenge = (token) => client .nextChallenge(token) .then(challenge => console.log(`Next challenge: ${challenge}`) || challenge) .then(client.solve) - .then(solution => console.log(`… solved into: ${solution}`)); + .then(solution => console.log(`Posting solution: ${solution}`) || solution) + .then(solution => client.postSolution(token, solution)); const waitForEnterToBePressed = () => { const readlineInterface = createInterface({ diff --git a/lambda-calcul/javascript/src/client.js b/lambda-calcul/javascript/src/client.js index 08b967d..e6b4fd3 100644 --- a/lambda-calcul/javascript/src/client.js +++ b/lambda-calcul/javascript/src/client.js @@ -16,11 +16,18 @@ const newClient = ({ }) .then(({ data: { expr } }) => expr); + const postSolution = (token, solution) => axios({ + method: 'POST', + url: `${challengerURL}/api/challenge?token=${token}`, + headers: { 'Content-Type': 'application/json;charset=utf-8' }, + data: solution, + }); + const solve = (challenge) => { const parsedExpression = parser.parse(challenge); const evaluatedExpression = interpreter.evaluate(parsedExpression); return printer.print(evaluatedExpression); - } + }; const start = () => axios({ method: 'POST', @@ -37,6 +44,7 @@ const newClient = ({ return { nextChallenge, + postSolution, solve, start, unregister, diff --git a/lambda-calcul/javascript/test/client.test.js b/lambda-calcul/javascript/test/client.test.js index 8a5ebff..a001c6f 100644 --- a/lambda-calcul/javascript/test/client.test.js +++ b/lambda-calcul/javascript/test/client.test.js @@ -80,6 +80,32 @@ describe('λ-calculator', () => { expect(client.solve('Some challenge')).toBe('Expression to print'); }); + it('posts solution', () => { + let challengerCalled = false; + + challenger.on('request', (request, response) => { + challengerCalled = true; + expect(request.headers).toHaveProperty('content-type', 'application/json;charset=utf-8'); + expect(request.method).toBe('POST'); + expect(request.url).toBe('/api/challenge?token=SomeToken'); + + let payload = ''; + request.on('data', (d) => (payload += d)); + request.on('end', () => { + expect(payload).toEqual('"answer"'); + }); + + response.end(); + }); + + const client = LC.newClient({ + playerName: 'John Smith', + challengerURL: `http://localhost:${port}`, + }); + return client.postSolution('SomeToken', "answer") + .then(() => expect(challengerCalled).toBe(true)); + }); + it('unregisters user', () => { let challengerCalled = false; |
