Skip to content

Commit

Permalink
refactor: modernize HTTPRequest tests (#7604)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandongregoryscott authored Oct 8, 2021
1 parent caee281 commit 197fcbd
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 111 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
196 changes: 85 additions & 111 deletions spec/HTTPRequest.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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', () => {
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 197fcbd

Please sign in to comment.