Page MenuHomePhabricator
Paste P9045

test.js
ActivePublic

Authored by Eevans on Thu, Sep 5, 10:21 PM.
const assert = require('assert');
const request = require('supertest');
const TEST_URL = process.env.TEST_URL;
function randomString(len) {
var result = '';
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charsLen = chars.length;
for (let i = 0; i < len; i++) {
result += chars.charAt(Math.floor(Math.random() * charsLen));
}
return result;
}
function newValue(len) {
return `KASK:V:TEST_${randomString(len)}`;
}
describe('CRUD', function() {
const key = `KASK:K:TEST_${randomString(64)}`;
const val = newValue(128);
it('creates', function() {
// GET should 404 (key has not yet been written)
request(TEST_URL)
.get(`/${key}`)
.expect(404);
request(TEST_URL)
.post(`/${key}`)
.set('Content-Type', 'application/octet-stream')
.send(val)
.expect(201);
});
it('reads', function() {
request(TEST_URL)
.get(`/${key}`)
.expect('Content-Type', 'application/octet-stream')
.expect(200)
.expect(response => assert.equal(response.body, val));
});
it('updates', function() {
const v = newValue(128);
request(TEST_URL)
.post(`/${key}`)
.set('Content-Type', 'application/octet-stream')
.send(v)
.expect(201);
request(TEST_URL)
.get(`/${key}`)
.expect('Content-Type', 'application/octet-stream')
.expect(200)
.expect(response => assert.equal(response.body, v));
});
it('deletes', function() {
request(TEST_URL)
.delete(`/${key}`)
.expect(204);
request(TEST_URL)
.get(`/${key}`)
.expect(404);
});
});

Event Timeline

Eevans edited the content of this paste. (Show Details)Thu, Sep 5, 10:21 PM
Eevans changed the title of this paste from untitled to kask_test.
Eevans changed the title of this paste from kask_test to test.js.Thu, Sep 5, 10:22 PM
Eevans updated the paste's language from autodetect to js.