Skip to content

Commit

Permalink
test: check whether payload has expected shape
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Oct 31, 2023
1 parent b834ed1 commit a39de5f
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions seeder/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var http = require('http');
const http = require('http');
const assert = require('assert');
const wbEdit = require( 'wikibase-edit' )( require( './wikibase-edit.config' ) );

let batchId = 1;
Expand All @@ -11,7 +12,35 @@ http.createServer(function (req, res) {
if (req.method !== 'POST') {
return { status: 405, body: 'Method not allowed' };
}
return { status: 200, body: '1' };
return new Promise((resolve, reject) => {
const body = [];
req
.on('error', (err) => {
reject(err);
})
.on('data', (chunk) => {
body.push(chunk);
})
.on('end', () => {
try {
const jsonBody = JSON.parse(
Buffer.concat(body).toString('utf8')
);
assert(
Array.isArray(jsonBody.batches),
'Expected a `batches` property on the request body.'
);
assert(
jsonBody.batches.length,
'Expected `batches` to be a non-empty array.'
);
} catch (err) {
reject(err);
return;
}
resolve({ status: 200, body: '1' });
})
});
case '/getBatches':
if (req.method !== 'GET') {
return { status: 405, body: 'Method not allowed' };
Expand Down

0 comments on commit a39de5f

Please sign in to comment.