refactor: modernize HTTPRequest tests (#7604)
This commit is contained in:
@@ -157,6 +157,7 @@ ___
|
|||||||
- Allow setting descending sort to full text queries (dblythy) [#7496](https://github.com/parse-community/parse-server/pull/7496)
|
- 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)
|
- 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)
|
- 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)
|
- Allow liveQuery on Session class (Daniel Blyth) [#7554](https://github.com/parse-community/parse-server/pull/7554)
|
||||||
|
|
||||||
## 4.10.4
|
## 4.10.4
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const httpRequest = require('../lib/cloud-code/httpRequest'),
|
|||||||
express = require('express');
|
express = require('express');
|
||||||
|
|
||||||
const port = 13371;
|
const port = 13371;
|
||||||
const httpRequestServer = 'http://localhost:' + port;
|
const httpRequestServer = `http://localhost:${port}`;
|
||||||
|
|
||||||
function startServer(done) {
|
function startServer(done) {
|
||||||
const app = express();
|
const app = express();
|
||||||
@@ -51,167 +51,136 @@ describe('httpRequest', () => {
|
|||||||
server.close(done);
|
server.close(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should do /hello', done => {
|
it('should do /hello', async () => {
|
||||||
httpRequest({
|
const httpResponse = await httpRequest({
|
||||||
url: httpRequestServer + '/hello',
|
url: `${httpRequestServer}/hello`,
|
||||||
}).then(function (httpResponse) {
|
});
|
||||||
expect(httpResponse.status).toBe(200);
|
|
||||||
expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}'));
|
expect(httpResponse.status).toBe(200);
|
||||||
expect(httpResponse.text).toEqual('{"response":"OK"}');
|
expect(httpResponse.buffer).toEqual(Buffer.from('{"response":"OK"}'));
|
||||||
expect(httpResponse.data.response).toEqual('OK');
|
expect(httpResponse.text).toEqual('{"response":"OK"}');
|
||||||
done();
|
expect(httpResponse.data.response).toEqual('OK');
|
||||||
}, done.fail);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should do not follow redirects by default', done => {
|
it('should do not follow redirects by default', async () => {
|
||||||
httpRequest({
|
const httpResponse = await httpRequest({
|
||||||
url: httpRequestServer + '/301',
|
url: `${httpRequestServer}/301`,
|
||||||
}).then(function (httpResponse) {
|
});
|
||||||
expect(httpResponse.status).toBe(301);
|
|
||||||
done();
|
expect(httpResponse.status).toBe(301);
|
||||||
}, done.fail);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should follow redirects when set', done => {
|
it('should follow redirects when set', async () => {
|
||||||
httpRequest({
|
const httpResponse = await httpRequest({
|
||||||
url: httpRequestServer + '/301',
|
url: `${httpRequestServer}/301`,
|
||||||
followRedirects: true,
|
followRedirects: true,
|
||||||
}).then(function (httpResponse) {
|
});
|
||||||
expect(httpResponse.status).toBe(200);
|
|
||||||
expect(httpResponse.buffer).toEqual(new Buffer('{"response":"OK"}'));
|
expect(httpResponse.status).toBe(200);
|
||||||
expect(httpResponse.text).toEqual('{"response":"OK"}');
|
expect(httpResponse.buffer).toEqual(Buffer.from('{"response":"OK"}'));
|
||||||
expect(httpResponse.data.response).toEqual('OK');
|
expect(httpResponse.text).toEqual('{"response":"OK"}');
|
||||||
done();
|
expect(httpResponse.data.response).toEqual('OK');
|
||||||
}, done.fail);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail on 404', done => {
|
it('should fail on 404', async () => {
|
||||||
let calls = 0;
|
await expectAsync(
|
||||||
httpRequest({
|
httpRequest({
|
||||||
url: httpRequestServer + '/404',
|
url: `${httpRequestServer}/404`,
|
||||||
}).then(
|
})
|
||||||
function () {
|
).toBeRejectedWith(
|
||||||
calls++;
|
jasmine.objectContaining({
|
||||||
fail('should not succeed');
|
status: 404,
|
||||||
done();
|
buffer: Buffer.from('NO'),
|
||||||
},
|
text: 'NO',
|
||||||
function (httpResponse) {
|
data: undefined,
|
||||||
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 post on echo', done => {
|
it('should post on echo', async () => {
|
||||||
httpRequest({
|
const httpResponse = await httpRequest({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: httpRequestServer + '/echo',
|
url: `${httpRequestServer}/echo`,
|
||||||
body: {
|
body: {
|
||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
},
|
},
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
}).then(
|
});
|
||||||
function (httpResponse) {
|
|
||||||
expect(httpResponse.status).toBe(200);
|
expect(httpResponse.status).toBe(200);
|
||||||
expect(httpResponse.data).toEqual({ foo: 'bar' });
|
expect(httpResponse.data).toEqual({ foo: 'bar' });
|
||||||
done();
|
|
||||||
},
|
|
||||||
function () {
|
|
||||||
fail('should not fail');
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should encode a query string body by default', done => {
|
it('should encode a query string body by default', () => {
|
||||||
const options = {
|
const options = {
|
||||||
body: { foo: 'bar' },
|
body: { foo: 'bar' },
|
||||||
};
|
};
|
||||||
const result = httpRequest.encodeBody(options);
|
const result = httpRequest.encodeBody(options);
|
||||||
|
|
||||||
expect(result.body).toEqual('foo=bar');
|
expect(result.body).toEqual('foo=bar');
|
||||||
expect(result.headers['Content-Type']).toEqual('application/x-www-form-urlencoded');
|
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 = {
|
const options = {
|
||||||
body: { foo: 'bar' },
|
body: { foo: 'bar' },
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
};
|
};
|
||||||
const result = httpRequest.encodeBody(options);
|
const result = httpRequest.encodeBody(options);
|
||||||
|
|
||||||
expect(result.body).toEqual('{"foo":"bar"}');
|
expect(result.body).toEqual('{"foo":"bar"}');
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
it('should encode a www-form body', done => {
|
|
||||||
|
it('should encode a www-form body', () => {
|
||||||
const options = {
|
const options = {
|
||||||
body: { foo: 'bar', bar: 'baz' },
|
body: { foo: 'bar', bar: 'baz' },
|
||||||
headers: { 'cOntent-tYpe': 'application/x-www-form-urlencoded' },
|
headers: { 'cOntent-tYpe': 'application/x-www-form-urlencoded' },
|
||||||
};
|
};
|
||||||
const result = httpRequest.encodeBody(options);
|
const result = httpRequest.encodeBody(options);
|
||||||
|
|
||||||
expect(result.body).toEqual('foo=bar&bar=baz');
|
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 = {
|
const options = {
|
||||||
body: { foo: 'bar', bar: 'baz' },
|
body: { foo: 'bar', bar: 'baz' },
|
||||||
headers: { 'cOntent-tYpe': 'mime/jpeg' },
|
headers: { 'cOntent-tYpe': 'mime/jpeg' },
|
||||||
};
|
};
|
||||||
const result = httpRequest.encodeBody(options);
|
const result = httpRequest.encodeBody(options);
|
||||||
|
|
||||||
expect(result.body).toEqual({ foo: 'bar', bar: 'baz' });
|
expect(result.body).toEqual({ foo: 'bar', bar: 'baz' });
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail gracefully', done => {
|
it('should fail gracefully', async () => {
|
||||||
httpRequest({
|
await expectAsync(
|
||||||
url: 'http://not a good url',
|
httpRequest({
|
||||||
}).then(done.fail, function (error) {
|
url: 'http://not a good url',
|
||||||
expect(error).not.toBeUndefined();
|
})
|
||||||
expect(error).not.toBeNull();
|
).toBeRejected();
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should params object to query string', done => {
|
it('should params object to query string', async () => {
|
||||||
httpRequest({
|
const httpResponse = await httpRequest({
|
||||||
url: httpRequestServer + '/qs',
|
url: `${httpRequestServer}/qs`,
|
||||||
params: {
|
params: {
|
||||||
foo: 'bar',
|
foo: 'bar',
|
||||||
},
|
},
|
||||||
}).then(
|
});
|
||||||
function (httpResponse) {
|
|
||||||
expect(httpResponse.status).toBe(200);
|
expect(httpResponse.status).toBe(200);
|
||||||
expect(httpResponse.data).toEqual({ foo: 'bar' });
|
expect(httpResponse.data).toEqual({ foo: 'bar' });
|
||||||
done();
|
|
||||||
},
|
|
||||||
function () {
|
|
||||||
fail('should not fail');
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should params string to query string', done => {
|
it('should params string to query string', async () => {
|
||||||
httpRequest({
|
const httpResponse = await httpRequest({
|
||||||
url: httpRequestServer + '/qs',
|
url: `${httpRequestServer}/qs`,
|
||||||
params: 'foo=bar&foo2=bar2',
|
params: 'foo=bar&foo2=bar2',
|
||||||
}).then(
|
});
|
||||||
function (httpResponse) {
|
|
||||||
expect(httpResponse.status).toBe(200);
|
expect(httpResponse.status).toBe(200);
|
||||||
expect(httpResponse.data).toEqual({ foo: 'bar', foo2: 'bar2' });
|
expect(httpResponse.data).toEqual({ foo: 'bar', foo2: 'bar2' });
|
||||||
done();
|
|
||||||
},
|
|
||||||
function () {
|
|
||||||
fail('should not fail');
|
|
||||||
done();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not crash with undefined body', () => {
|
it('should not crash with undefined body', () => {
|
||||||
@@ -230,6 +199,7 @@ describe('httpRequest', () => {
|
|||||||
|
|
||||||
const serialized = JSON.stringify(httpResponse);
|
const serialized = JSON.stringify(httpResponse);
|
||||||
const result = JSON.parse(serialized);
|
const result = JSON.parse(serialized);
|
||||||
|
|
||||||
expect(result.text).toBe('hello');
|
expect(result.text).toBe('hello');
|
||||||
expect(result.data).toBe(undefined);
|
expect(result.data).toBe(undefined);
|
||||||
expect(result.body).toBe(undefined);
|
expect(result.body).toBe(undefined);
|
||||||
@@ -251,43 +221,47 @@ describe('httpRequest', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('serialized httpResponse correctly with body buffer string', () => {
|
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.text).toBe('hello');
|
||||||
expect(httpResponse.data).toBe(undefined);
|
expect(httpResponse.data).toBe(undefined);
|
||||||
|
|
||||||
const serialized = JSON.stringify(httpResponse);
|
const serialized = JSON.stringify(httpResponse);
|
||||||
const result = JSON.parse(serialized);
|
const result = JSON.parse(serialized);
|
||||||
|
|
||||||
expect(result.text).toBe('hello');
|
expect(result.text).toBe('hello');
|
||||||
expect(result.data).toBe(undefined);
|
expect(result.data).toBe(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('serialized httpResponse correctly with body buffer JSON Object', () => {
|
it('serialized httpResponse correctly with body buffer JSON Object', () => {
|
||||||
const json = '{"foo":"bar"}';
|
const json = '{"foo":"bar"}';
|
||||||
const httpResponse = new HTTPResponse({}, new Buffer(json));
|
const httpResponse = new HTTPResponse({}, Buffer.from(json));
|
||||||
const serialized = JSON.stringify(httpResponse);
|
const serialized = JSON.stringify(httpResponse);
|
||||||
const result = JSON.parse(serialized);
|
const result = JSON.parse(serialized);
|
||||||
|
|
||||||
expect(result.text).toEqual('{"foo":"bar"}');
|
expect(result.text).toEqual('{"foo":"bar"}');
|
||||||
expect(result.data).toEqual({ foo: 'bar' });
|
expect(result.data).toEqual({ foo: 'bar' });
|
||||||
});
|
});
|
||||||
|
|
||||||
it('serialized httpResponse with Parse._encode should be allright', () => {
|
it('serialized httpResponse with Parse._encode should be allright', () => {
|
||||||
const json = '{"foo":"bar"}';
|
const json = '{"foo":"bar"}';
|
||||||
const httpResponse = new HTTPResponse({}, new Buffer(json));
|
const httpResponse = new HTTPResponse({}, Buffer.from(json));
|
||||||
const encoded = Parse._encode(httpResponse);
|
const encoded = Parse._encode(httpResponse);
|
||||||
let foundData,
|
let foundData,
|
||||||
foundText,
|
foundText,
|
||||||
foundBody = false;
|
foundBody = false;
|
||||||
|
|
||||||
for (const key in encoded) {
|
for (const key in encoded) {
|
||||||
if (key == 'data') {
|
if (key === 'data') {
|
||||||
foundData = true;
|
foundData = true;
|
||||||
}
|
}
|
||||||
if (key == 'text') {
|
if (key === 'text') {
|
||||||
foundText = true;
|
foundText = true;
|
||||||
}
|
}
|
||||||
if (key == 'body') {
|
if (key === 'body') {
|
||||||
foundBody = true;
|
foundBody = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
expect(foundData).toBe(true);
|
expect(foundData).toBe(true);
|
||||||
expect(foundText).toBe(true);
|
expect(foundText).toBe(true);
|
||||||
expect(foundBody).toBe(false);
|
expect(foundBody).toBe(false);
|
||||||
|
|||||||
Reference in New Issue
Block a user