From 197fcbda00affeef509ef95afde0fc54ec9be1d8 Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Fri, 8 Oct 2021 16:44:40 -0400 Subject: [PATCH] refactor: modernize HTTPRequest tests (#7604) --- CHANGELOG.md | 1 + spec/HTTPRequest.spec.js | 196 +++++++++++++++++---------------------- 2 files changed, 86 insertions(+), 111 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42858f691a..9ba4f42830 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -157,6 +157,7 @@ ___ - Allow setting descending sort to full text queries (dblythy) [#7496](https://github.com/parse-community/parse-server/pull/7496) - Allow cloud string for ES modules (Daniel Blyth) [#7560](https://github.com/parse-community/parse-server/pull/7560) - docs: Introduce deprecation ID for reference in comments and online search (Manuel Trezza) [#7562](https://github.com/parse-community/parse-server/pull/7562) +- refactor: Modernize HTTPRequest tests (brandongregoryscott) [#7604](https://github.com/parse-community/parse-server/pull/7604) - Allow liveQuery on Session class (Daniel Blyth) [#7554](https://github.com/parse-community/parse-server/pull/7554) ## 4.10.4 diff --git a/spec/HTTPRequest.spec.js b/spec/HTTPRequest.spec.js index efd133a236..f218ff3c91 100644 --- a/spec/HTTPRequest.spec.js +++ b/spec/HTTPRequest.spec.js @@ -6,7 +6,7 @@ const httpRequest = require('../lib/cloud-code/httpRequest'), express = require('express'); const port = 13371; -const httpRequestServer = 'http://localhost:' + port; +const httpRequestServer = `http://localhost:${port}`; function startServer(done) { const app = express(); @@ -51,167 +51,136 @@ describe('httpRequest', () => { server.close(done); }); - it('should do /hello', done => { - httpRequest({ - url: httpRequestServer + '/hello', - }).then(function (httpResponse) { - expect(httpResponse.status).toBe(200); - expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}')); - expect(httpResponse.text).toEqual('{"response":"OK"}'); - expect(httpResponse.data.response).toEqual('OK'); - done(); - }, done.fail); + it('should do /hello', async () => { + const httpResponse = await httpRequest({ + url: `${httpRequestServer}/hello`, + }); + + expect(httpResponse.status).toBe(200); + expect(httpResponse.buffer).toEqual(Buffer.from('{"response":"OK"}')); + expect(httpResponse.text).toEqual('{"response":"OK"}'); + expect(httpResponse.data.response).toEqual('OK'); }); - it('should do not follow redirects by default', done => { - httpRequest({ - url: httpRequestServer + '/301', - }).then(function (httpResponse) { - expect(httpResponse.status).toBe(301); - done(); - }, done.fail); + it('should do not follow redirects by default', async () => { + const httpResponse = await httpRequest({ + url: `${httpRequestServer}/301`, + }); + + expect(httpResponse.status).toBe(301); }); - it('should follow redirects when set', done => { - httpRequest({ - url: httpRequestServer + '/301', + it('should follow redirects when set', async () => { + const httpResponse = await httpRequest({ + url: `${httpRequestServer}/301`, followRedirects: true, - }).then(function (httpResponse) { - expect(httpResponse.status).toBe(200); - expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}')); - expect(httpResponse.text).toEqual('{"response":"OK"}'); - expect(httpResponse.data.response).toEqual('OK'); - done(); - }, done.fail); + }); + + expect(httpResponse.status).toBe(200); + expect(httpResponse.buffer).toEqual(Buffer.from('{"response":"OK"}')); + expect(httpResponse.text).toEqual('{"response":"OK"}'); + expect(httpResponse.data.response).toEqual('OK'); }); - it('should fail on 404', done => { - let calls = 0; - httpRequest({ - url: httpRequestServer + '/404', - }).then( - function () { - calls++; - fail('should not succeed'); - done(); - }, - function (httpResponse) { - calls++; - expect(calls).toBe(1); - expect(httpResponse.status).toBe(404); - expect(httpResponse.buffer).toEqual(new Buffer('NO')); - expect(httpResponse.text).toEqual('NO'); - expect(httpResponse.data).toBe(undefined); - done(); - } + it('should fail on 404', async () => { + await expectAsync( + httpRequest({ + url: `${httpRequestServer}/404`, + }) + ).toBeRejectedWith( + jasmine.objectContaining({ + status: 404, + buffer: Buffer.from('NO'), + text: 'NO', + data: undefined, + }) ); }); - it('should post on echo', done => { - httpRequest({ + it('should post on echo', async () => { + const httpResponse = await httpRequest({ method: 'POST', - url: httpRequestServer + '/echo', + url: `${httpRequestServer}/echo`, body: { foo: 'bar', }, headers: { 'Content-Type': 'application/json', }, - }).then( - function (httpResponse) { - expect(httpResponse.status).toBe(200); - expect(httpResponse.data).toEqual({ foo: 'bar' }); - done(); - }, - function () { - fail('should not fail'); - done(); - } - ); + }); + + expect(httpResponse.status).toBe(200); + expect(httpResponse.data).toEqual({ foo: 'bar' }); }); - it('should encode a query string body by default', done => { + it('should encode a query string body by default', () => { const options = { body: { foo: 'bar' }, }; const result = httpRequest.encodeBody(options); + expect(result.body).toEqual('foo=bar'); expect(result.headers['Content-Type']).toEqual('application/x-www-form-urlencoded'); - done(); }); - it('should encode a JSON body', done => { + it('should encode a JSON body', () => { const options = { body: { foo: 'bar' }, headers: { 'Content-Type': 'application/json' }, }; const result = httpRequest.encodeBody(options); + expect(result.body).toEqual('{"foo":"bar"}'); - done(); }); - it('should encode a www-form body', done => { + + it('should encode a www-form body', () => { const options = { body: { foo: 'bar', bar: 'baz' }, headers: { 'cOntent-tYpe': 'application/x-www-form-urlencoded' }, }; const result = httpRequest.encodeBody(options); + expect(result.body).toEqual('foo=bar&bar=baz'); - done(); }); - it('should not encode a wrong content type', done => { + + it('should not encode a wrong content type', () => { const options = { body: { foo: 'bar', bar: 'baz' }, headers: { 'cOntent-tYpe': 'mime/jpeg' }, }; const result = httpRequest.encodeBody(options); + expect(result.body).toEqual({ foo: 'bar', bar: 'baz' }); - done(); }); - it('should fail gracefully', done => { - httpRequest({ - url: 'http://not a good url', - }).then(done.fail, function (error) { - expect(error).not.toBeUndefined(); - expect(error).not.toBeNull(); - done(); - }); + it('should fail gracefully', async () => { + await expectAsync( + httpRequest({ + url: 'http://not a good url', + }) + ).toBeRejected(); }); - it('should params object to query string', done => { - httpRequest({ - url: httpRequestServer + '/qs', + it('should params object to query string', async () => { + const httpResponse = await httpRequest({ + url: `${httpRequestServer}/qs`, params: { foo: 'bar', }, - }).then( - function (httpResponse) { - expect(httpResponse.status).toBe(200); - expect(httpResponse.data).toEqual({ foo: 'bar' }); - done(); - }, - function () { - fail('should not fail'); - done(); - } - ); + }); + + expect(httpResponse.status).toBe(200); + expect(httpResponse.data).toEqual({ foo: 'bar' }); }); - it('should params string to query string', done => { - httpRequest({ - url: httpRequestServer + '/qs', + it('should params string to query string', async () => { + const httpResponse = await httpRequest({ + url: `${httpRequestServer}/qs`, params: 'foo=bar&foo2=bar2', - }).then( - function (httpResponse) { - expect(httpResponse.status).toBe(200); - expect(httpResponse.data).toEqual({ foo: 'bar', foo2: 'bar2' }); - done(); - }, - function () { - fail('should not fail'); - done(); - } - ); + }); + + expect(httpResponse.status).toBe(200); + expect(httpResponse.data).toEqual({ foo: 'bar', foo2: 'bar2' }); }); it('should not crash with undefined body', () => { @@ -230,6 +199,7 @@ describe('httpRequest', () => { const serialized = JSON.stringify(httpResponse); const result = JSON.parse(serialized); + expect(result.text).toBe('hello'); expect(result.data).toBe(undefined); expect(result.body).toBe(undefined); @@ -251,43 +221,47 @@ describe('httpRequest', () => { }); it('serialized httpResponse correctly with body buffer string', () => { - const httpResponse = new HTTPResponse({}, new Buffer('hello')); + const httpResponse = new HTTPResponse({}, Buffer.from('hello')); expect(httpResponse.text).toBe('hello'); expect(httpResponse.data).toBe(undefined); const serialized = JSON.stringify(httpResponse); const result = JSON.parse(serialized); + expect(result.text).toBe('hello'); expect(result.data).toBe(undefined); }); it('serialized httpResponse correctly with body buffer JSON Object', () => { const json = '{"foo":"bar"}'; - const httpResponse = new HTTPResponse({}, new Buffer(json)); + const httpResponse = new HTTPResponse({}, Buffer.from(json)); const serialized = JSON.stringify(httpResponse); const result = JSON.parse(serialized); + expect(result.text).toEqual('{"foo":"bar"}'); expect(result.data).toEqual({ foo: 'bar' }); }); it('serialized httpResponse with Parse._encode should be allright', () => { const json = '{"foo":"bar"}'; - const httpResponse = new HTTPResponse({}, new Buffer(json)); + const httpResponse = new HTTPResponse({}, Buffer.from(json)); const encoded = Parse._encode(httpResponse); let foundData, foundText, foundBody = false; + for (const key in encoded) { - if (key == 'data') { + if (key === 'data') { foundData = true; } - if (key == 'text') { + if (key === 'text') { foundText = true; } - if (key == 'body') { + if (key === 'body') { foundBody = true; } } + expect(foundData).toBe(true); expect(foundText).toBe(true); expect(foundBody).toBe(false);