remove runtime dependency on request (#5076)

This commit is contained in:
Florent Vilmart
2018-09-23 12:31:08 -04:00
committed by GitHub
parent 4dc4a3a56d
commit 93a0017b25
13 changed files with 2706 additions and 2779 deletions

View File

@@ -50,91 +50,48 @@ describe('httpRequest', () => {
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();
},
function() {
fail('should not fail');
done();
}
);
});
it('should do /hello with callback and promises', done => {
let calls = 0;
httpRequest({
url: httpRequestServer + '/hello',
success: function() {
calls++;
},
error: function() {
calls++;
},
}).then(
function(httpResponse) {
expect(calls).toBe(1);
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();
},
function() {
fail('should not fail');
done();
}
);
}).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 not follow redirects by default', done => {
httpRequest({
url: httpRequestServer + '/301',
}).then(
function(httpResponse) {
expect(httpResponse.status).toBe(301);
done();
},
function() {
fail('should not fail');
done();
}
);
}).then(function(httpResponse) {
expect(httpResponse.status).toBe(301);
done();
}, done.fail);
});
it('should follow redirects when set', done => {
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();
},
function() {
fail('should not fail');
done();
}
);
}).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 fail on 404', done => {
let calls = 0;
httpRequest({
url: httpRequestServer + '/404',
success: function() {
}).then(
function() {
calls++;
fail('should not succeed');
done();
},
error: function(httpResponse) {
function(httpResponse) {
calls++;
expect(calls).toBe(1);
expect(httpResponse.status).toBe(404);
@@ -142,12 +99,11 @@ describe('httpRequest', () => {
expect(httpResponse.text).toEqual('NO');
expect(httpResponse.data).toBe(undefined);
done();
},
});
}
);
});
it('should post on echo', done => {
let calls = 0;
httpRequest({
method: 'POST',
url: httpRequestServer + '/echo',
@@ -157,15 +113,8 @@ describe('httpRequest', () => {
headers: {
'Content-Type': 'application/json',
},
success: function() {
calls++;
},
error: function() {
calls++;
},
}).then(
function(httpResponse) {
expect(calls).toBe(1);
expect(httpResponse.status).toBe(200);
expect(httpResponse.data).toEqual({ foo: 'bar' });
done();
@@ -220,15 +169,10 @@ describe('httpRequest', () => {
it('should fail gracefully', done => {
httpRequest({
url: 'http://not a good url',
success: function() {
fail('should not succeed');
done();
},
error: function(error) {
expect(error).not.toBeUndefined();
expect(error).not.toBeNull();
done();
},
}).then(done.fail, function(error) {
expect(error).not.toBeUndefined();
expect(error).not.toBeNull();
done();
});
});

View File

@@ -1,4 +1,4 @@
const request = require('request');
const request = require('../lib/request');
function createProduct() {
const file = new Parse.File(
@@ -26,165 +26,136 @@ describe('test validate_receipt endpoint', () => {
beforeEach(done => {
createProduct()
.then(done)
.catch(function() {
.catch(function(err) {
console.error({ err });
done();
});
});
it('should bypass appstore validation', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
productIdentifier: 'a-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
it('should bypass appstore validation', async () => {
const httpResponse = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
done();
} else {
expect(body.__type).toEqual('File');
const url = body.url;
request.get(
{
url: url,
},
function(err, res, body) {
expect(body).toEqual('download_file');
done();
}
);
}
}
);
method: 'POST',
url: 'http://localhost:8378/1/validate_purchase',
body: {
productIdentifier: 'a-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
});
const body = httpResponse.data;
if (typeof body != 'object') {
fail('Body is not an object');
} else {
console.log(body);
expect(body.__type).toEqual('File');
const url = body.url;
const otherResponse = await request({
url: url,
});
expect(otherResponse.text).toBe('download_file');
}
});
it('should fail for missing receipt', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
productIdentifier: 'a-product',
bypassAppStoreValidation: true,
},
it('should fail for missing receipt', async () => {
const response = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
done();
} else {
expect(body.code).toEqual(Parse.Error.INVALID_JSON);
done();
}
}
);
url: 'http://localhost:8378/1/validate_purchase',
method: 'POST',
body: {
productIdentifier: 'a-product',
bypassAppStoreValidation: true,
},
}).then(fail, res => res);
const body = response.data;
expect(body.code).toEqual(Parse.Error.INVALID_JSON);
});
it('should fail for missing product identifier', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
it('should fail for missing product identifier', async () => {
const response = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
done();
} else {
expect(body.code).toEqual(Parse.Error.INVALID_JSON);
done();
}
}
);
url: 'http://localhost:8378/1/validate_purchase',
method: 'POST',
body: {
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
}).then(fail, res => res);
const body = response.data;
expect(body.code).toEqual(Parse.Error.INVALID_JSON);
});
it('should bypass appstore validation and not find product', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
productIdentifier: 'another-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
bypassAppStoreValidation: true,
},
it('should bypass appstore validation and not find product', async () => {
const response = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
done();
} else {
expect(body.code).toEqual(Parse.Error.OBJECT_NOT_FOUND);
expect(body.error).toEqual('Object not found.');
done();
}
}
);
url: 'http://localhost:8378/1/validate_purchase',
method: 'POST',
body: {
productIdentifier: 'another-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf8').toString('base64'),
},
bypassAppStoreValidation: true,
},
}).catch(error => error);
const body = response.data;
if (typeof body != 'object') {
fail('Body is not an object');
} else {
expect(body.code).toEqual(Parse.Error.OBJECT_NOT_FOUND);
expect(body.error).toEqual('Object not found.');
}
});
it('should fail at appstore validation', done => {
request.post(
{
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
},
url: 'http://localhost:8378/1/validate_purchase',
json: true,
body: {
productIdentifier: 'a-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
it('should fail at appstore validation', async () => {
const response = await request({
headers: {
'X-Parse-Application-Id': 'test',
'X-Parse-REST-API-Key': 'rest',
'Content-Type': 'application/json',
},
url: 'http://localhost:8378/1/validate_purchase',
method: 'POST',
body: {
productIdentifier: 'a-product',
receipt: {
__type: 'Bytes',
base64: new Buffer('receipt', 'utf-8').toString('base64'),
},
},
function(err, res, body) {
if (typeof body != 'object') {
fail('Body is not an object');
} else {
expect(body.status).toBe(21002);
expect(body.error).toBe(
'The data in the receipt-data property was malformed or missing.'
);
}
done();
}
);
});
const body = response.data;
if (typeof body != 'object') {
fail('Body is not an object');
} else {
expect(body.status).toBe(21002);
expect(body.error).toBe(
'The data in the receipt-data property was malformed or missing.'
);
}
});
it('should not create a _Product', done => {

View File

@@ -1,9 +1,10 @@
{
"apps": [
{
"arg1": "my_app",
"arg2": 8888,
"arg3": "hello",
"arg4": "/1"
}]
{
"arg1": "my_app",
"arg2": 8888,
"arg3": "hello",
"arg4": "/1"
}
]
}

View File

@@ -1,16 +1,16 @@
{
"apps": [
{
"arg1": "my_app",
"arg2": "99999",
"arg3": "hello",
"arg4": "/1"
},
{
"arg1": "my_app2",
"arg2": "9999",
"arg3": "hello",
"arg4": "/1"
}
{
"arg1": "my_app",
"arg2": "99999",
"arg3": "hello",
"arg4": "/1"
},
{
"arg1": "my_app2",
"arg2": "9999",
"arg3": "hello",
"arg4": "/1"
}
]
}

View File

@@ -1,10 +1,6 @@
{
"spec_dir": "spec",
"spec_files": [
"*spec.js"
],
"helpers": [
"helper.js"
],
"spec_files": ["*spec.js"],
"helpers": ["helper.js"],
"random": false
}