From 55101b35c15430506dec822abf416b26c5fcedb1 Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Sat, 2 Oct 2021 20:38:36 -0400 Subject: [PATCH 1/4] Update HTTPRequest tests to use async/await syntax, cleanup unused done callbacks, etc --- spec/HTTPRequest.spec.js | 174 +++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 100 deletions(-) diff --git a/spec/HTTPRequest.spec.js b/spec/HTTPRequest.spec.js index efd133a236..5e1b197298 100644 --- a/spec/HTTPRequest.spec.js +++ b/spec/HTTPRequest.spec.js @@ -51,64 +51,54 @@ describe('httpRequest', () => { server.close(done); }); - it('should do /hello', done => { - httpRequest({ + it('should do /hello', async () => { + const httpResponse = await 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); + }); + + 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({ + it('should do not follow redirects by default', async () => { + const httpResponse = await httpRequest({ url: httpRequestServer + '/301', - }).then(function (httpResponse) { - expect(httpResponse.status).toBe(301); - done(); - }, done.fail); + }); + + expect(httpResponse.status).toBe(301); }); - it('should follow redirects when set', done => { - httpRequest({ + 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 () => { + try { + await httpRequest({ + url: httpRequestServer + '/404', + }); + + fail('should not succeed'); + } catch (httpResponse) { + expect(httpResponse.status).toBe(404); + expect(httpResponse.buffer).toEqual(Buffer.from('NO')); + expect(httpResponse.text).toEqual('NO'); + expect(httpResponse.data).toBe(undefined); + } }); - it('should post on echo', done => { - httpRequest({ + it('should post on echo', async () => { + const httpResponse = await httpRequest({ method: 'POST', url: httpRequestServer + '/echo', body: { @@ -117,101 +107,85 @@ describe('httpRequest', () => { 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) { + it('should fail gracefully', async () => { + try { + await httpRequest({ + url: 'http://not a good url', + }); + + fail('should not succeed'); + } catch (error) { expect(error).not.toBeUndefined(); expect(error).not.toBeNull(); - done(); - }); + } }); - it('should params object to query string', done => { - httpRequest({ + 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({ + 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', () => { @@ -251,7 +225,7 @@ 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); @@ -263,7 +237,7 @@ describe('httpRequest', () => { 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"}'); @@ -272,7 +246,7 @@ describe('httpRequest', () => { 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, From 2936bdf00db2ea5aa6d3ea3a9e72b680df33d644 Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Sat, 2 Oct 2021 22:53:21 -0400 Subject: [PATCH 2/4] Leverage template strings + expectAsync/toBeRejected based on PR feedback --- spec/HTTPRequest.spec.js | 62 ++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/spec/HTTPRequest.spec.js b/spec/HTTPRequest.spec.js index 5e1b197298..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(); @@ -53,7 +53,7 @@ describe('httpRequest', () => { it('should do /hello', async () => { const httpResponse = await httpRequest({ - url: httpRequestServer + '/hello', + url: `${httpRequestServer}/hello`, }); expect(httpResponse.status).toBe(200); @@ -64,7 +64,7 @@ describe('httpRequest', () => { it('should do not follow redirects by default', async () => { const httpResponse = await httpRequest({ - url: httpRequestServer + '/301', + url: `${httpRequestServer}/301`, }); expect(httpResponse.status).toBe(301); @@ -72,7 +72,7 @@ describe('httpRequest', () => { it('should follow redirects when set', async () => { const httpResponse = await httpRequest({ - url: httpRequestServer + '/301', + url: `${httpRequestServer}/301`, followRedirects: true, }); @@ -83,24 +83,24 @@ describe('httpRequest', () => { }); it('should fail on 404', async () => { - try { - await httpRequest({ - url: httpRequestServer + '/404', - }); - - fail('should not succeed'); - } catch (httpResponse) { - expect(httpResponse.status).toBe(404); - expect(httpResponse.buffer).toEqual(Buffer.from('NO')); - expect(httpResponse.text).toEqual('NO'); - expect(httpResponse.data).toBe(undefined); - } + await expectAsync( + httpRequest({ + url: `${httpRequestServer}/404`, + }) + ).toBeRejectedWith( + jasmine.objectContaining({ + status: 404, + buffer: Buffer.from('NO'), + text: 'NO', + data: undefined, + }) + ); }); it('should post on echo', async () => { const httpResponse = await httpRequest({ method: 'POST', - url: httpRequestServer + '/echo', + url: `${httpRequestServer}/echo`, body: { foo: 'bar', }, @@ -154,21 +154,16 @@ describe('httpRequest', () => { }); it('should fail gracefully', async () => { - try { - await httpRequest({ + await expectAsync( + httpRequest({ url: 'http://not a good url', - }); - - fail('should not succeed'); - } catch (error) { - expect(error).not.toBeUndefined(); - expect(error).not.toBeNull(); - } + }) + ).toBeRejected(); }); it('should params object to query string', async () => { const httpResponse = await httpRequest({ - url: httpRequestServer + '/qs', + url: `${httpRequestServer}/qs`, params: { foo: 'bar', }, @@ -180,7 +175,7 @@ describe('httpRequest', () => { it('should params string to query string', async () => { const httpResponse = await httpRequest({ - url: httpRequestServer + '/qs', + url: `${httpRequestServer}/qs`, params: 'foo=bar&foo2=bar2', }); @@ -204,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); @@ -231,6 +227,7 @@ describe('httpRequest', () => { const serialized = JSON.stringify(httpResponse); const result = JSON.parse(serialized); + expect(result.text).toBe('hello'); expect(result.data).toBe(undefined); }); @@ -240,6 +237,7 @@ describe('httpRequest', () => { 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' }); }); @@ -251,17 +249,19 @@ describe('httpRequest', () => { 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); From 905baa2adb27cf517d8b5b54ff52482be08e683e Mon Sep 17 00:00:00 2001 From: Brandon Scott Date: Sun, 3 Oct 2021 10:49:28 -0400 Subject: [PATCH 3/4] Add changelog entry --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 635777d7f1..9348fc3b23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -156,6 +156,8 @@ ___ - 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) + ## 4.10.4 [Full Changelog](https://github.com/parse-community/parse-server/compare/4.10.3...4.10.4) From ba97e90bed1909ab9d636e7cc2cfdc0966a7bc93 Mon Sep 17 00:00:00 2001 From: Manuel <5673677+mtrezza@users.noreply.github.com> Date: Fri, 8 Oct 2021 17:32:56 +0200 Subject: [PATCH 4/4] fix changelog entry --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a777ae2bf..9ba4f42830 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -157,7 +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) +- 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