2016-10-05 13:06:35 +00:00
|
|
|
const assert = require('assert')
|
2016-10-06 12:32:20 +00:00
|
|
|
const {remote} = require('electron')
|
2016-10-07 09:07:49 +00:00
|
|
|
const http = require('http')
|
|
|
|
const url = require('url')
|
2016-10-06 12:32:20 +00:00
|
|
|
const {net} = remote
|
2016-10-05 13:06:35 +00:00
|
|
|
|
2016-10-06 12:32:20 +00:00
|
|
|
describe.only('net module', function() {
|
2016-10-07 09:07:49 +00:00
|
|
|
this.timeout(0)
|
2016-10-05 13:06:35 +00:00
|
|
|
describe('HTTP basics', function() {
|
2016-10-07 09:07:49 +00:00
|
|
|
|
|
|
|
let server
|
|
|
|
beforeEach(function (done) {
|
|
|
|
server = http.createServer()
|
|
|
|
server.listen(0, '127.0.0.1', function () {
|
|
|
|
server.url = 'http://127.0.0.1:' + server.address().port
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
server.close(function() {
|
|
|
|
})
|
|
|
|
server = null
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to issue a basic GET request', function(done) {
|
|
|
|
const request_url = '/request_url'
|
|
|
|
server.on('request', function(request, response) {
|
|
|
|
switch (request.url) {
|
|
|
|
case request_url:
|
|
|
|
assert.equal(request.method, 'GET')
|
|
|
|
response.end();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
response.statusCode = 501
|
|
|
|
response.statusMessage = 'Not Implemented'
|
|
|
|
response.end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request(`${server.url}${request_url}`)
|
|
|
|
urlRequest.on('response', function(response) {
|
|
|
|
assert.equal(response.statusCode, 200)
|
|
|
|
response.on('end', function() {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.on('data', function(chunk) {
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should be able to issue a basic POST request', function(done) {
|
|
|
|
const request_url = '/request_url'
|
|
|
|
server.on('request', function(request, response) {
|
|
|
|
switch (request.url) {
|
|
|
|
case request_url:
|
|
|
|
assert.equal(request.method, 'POST')
|
|
|
|
response.end();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
response.statusCode = 501
|
|
|
|
response.statusMessage = 'Not Implemented'
|
|
|
|
response.end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: `${server.url}${request_url}`
|
|
|
|
})
|
|
|
|
urlRequest.on('response', function(response) {
|
|
|
|
assert.equal(response.statusCode, 200)
|
|
|
|
response.on('end', function() {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.on('data', function(chunk) {
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should fetch correct data in a GET request', function(done) {
|
|
|
|
const request_url = '/request_url'
|
|
|
|
const body_data = "Hello World!"
|
|
|
|
server.on('request', function(request, response) {
|
|
|
|
switch (request.url) {
|
|
|
|
case request_url:
|
|
|
|
assert.equal(request.method, 'GET')
|
|
|
|
response.write(body_data)
|
|
|
|
response.end();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
response.statusCode = 501
|
|
|
|
response.statusMessage = 'Not Implemented'
|
|
|
|
response.end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request(`${server.url}${request_url}`)
|
|
|
|
urlRequest.on('response', function(response) {
|
|
|
|
let expected_body_data = '';
|
|
|
|
assert.equal(response.statusCode, 200)
|
|
|
|
response.on('end', function() {
|
|
|
|
assert.equal(expected_body_data, body_data)
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.on('data', function(chunk) {
|
|
|
|
expected_body_data += chunk.toString();
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.end();
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should post the correct data in a POST request', function(done) {
|
|
|
|
const request_url = '/request_url'
|
|
|
|
const body_data = "Hello World!"
|
|
|
|
server.on('request', function(request, response) {
|
|
|
|
let posted_body_data = ''
|
|
|
|
switch (request.url) {
|
|
|
|
case request_url:
|
|
|
|
assert.equal(request.method, 'POST')
|
|
|
|
request.on('data', function(chunk) {
|
|
|
|
posted_body_data += chunk.toString()
|
|
|
|
})
|
|
|
|
request.on('end', function() {
|
|
|
|
assert.equal(posted_body_data, body_data)
|
|
|
|
response.end();
|
|
|
|
})
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
response.statusCode = 501
|
|
|
|
response.statusMessage = 'Not Implemented'
|
|
|
|
response.end()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const urlRequest = net.request({
|
|
|
|
method: 'POST',
|
|
|
|
url: `${server.url}${request_url}`
|
|
|
|
})
|
|
|
|
urlRequest.on('response', function(response) {
|
|
|
|
assert.equal(response.statusCode, 200)
|
|
|
|
response.on('end', function() {
|
|
|
|
done()
|
|
|
|
})
|
|
|
|
response.on('data', function(chunk) {
|
|
|
|
})
|
|
|
|
})
|
|
|
|
urlRequest.write(body_data)
|
|
|
|
urlRequest.end();
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
describe('ClientRequest API', function() {
|
|
|
|
it ('should emit ClientRequest events in a GET request', function(done) {
|
2016-10-06 12:32:20 +00:00
|
|
|
this.timeout(30000);
|
2016-10-05 13:06:35 +00:00
|
|
|
let response_event_emitted = false;
|
|
|
|
let data_event_emitted = false;
|
|
|
|
let end_event_emitted = false;
|
|
|
|
let finish_event_emitted = false;
|
|
|
|
const urlRequest = net.request({
|
2016-10-07 09:07:49 +00:00
|
|
|
method: 'GET',
|
|
|
|
url: 'https://www.google.com'
|
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.on('response', function(response) {
|
|
|
|
response_event_emitted = true;
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
assert(typeof statusCode === 'number')
|
|
|
|
assert.equal(statusCode, 200)
|
|
|
|
const statusMessage = response.statusMessage
|
|
|
|
const rawHeaders = response.rawHeaders
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(typeof rawHeaders === 'object')
|
2016-10-05 13:06:35 +00:00
|
|
|
const httpVersion = response.httpVersion;
|
|
|
|
assert(typeof httpVersion === 'string')
|
|
|
|
assert(httpVersion.length > 0)
|
|
|
|
const httpVersionMajor = response.httpVersionMajor;
|
|
|
|
assert(typeof httpVersionMajor === 'number')
|
|
|
|
assert(httpVersionMajor >= 1)
|
|
|
|
const httpVersionMinor = response.httpVersionMinor;
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(typeof httpVersionMinor === 'number')
|
2016-10-05 13:06:35 +00:00
|
|
|
assert(httpVersionMinor >= 0)
|
|
|
|
let body = '';
|
|
|
|
response.on('data', function(buffer) {
|
|
|
|
data_event_emitted = true;
|
|
|
|
body += buffer.toString()
|
|
|
|
assert(typeof body === 'string')
|
|
|
|
assert(body.length > 0)
|
2016-10-07 09:07:49 +00:00
|
|
|
});
|
2016-10-05 13:06:35 +00:00
|
|
|
response.on('end', function() {
|
|
|
|
end_event_emitted = true;
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
|
|
|
});
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.on('finish', function() {
|
|
|
|
finish_event_emitted = true;
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.on('error', function(error) {
|
|
|
|
assert.ifError(error);
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.on('close', function() {
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(response_event_emitted)
|
2016-10-05 13:06:35 +00:00
|
|
|
assert(data_event_emitted)
|
|
|
|
assert(end_event_emitted)
|
|
|
|
assert(finish_event_emitted)
|
|
|
|
done()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.end();
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
|
2016-10-07 09:07:49 +00:00
|
|
|
it ('should emit ClientRequest events in a POST request', function(done) {
|
2016-10-06 12:32:20 +00:00
|
|
|
this.timeout(20000);
|
2016-10-05 13:06:35 +00:00
|
|
|
let response_event_emitted = false;
|
|
|
|
let data_event_emitted = false;
|
|
|
|
let end_event_emitted = false;
|
|
|
|
let finish_event_emitted = false;
|
2016-10-06 12:32:20 +00:00
|
|
|
const urlRequest = net.request({
|
2016-10-07 09:07:49 +00:00
|
|
|
method: 'POST',
|
|
|
|
url: 'http://httpbin.org/post'
|
|
|
|
});
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.on('response', function(response) {
|
|
|
|
response_event_emitted = true;
|
|
|
|
const statusCode = response.statusCode
|
|
|
|
assert(typeof statusCode === 'number')
|
|
|
|
assert.equal(statusCode, 200)
|
|
|
|
const statusMessage = response.statusMessage
|
|
|
|
const rawHeaders = response.rawHeaders
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(typeof rawHeaders === 'object')
|
2016-10-05 13:06:35 +00:00
|
|
|
const httpVersion = response.httpVersion;
|
|
|
|
assert(typeof httpVersion === 'string')
|
|
|
|
assert(httpVersion.length > 0)
|
|
|
|
const httpVersionMajor = response.httpVersionMajor;
|
|
|
|
assert(typeof httpVersionMajor === 'number')
|
|
|
|
assert(httpVersionMajor >= 1)
|
|
|
|
const httpVersionMinor = response.httpVersionMinor;
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(typeof httpVersionMinor === 'number')
|
2016-10-05 13:06:35 +00:00
|
|
|
assert(httpVersionMinor >= 0)
|
|
|
|
let body = '';
|
2016-10-06 12:32:20 +00:00
|
|
|
response.on('end', function() {
|
|
|
|
end_event_emitted = true;
|
|
|
|
assert(response_event_emitted)
|
|
|
|
assert(data_event_emitted)
|
|
|
|
assert(end_event_emitted)
|
|
|
|
assert(finish_event_emitted)
|
|
|
|
done()
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
response.on('data', function(buffer) {
|
|
|
|
data_event_emitted = true;
|
|
|
|
body += buffer.toString()
|
|
|
|
assert(typeof body === 'string')
|
|
|
|
assert(body.length > 0)
|
2016-10-07 09:07:49 +00:00
|
|
|
});
|
2016-10-06 12:32:20 +00:00
|
|
|
|
2016-10-07 09:07:49 +00:00
|
|
|
});
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.on('finish', function() {
|
|
|
|
finish_event_emitted = true;
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.on('error', function(error) {
|
|
|
|
assert.ifError(error);
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.on('close', function() {
|
2016-10-06 12:32:20 +00:00
|
|
|
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
for (let i = 0; i < 100; ++i) {
|
|
|
|
urlRequest.write('Hello World!');
|
2016-10-07 09:07:49 +00:00
|
|
|
}
|
2016-10-05 13:06:35 +00:00
|
|
|
urlRequest.end();
|
|
|
|
})
|
2016-10-07 09:07:49 +00:00
|
|
|
|
2016-10-05 13:06:35 +00:00
|
|
|
it ('should be able to set a custom HTTP header', function() {
|
|
|
|
assert(false)
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
it ('should be able to abort an HTTP request', function() {
|
|
|
|
assert(false)
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
it ('should be able to pipe into a request', function() {
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(false)
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
it ('should be able to create a request with options', function() {
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(false)
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
it ('should be able to specify a custom session', function() {
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(false)
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
2016-10-05 13:06:35 +00:00
|
|
|
it ('should support chunked encoding', function() {
|
2016-10-06 12:32:20 +00:00
|
|
|
assert(false)
|
2016-10-07 09:07:49 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
describe('IncomingMessage API', function() {
|
|
|
|
it('should provide a Node.js-similar API', function() {
|
|
|
|
assert(false)
|
|
|
|
})
|
2016-10-06 12:32:20 +00:00
|
|
|
it ('should not emit any event after close', function() {
|
|
|
|
assert(false)
|
2016-10-05 13:06:35 +00:00
|
|
|
})
|
2016-10-07 09:07:49 +00:00
|
|
|
it ('should be able to pipe from a response', function() {
|
|
|
|
assert(false)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|