@@ -1,123 +1,137 @@
|
||||
'use strict';
|
||||
|
||||
const httpRequest = require("../lib/cloud-code/httpRequest"),
|
||||
const httpRequest = require('../lib/cloud-code/httpRequest'),
|
||||
HTTPResponse = require('../lib/cloud-code/HTTPResponse').default,
|
||||
bodyParser = require('body-parser'),
|
||||
express = require("express");
|
||||
express = require('express');
|
||||
|
||||
const port = 13371;
|
||||
const httpRequestServer = "http://localhost:" + port;
|
||||
const httpRequestServer = 'http://localhost:' + port;
|
||||
|
||||
function startServer(done) {
|
||||
const app = express();
|
||||
app.use(bodyParser.json({ 'type': '*/*' }));
|
||||
app.get("/hello", function(req, res){
|
||||
res.json({response: "OK"});
|
||||
app.use(bodyParser.json({ type: '*/*' }));
|
||||
app.get('/hello', function(req, res) {
|
||||
res.json({ response: 'OK' });
|
||||
});
|
||||
|
||||
app.get("/404", function(req, res){
|
||||
app.get('/404', function(req, res) {
|
||||
res.status(404);
|
||||
res.send("NO");
|
||||
res.send('NO');
|
||||
});
|
||||
|
||||
app.get("/301", function(req, res){
|
||||
app.get('/301', function(req, res) {
|
||||
res.status(301);
|
||||
res.location("/hello");
|
||||
res.location('/hello');
|
||||
res.send();
|
||||
});
|
||||
|
||||
app.post('/echo', function(req, res){
|
||||
app.post('/echo', function(req, res) {
|
||||
res.json(req.body);
|
||||
});
|
||||
|
||||
app.get('/qs', function(req, res){
|
||||
app.get('/qs', function(req, res) {
|
||||
res.json(req.query);
|
||||
});
|
||||
|
||||
return app.listen(13371, undefined, done);
|
||||
}
|
||||
|
||||
describe("httpRequest", () => {
|
||||
describe('httpRequest', () => {
|
||||
let server;
|
||||
beforeAll((done) => {
|
||||
beforeAll(done => {
|
||||
server = startServer(done);
|
||||
});
|
||||
|
||||
afterAll((done) => {
|
||||
afterAll(done => {
|
||||
server.close(done);
|
||||
});
|
||||
|
||||
it("should do /hello", (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();
|
||||
}, function(){
|
||||
fail("should not fail");
|
||||
done();
|
||||
})
|
||||
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) => {
|
||||
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();
|
||||
})
|
||||
});
|
||||
|
||||
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();
|
||||
})
|
||||
});
|
||||
|
||||
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();
|
||||
})
|
||||
});
|
||||
|
||||
it("should fail on 404", (done) => {
|
||||
let calls = 0;
|
||||
httpRequest({
|
||||
url: httpRequestServer + "/404",
|
||||
url: httpRequestServer + '/hello',
|
||||
success: function() {
|
||||
calls++;
|
||||
fail("should not succeed");
|
||||
},
|
||||
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();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
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();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
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();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('should fail on 404', done => {
|
||||
let calls = 0;
|
||||
httpRequest({
|
||||
url: httpRequestServer + '/404',
|
||||
success: function() {
|
||||
calls++;
|
||||
fail('should not succeed');
|
||||
done();
|
||||
},
|
||||
error: function(httpResponse) {
|
||||
@@ -128,117 +142,130 @@ 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",
|
||||
body: {
|
||||
foo: "bar"
|
||||
},
|
||||
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();
|
||||
}, function(){
|
||||
fail("should not fail");
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
it("should encode a query string body by default", (done) => {
|
||||
it('should post on echo', done => {
|
||||
let calls = 0;
|
||||
httpRequest({
|
||||
method: 'POST',
|
||||
url: httpRequestServer + '/echo',
|
||||
body: {
|
||||
foo: 'bar',
|
||||
},
|
||||
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();
|
||||
},
|
||||
function() {
|
||||
fail('should not fail');
|
||||
done();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('should encode a query string body by default', done => {
|
||||
const options = {
|
||||
body: {"foo": "bar"},
|
||||
}
|
||||
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');
|
||||
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', done => {
|
||||
const options = {
|
||||
body: {"foo": "bar"},
|
||||
headers: {'Content-Type': 'application/json'}
|
||||
}
|
||||
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', done => {
|
||||
const options = {
|
||||
body: {"foo": "bar", "bar": "baz"},
|
||||
headers: {'cOntent-tYpe': 'application/x-www-form-urlencoded'}
|
||||
}
|
||||
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");
|
||||
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', done => {
|
||||
const options = {
|
||||
body:{"foo": "bar", "bar": "baz"},
|
||||
headers: {'cOntent-tYpe': 'mime/jpeg'}
|
||||
}
|
||||
body: { foo: 'bar', bar: 'baz' },
|
||||
headers: { 'cOntent-tYpe': 'mime/jpeg' },
|
||||
};
|
||||
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', done => {
|
||||
httpRequest({
|
||||
url: "http://not a good url",
|
||||
url: 'http://not a good url',
|
||||
success: function() {
|
||||
fail("should not succeed");
|
||||
fail('should not succeed');
|
||||
done();
|
||||
},
|
||||
error: function(error) {
|
||||
expect(error).not.toBeUndefined();
|
||||
expect(error).not.toBeNull();
|
||||
done();
|
||||
}
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should params object to query string", (done) => {
|
||||
it('should params object to query string', done => {
|
||||
httpRequest({
|
||||
url: httpRequestServer + "/qs",
|
||||
url: httpRequestServer + '/qs',
|
||||
params: {
|
||||
foo: "bar"
|
||||
foo: 'bar',
|
||||
},
|
||||
}).then(
|
||||
function(httpResponse) {
|
||||
expect(httpResponse.status).toBe(200);
|
||||
expect(httpResponse.data).toEqual({ foo: 'bar' });
|
||||
done();
|
||||
},
|
||||
function() {
|
||||
fail('should not fail');
|
||||
done();
|
||||
}
|
||||
}).then(function(httpResponse){
|
||||
expect(httpResponse.status).toBe(200);
|
||||
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', done => {
|
||||
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();
|
||||
})
|
||||
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();
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('should not crash with undefined body', () => {
|
||||
@@ -263,17 +290,17 @@ describe("httpRequest", () => {
|
||||
});
|
||||
|
||||
it('serialized httpResponse correctly with body object', () => {
|
||||
const httpResponse = new HTTPResponse({}, {foo: "bar"});
|
||||
const httpResponse = new HTTPResponse({}, { foo: 'bar' });
|
||||
Parse._encode(httpResponse);
|
||||
const serialized = JSON.stringify(httpResponse);
|
||||
const result = JSON.parse(serialized);
|
||||
|
||||
expect(httpResponse.text).toEqual('{"foo":"bar"}');
|
||||
expect(httpResponse.data).toEqual({foo: 'bar'});
|
||||
expect(httpResponse.body).toEqual({foo: 'bar'});
|
||||
expect(httpResponse.data).toEqual({ foo: 'bar' });
|
||||
expect(httpResponse.body).toEqual({ foo: 'bar' });
|
||||
|
||||
expect(result.text).toEqual('{"foo":"bar"}');
|
||||
expect(result.data).toEqual({foo: 'bar'});
|
||||
expect(result.data).toEqual({ foo: 'bar' });
|
||||
expect(result.body).toEqual(undefined);
|
||||
});
|
||||
|
||||
@@ -294,15 +321,17 @@ describe("httpRequest", () => {
|
||||
const serialized = JSON.stringify(httpResponse);
|
||||
const result = JSON.parse(serialized);
|
||||
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', () => {
|
||||
const json = '{"foo":"bar"}';
|
||||
const httpResponse = new HTTPResponse({}, new Buffer(json));
|
||||
const encoded = Parse._encode(httpResponse);
|
||||
let foundData, foundText, foundBody = false;
|
||||
for(const key in encoded) {
|
||||
let foundData,
|
||||
foundText,
|
||||
foundBody = false;
|
||||
for (const key in encoded) {
|
||||
if (key == 'data') {
|
||||
foundData = true;
|
||||
}
|
||||
@@ -317,5 +346,4 @@ describe("httpRequest", () => {
|
||||
expect(foundText).toBe(true);
|
||||
expect(foundBody).toBe(false);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user