Skip to content

Commit

Permalink
Merge branch 'master' into afterFind-Pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrezza authored Oct 8, 2021
2 parents 453eb79 + 197fcbd commit 8e4a46a
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 133 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,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)
- Allow liveQuery on Session class (Daniel Blyth) [#7554](https://github.com/parse-community/parse-server/pull/7554)

## 4.10.4
[Full Changelog](https://github.com/parse-community/parse-server/compare/4.10.3...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
54 changes: 53 additions & 1 deletion spec/ParseLiveQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,58 @@ describe('ParseLiveQuery', function () {
}
});

it('liveQuery on Session class', async done => {
await reconfigureServer({
liveQuery: { classNames: [Parse.Session] },
startLiveQueryServer: true,
verbose: false,
silent: true,
});

const user = new Parse.User();
user.setUsername('username');
user.setPassword('password');
await user.signUp();

const query = new Parse.Query(Parse.Session);
const subscription = await query.subscribe();

subscription.on('create', async obj => {
await new Promise(resolve => setTimeout(resolve, 200));
expect(obj.get('user').id).toBe(user.id);
expect(obj.get('createdWith')).toEqual({ action: 'login', authProvider: 'password' });
expect(obj.get('expiresAt')).toBeInstanceOf(Date);
expect(obj.get('installationId')).toBeDefined();
expect(obj.get('createdAt')).toBeInstanceOf(Date);
expect(obj.get('updatedAt')).toBeInstanceOf(Date);
done();
});

await Parse.User.logIn('username', 'password');
});

it('prevent liveQuery on Session class when not logged in', async done => {
await reconfigureServer({
liveQuery: {
classNames: [Parse.Session],
},
startLiveQueryServer: true,
verbose: false,
silent: true,
});

Parse.LiveQuery.on('error', error => {
expect(error).toBe('Invalid session token');
});
const query = new Parse.Query(Parse.Session);
const subscription = await query.subscribe();
subscription.on('error', error => {
Parse.LiveQuery.removeAllListeners('error');
expect(error).toBe('Invalid session token');
done();
});
});

it('handle invalid websocket payload length', async done => {
await reconfigureServer({
liveQuery: {
Expand Down Expand Up @@ -792,7 +844,7 @@ describe('ParseLiveQuery', function () {

await reconfigureServer({
liveQuery: {
classNames: ['_User'],
classNames: [Parse.User],
},
startLiveQueryServer: true,
verbose: false,
Expand Down
Loading

0 comments on commit 8e4a46a

Please sign in to comment.