How to use actualBody method in wpt

Best JavaScript code snippet using wpt

question.js

Source:question.js Github

copy

Full Screen

1// npm packages2import request from 'supertest';3import moment from 'moment';4// our packages5import app from '../src/app';6export default (test) => {7 const sharedInput = {text: 'Do you like my aweful coding?', expirationDate: moment().add(1, 'days').toDate()};8 const sharedInputOther = {text: 'Do you like things?', expirationDate: moment().add(2, 'days').toDate()};9 const updatedInput = {text: 'Update text question?', expirationDate: moment().add(3, 'days').toDate()};10 test('POST /api/question - should not create new question without text', (t) => {11 const input = {text: undefined, expirationDate: moment().add(1, 'days').toDate()};12 request(app)13 .post('/api/question')14 .set('x-access-token', app.get('token'))15 .send(input)16 .expect(400)17 .expect('Content-Type', /json/)18 .end((err, res) => {19 const expectedBody = {error: 'Text should be present!'};20 const actualBody = res.body;21 t.error(err, 'No error');22 t.deepEqual(actualBody, expectedBody, 'Retrieve correct error');23 t.end();24 });25 });26 test('POST /api/question - should not create new question with malformed date', (t) => {27 const input = {text: 'Am I a question?', expirationDate: 'not a date'};28 request(app)29 .post('/api/question')30 .set('x-access-token', app.get('token'))31 .send(input)32 .expect(400)33 .expect('Content-Type', /json/)34 .end((err, res) => {35 const expectedBody = {error: 'Date should be valid ISO Date!'};36 const actualBody = res.body;37 t.error(err, 'No error');38 t.deepEqual(actualBody, expectedBody, 'Retrieve correct error');39 t.end();40 });41 });42 test('GET /api/question - should get empty latest questions', (t) => {43 request(app)44 .get('/api/question')45 .set('x-access-token', app.get('token'))46 .expect(200)47 .expect('Content-Type', /json/)48 .end((err, res) => {49 const actualBody = res.body;50 t.error(err, 'No error');51 t.equal(actualBody.length, 0, 'Retrieve 0 questions');52 t.end();53 });54 });55 test('POST /api/question - create new question', (t) => {56 request(app)57 .post('/api/question')58 .set('x-access-token', app.get('token'))59 .send(sharedInput)60 .expect(200)61 .expect('Content-Type', /json/)62 .end((err, res) => {63 const actualBody = res.body;64 t.error(err, 'No error');65 t.equal(actualBody.text, sharedInput.text, 'Retrieve same question text');66 t.equal(actualBody.owner, app.get('user').id, 'Question belongs to correct user');67 t.ok(moment(actualBody.creationDate).isValid(), 'Creation date must be valid');68 t.ok(69 moment(actualBody.expirationDate).isSame(sharedInput.expirationDate),70 'Retrieve same question expirationDate'71 );72 app.set('question', actualBody);73 t.end();74 });75 });76 test('POST /api/question/:id/answer - answer existing question', (t) => {77 const answer = 'test answer';78 request(app)79 .post(`/api/question/${app.get('question').id}/answer`)80 .set('x-access-token', app.get('token'))81 .send({answer})82 .expect(200)83 .expect('Content-Type', /json/)84 .end((err, res) => {85 const actualBody = res.body;86 t.error(err, 'No error');87 t.equal(actualBody.answers.length, 1, 'Retrieve one answer');88 t.equal(actualBody.answers[0].answer, answer, 'Retrieve same answer');89 app.set('question', actualBody);90 t.end();91 });92 });93 test('POST /api/question - create new question with different user', (t) => {94 request(app)95 .post('/api/question')96 .set('x-access-token', app.get('other-token'))97 .send(sharedInputOther)98 .expect(200)99 .expect('Content-Type', /json/)100 .end((err, res) => {101 const actualBody = res.body;102 t.error(err, 'No error');103 t.equal(actualBody.text, sharedInputOther.text, 'Retrieve same question text');104 t.equal(actualBody.owner, app.get('other-user').id, 'Question belongs to correct user');105 t.ok(moment(actualBody.creationDate).isValid(), 'Creation date must be valid');106 t.ok(107 moment(actualBody.expirationDate).isSame(sharedInputOther.expirationDate),108 'Retrieve same question expirationDate'109 );110 app.set('other-question', actualBody);111 t.end();112 });113 });114 test('GET /api/question - get latest questions', (t) => {115 request(app)116 .get('/api/question')117 .set('x-access-token', app.get('token'))118 .expect(200)119 .expect('Content-Type', /json/)120 .end((err, res) => {121 const actualBody = res.body;122 t.error(err, 'No error');123 t.equal(actualBody.length, 2, 'Retrieve 2 questions');124 t.equal(actualBody[0].text, sharedInputOther.text, 'Retrieve same question text');125 t.equal(actualBody[0].owner, app.get('other-user').id, 'Question belongs to correct user');126 t.ok(moment(actualBody[0].creationDate).isValid(), 'Creation date must be valid');127 t.ok(128 moment(actualBody[0].expirationDate).isSame(sharedInputOther.expirationDate),129 'Retrieve same question expirationDate'130 );131 t.equal(actualBody[1].text, sharedInput.text, 'Retrieve same question text');132 t.equal(actualBody[1].owner, app.get('user').id, 'Question belongs to correct user');133 t.ok(moment(actualBody[1].creationDate).isValid(), 'Creation date must be valid');134 t.ok(135 moment(actualBody[1].expirationDate).isSame(sharedInput.expirationDate),136 'Retrieve same question expirationDate'137 );138 t.end();139 });140 });141 test('GET /api/question/:id - get question', (t) => {142 request(app)143 .get(`/api/question/${app.get('question').id}`)144 .set('x-access-token', app.get('token'))145 .expect(200)146 .expect('Content-Type', /json/)147 .end((err, res) => {148 const actualBody = res.body;149 t.error(err, 'No error');150 t.equal(actualBody.text, sharedInput.text, 'Retrieve same question text');151 t.equal(actualBody.owner, app.get('user').id, 'Question belongs to correct user');152 t.ok(moment(actualBody.creationDate).isValid(), 'Creation date must be valid');153 t.ok(154 moment(actualBody.expirationDate).isSame(sharedInput.expirationDate),155 'Retrieve same question expirationDate'156 );157 t.end();158 });159 });160 test('POST /api/question/:id - should not update question without text', (t) => {161 request(app)162 .post(`/api/question/${app.get('question').id}`)163 .set('x-access-token', app.get('token'))164 .send({text: ''})165 .expect(400)166 .expect('Content-Type', /json/)167 .end((err, res) => {168 const expectedBody = {error: 'Text should be not empty!'};169 const actualBody = res.body;170 t.error(err, 'No error');171 t.deepEqual(actualBody, expectedBody, 'Retrieve correct error');172 t.end();173 });174 });175 test('POST /api/question/:id - should not update question with invalid date', (t) => {176 request(app)177 .post(`/api/question/${app.get('question').id}`)178 .set('x-access-token', app.get('token'))179 .send({expirationDate: 'not a date'})180 .expect(400)181 .expect('Content-Type', /json/)182 .end((err, res) => {183 const expectedBody = {error: 'Date should be valid ISO Date!'};184 const actualBody = res.body;185 t.error(err, 'No error');186 t.deepEqual(actualBody, expectedBody, 'Retrieve correct error');187 t.end();188 });189 });190 test('POST /api/question/:id - should not update non-existent question', (t) => {191 request(app)192 .post('/api/question/123')193 .set('x-access-token', app.get('token'))194 .send({text: 'Question?', expirationDate: moment().toDate()})195 .expect(400)196 .expect('Content-Type', /json/)197 .end((err, res) => {198 const actualBody = res.body;199 t.error(err, 'No error');200 t.ok(actualBody.error.indexOf('DocumentNotFoundError') !== -1, 'Retrieve correct error');201 t.end();202 });203 });204 test('POST /api/question/:id - should not update question of non-owner', (t) => {205 request(app)206 .post(`/api/question/${app.get('other-question').id}`)207 .set('x-access-token', app.get('token'))208 .send({text: 'Question?', expirationDate: moment().toDate()})209 .expect(403)210 .expect('Content-Type', /json/)211 .end((err, res) => {212 const expectedBody = {error: 'Not enough rights to change the question!'};213 const actualBody = res.body;214 t.error(err, 'No error');215 t.deepEqual(actualBody, expectedBody, 'Retrieve correct error');216 t.end();217 });218 });219 test('POST /api/question/:id - should get question back if same data is sent', (t) => {220 request(app)221 .post(`/api/question/${app.get('question').id}`)222 .set('x-access-token', app.get('token'))223 .send(sharedInput)224 .expect(200)225 .expect('Content-Type', /json/)226 .end((err, res) => {227 const expectedBody = app.get('question');228 const actualBody = res.body;229 t.error(err, 'No error');230 t.deepEqual(actualBody, expectedBody, 'Retrieve same question');231 t.end();232 });233 });234 test('POST /api/question/:id - should update question with new text', (t) => {235 request(app)236 .post(`/api/question/${app.get('question').id}`)237 .set('x-access-token', app.get('token'))238 .send({text: updatedInput.text})239 .expect(200)240 .expect('Content-Type', /json/)241 .end((err, res) => {242 const expectedBody = {243 ...app.get('question'),244 text: updatedInput.text,245 };246 const actualBody = res.body;247 t.error(err, 'No error');248 t.deepEqual(actualBody, expectedBody, 'Retrieve same question');249 t.end();250 });251 });252 test('POST /api/question/:id - should update question with new date', (t) => {253 request(app)254 .post(`/api/question/${app.get('question').id}`)255 .set('x-access-token', app.get('token'))256 .send({expirationDate: updatedInput.expirationDate})257 .expect(200)258 .expect('Content-Type', /json/)259 .end((err, res) => {260 const expectedBody = {261 ...app.get('question'),262 ...updatedInput,263 };264 const actualBody = res.body;265 // get dates266 const actualDate = actualBody.expirationDate;267 const expectedDate = expectedBody.expirationDate;268 // delete from objects269 delete actualBody.expirationDate;270 delete expectedBody.expirationDate;271 // compare272 t.error(err, 'No error');273 t.deepEqual(actualBody, expectedBody, 'Retrieve same question');274 t.ok(moment(actualDate).isSame(expectedDate), 'Retrieve same dates');275 t.end();276 });277 });278 test('DELETE /api/question/:id - should not delete question with different owner', (t) => {279 request(app)280 .delete(`/api/question/${app.get('other-question').id}`)281 .set('x-access-token', app.get('token'))282 .expect(403)283 .expect('Content-Type', /json/)284 .end((err, res) => {285 const expectedBody = {error: 'Not enough rights to delete the question!'};286 const actualBody = res.body;287 // compare288 t.error(err, 'No error');289 t.deepEqual(actualBody, expectedBody, 'Retrieve same question');290 t.end();291 });292 });293 test('DELETE /api/question/:id - should delete question', (t) => {294 request(app)295 .delete(`/api/question/${app.get('question').id}`)296 .set('x-access-token', app.get('token'))297 .expect(204)298 .end((err) => {299 // compare300 t.error(err, 'No error');301 // try to get it and expect to fail302 t.test(' - GET /api/question/:id - should fail to get deleted question', (st) => {303 request(app)304 .get(`/api/question/${app.get('question').id}`)305 .set('x-access-token', app.get('token'))306 .expect(400)307 .end((e, res) => {308 const actualBody = res.body;309 st.error(e, 'No error');310 st.ok(actualBody.error.indexOf('DocumentNotFoundError') !== -1, 'Retrieve correct error');311 st.end();312 // end delete test313 t.end();314 });315 });316 });317 });...

Full Screen

Full Screen

user.js

Source:user.js Github

copy

Full Screen

1// npm packages2import request from 'supertest';3import jwt from 'jsonwebtoken';4// our packages5import app from '../src/app';6import {auth as authConfig} from '../config';7export default (test) => {8 test('GET /api/user/:id', (t) => {9 request(app)10 .get(`/api/user/${app.get('user').id}`)11 .set('x-access-token', app.get('token'))12 .expect(200)13 .expect('Content-Type', /json/)14 .end((err, res) => {15 const expectedBody = app.get('user');16 const actualBody = res.body;17 t.error(err, 'No error');18 t.deepEqual(actualBody, expectedBody, 'Retrieve user');19 t.notOk(actualBody.password, 'No password included');20 t.end();21 });22 });23 test('GET /api/user/me', (t) => {24 request(app)25 .get('/api/user/me')26 .set('x-access-token', app.get('token'))27 .expect(200)28 .expect('Content-Type', /json/)29 .end((err, res) => {30 const expectedBody = app.get('user');31 const actualBody = res.body;32 t.error(err, 'No error');33 t.deepEqual(actualBody, expectedBody, 'Retrieve user');34 t.notOk(actualBody.password, 'No password included');35 t.end();36 });37 });38 test('GET /api/user/:id with non-existent id', (t) => {39 request(app)40 .get('/api/user/1234')41 .set('x-access-token', app.get('token'))42 .expect(400)43 .expect('Content-Type', /json/)44 .end((err, res) => {45 const expectedBody = {error: 'User does not exist'};46 const actualBody = res.body;47 t.error(err, 'No error');48 t.deepEqual(actualBody, expectedBody, 'Get correct error');49 t.end();50 });51 });52 test('POST /api/user/:id - should not allow change not self', (t) => {53 request(app)54 .post('/api/user/1234')55 .set('x-access-token', app.get('token'))56 .send({login: 'test123'})57 .expect(403)58 .expect('Content-Type', /json/)59 .end((err, res) => {60 const expectedBody = {error: 'Not enough rights to change other user profile!'};61 const actualBody = res.body;62 t.error(err, 'No error');63 t.deepEqual(actualBody, expectedBody, 'Get correct error');64 t.end();65 });66 });67 test('POST /api/user/:id - update with same data', (t) => {68 request(app)69 .post(`/api/user/${app.get('user').id}`)70 .set('x-access-token', app.get('token'))71 .send({login: 'test', password: '123'})72 .expect(200)73 .expect('Content-Type', /json/)74 .end((err, res) => {75 const expectedBody = app.get('user');76 const actualBody = res.body;77 t.error(err, 'No error');78 t.deepEqual(actualBody, expectedBody, 'Retrieve new user');79 t.notOk(actualBody.password, 'No password included');80 t.end();81 });82 });83 test('POST /api/user/:id - should throw error if new passwords do not match', (t) => {84 request(app)85 .post(`/api/user/${app.get('user').id}`)86 .set('x-access-token', app.get('token'))87 .send({password: '1234', passwordRepeat: '321'})88 .expect(400)89 .expect('Content-Type', /json/)90 .end((err, res) => {91 const expectedBody = {error: 'Passwords do not match!'};92 const actualBody = res.body;93 t.error(err, 'No error');94 t.deepEqual(actualBody, expectedBody, 'Retrieve correct error');95 t.end();96 });97 });98 test('POST /api/user/:id - update with existing login', (t) => {99 request(app)100 .post(`/api/user/${app.get('user').id}`)101 .set('x-access-token', app.get('token'))102 .send({login: 'other'})103 .expect(400)104 .expect('Content-Type', /json/)105 .end((err, res) => {106 const expectedBody = {error: 'Login already taken!'};107 const actualBody = res.body;108 t.error(err, 'No error');109 t.deepEqual(actualBody, expectedBody, 'Retrieve correct error');110 t.end();111 });112 });113 test('POST /api/user/:id - update with new login', (t) => {114 request(app)115 .post(`/api/user/${app.get('user').id}`)116 .set('x-access-token', app.get('token'))117 .send({login: 'test123'})118 .expect(200)119 .expect('Content-Type', /json/)120 .end((err, res) => {121 const expectedBody = {122 ...app.get('user'),123 login: 'test123',124 };125 const actualBody = res.body;126 t.error(err, 'No error');127 t.deepEqual(actualBody, expectedBody, 'Retrieve new user');128 t.notOk(actualBody.password, 'No password included');129 t.end();130 });131 });132 test('POST /api/user/:id - update with new password', (t) => {133 request(app)134 .post(`/api/user/${app.get('user').id}`)135 .set('x-access-token', app.get('token'))136 .send({password: 'asd', passwordRepeat: 'asd'})137 .expect(200)138 .expect('Content-Type', /json/)139 .end((err, res) => {140 const expectedBody = {141 ...app.get('user'),142 login: 'test123',143 };144 const actualBody = res.body;145 t.error(err, 'No error');146 t.deepEqual(actualBody, expectedBody, 'Retrieve new user');147 t.notOk(actualBody.password, 'No password included');148 t.end();149 });150 });151 test('Should login with updated username and password', (t) => {152 request(app)153 .post('/api/login')154 .send({login: 'test123', password: 'asd'})155 .expect(200)156 .expect('Content-Type', /json/)157 .end((err, res) => {158 const actualBody = res.body;159 t.error(err, 'No error');160 t.ok(actualBody.user, 'User exists');161 t.ok(actualBody.token, 'Token exists');162 const decodedUser = jwt.verify(actualBody.token, authConfig.jwtSecret);163 delete decodedUser.iat;164 t.equal(actualBody.user.login, 'test123', 'Login matches request');165 t.notOk(actualBody.user.password, 'No password included');166 t.deepEqual(actualBody.user, decodedUser, 'User must match token');167 app.set('token', actualBody.token);168 app.set('user', actualBody.user);169 t.end();170 });171 });...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var wpt = new WebPageTest('www.webpagetest.org');3 if (err) return console.error(err);4 wpt.getTestResults(data.data.testId, function(err, data) {5 if (err) return console.error(err);6 console.log(data.data.runs[1].firstView.actualBody);7 });8});

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var test = new wpt('API_KEY');3 if (err) return console.log(err);4 test.getTestResults(data.data.testId, function(err, data) {5 if (err) return console.log(err);6 test.getTestResults(data.data.testId, function(err, data) {7 if (err) return console.log(err);8 console.log(data);9 });10 });11});12### runTest(url, options, callback)

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var client = wpt('www.webpagetest.org');3client.runTest(url, {4 lighthouseConfig: {5 "screenEmulation": {6 },7 "throttling": {8 },9 }10}, function (err, data) {11 if (err) return console.error(err);12 console.log(data.data.median.firstView.actualBody);13});14var wpt = require('webpagetest');15var client = wpt('www.webpagetest.org');16client.runTest(url, {17 lighthouseConfig: {18 "screenEmulation": {19 },

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('webpagetest');2var webPageTest = new wpt('API_KEY');3var location = "ec2-us-west-1:Chrome";4var options = {5};6webPageTest.runTest(url, options, function(err, data) {7 if (err) {8 console.log(err);9 } else {10 console.log(data);11 var testId = data.data.testId;12 var options = {13 };14 webPageTest.getTestResults(testId, options, function(err, data) {15 if (err) {16 console.log(err);17 } else {18 console.log(data);19 }20 });21 }22});23var wpt = require('webpagetest');24var webPageTest = new wpt('API_KEY');

Full Screen

Using AI Code Generation

copy

Full Screen

1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.1234567890abcdefghijklmnopqrstuv');3wpt.runTest(url, {location: 'Dulles:Chrome'}, function(err, data) {4 if (err) {5 console.log(err);6 } else {7 console.log(data);8 wpt.getTestResults(data.data.testId, function(err, data) {9 if (err) {10 console.log(err);11 } else {12 console.log(data);13 }14 });15 }16});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run wpt automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful