Adding basic http tests, fixing issues in ClientRequest constructor.
This commit is contained in:
parent
046f48db51
commit
9b94dfcbdc
2 changed files with 202 additions and 42 deletions
|
@ -59,6 +59,14 @@ class IncomingMessage extends Readable {
|
|||
return this._url_request.rawResponseHeaders
|
||||
}
|
||||
|
||||
get rawTrailers() {
|
||||
throw (new Error('HTTP trailers are not supported.'))
|
||||
}
|
||||
|
||||
get trailers() {
|
||||
throw (new Error('HTTP trailers are not supported.'))
|
||||
}
|
||||
|
||||
_storeInternalData(chunk) {
|
||||
this._data.push(chunk)
|
||||
}
|
||||
|
@ -113,7 +121,7 @@ class ClientRequest extends EventEmitter {
|
|||
|
||||
if (!urlStr) {
|
||||
let urlObj = {}
|
||||
const protocol = options.protocol || 'http'
|
||||
const protocol = options.protocol || 'http:'
|
||||
if (!kSupportedProtocols.has(protocol)) {
|
||||
throw new Error('Protocol "' + protocol + '" not supported. ')
|
||||
}
|
||||
|
@ -133,7 +141,6 @@ class ClientRequest extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
const path = options.path || '/'
|
||||
if (options.path && / /.test(options.path)) {
|
||||
// The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
|
||||
// with an additional rule for ignoring percentage-escaped characters
|
||||
|
@ -143,7 +150,9 @@ class ClientRequest extends EventEmitter {
|
|||
// an invalid request.
|
||||
throw new TypeError('Request path contains unescaped characters.')
|
||||
}
|
||||
urlObj.path = path
|
||||
urlObj.pathname = options.pathname || '/'
|
||||
urlObj.search = options.search
|
||||
urlObj.hash = options.hash
|
||||
urlStr = url.format(urlObj)
|
||||
}
|
||||
|
||||
|
@ -252,10 +261,6 @@ class ClientRequest extends EventEmitter {
|
|||
chunk = Buffer.from(chunk, encoding)
|
||||
}
|
||||
|
||||
// Headers are assumed to be sent on first call to _writeBuffer,
|
||||
// i.e. after the first call to write or end.
|
||||
let result = this._url_request.write(chunk, isLast)
|
||||
|
||||
// Since writing to the network is asynchronous, we conservatively
|
||||
// assume that request headers are written after delivering the first
|
||||
// buffer to the network IO thread.
|
||||
|
@ -263,6 +268,10 @@ class ClientRequest extends EventEmitter {
|
|||
this._url_request.setChunkedUpload(this.chunkedEncoding)
|
||||
}
|
||||
|
||||
// Headers are assumed to be sent on first call to _writeBuffer,
|
||||
// i.e. after the first call to write or end.
|
||||
let result = this._url_request.write(chunk, isLast)
|
||||
|
||||
// The write callback is fired asynchronously to mimic Node.js.
|
||||
if (callback) {
|
||||
process.nextTick(callback)
|
||||
|
|
|
@ -1,19 +1,166 @@
|
|||
const assert = require('assert')
|
||||
const {remote} = require('electron')
|
||||
const http = require('http')
|
||||
const url = require('url')
|
||||
const {net} = remote
|
||||
|
||||
describe.only('net module', function() {
|
||||
this.timeout(0)
|
||||
describe('HTTP basics', function() {
|
||||
it ('should be able to fetch google.com', function(done) {
|
||||
|
||||
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) {
|
||||
this.timeout(30000);
|
||||
let response_event_emitted = false;
|
||||
let data_event_emitted = false;
|
||||
let end_event_emitted = false;
|
||||
let finish_event_emitted = false;
|
||||
const urlRequest = net.request({
|
||||
method: 'GET',
|
||||
url: 'https://www.google.com'
|
||||
})
|
||||
method: 'GET',
|
||||
url: 'https://www.google.com'
|
||||
})
|
||||
urlRequest.on('response', function(response) {
|
||||
response_event_emitted = true;
|
||||
const statusCode = response.statusCode
|
||||
|
@ -37,37 +184,37 @@ describe.only('net module', function() {
|
|||
body += buffer.toString()
|
||||
assert(typeof body === 'string')
|
||||
assert(body.length > 0)
|
||||
});
|
||||
});
|
||||
response.on('end', function() {
|
||||
end_event_emitted = true;
|
||||
})
|
||||
});
|
||||
})
|
||||
});
|
||||
urlRequest.on('finish', function() {
|
||||
finish_event_emitted = true;
|
||||
})
|
||||
})
|
||||
urlRequest.on('error', function(error) {
|
||||
assert.ifError(error);
|
||||
})
|
||||
})
|
||||
urlRequest.on('close', function() {
|
||||
assert(response_event_emitted)
|
||||
assert(data_event_emitted)
|
||||
assert(end_event_emitted)
|
||||
assert(finish_event_emitted)
|
||||
done()
|
||||
})
|
||||
})
|
||||
urlRequest.end();
|
||||
})
|
||||
})
|
||||
|
||||
it ('should be able to post data', function(done) {
|
||||
it ('should emit ClientRequest events in a POST request', function(done) {
|
||||
this.timeout(20000);
|
||||
let response_event_emitted = false;
|
||||
let data_event_emitted = false;
|
||||
let end_event_emitted = false;
|
||||
let finish_event_emitted = false;
|
||||
const urlRequest = net.request({
|
||||
method: 'POST',
|
||||
url: 'http://httpbin.org/post'
|
||||
});
|
||||
method: 'POST',
|
||||
url: 'http://httpbin.org/post'
|
||||
});
|
||||
urlRequest.on('response', function(response) {
|
||||
response_event_emitted = true;
|
||||
const statusCode = response.statusCode
|
||||
|
@ -93,54 +240,58 @@ describe.only('net module', function() {
|
|||
assert(end_event_emitted)
|
||||
assert(finish_event_emitted)
|
||||
done()
|
||||
})
|
||||
})
|
||||
response.on('data', function(buffer) {
|
||||
data_event_emitted = true;
|
||||
body += buffer.toString()
|
||||
assert(typeof body === 'string')
|
||||
assert(body.length > 0)
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
urlRequest.on('finish', function() {
|
||||
finish_event_emitted = true;
|
||||
})
|
||||
})
|
||||
urlRequest.on('error', function(error) {
|
||||
assert.ifError(error);
|
||||
})
|
||||
})
|
||||
urlRequest.on('close', function() {
|
||||
|
||||
})
|
||||
})
|
||||
for (let i = 0; i < 100; ++i) {
|
||||
urlRequest.write('Hello World!');
|
||||
}
|
||||
}
|
||||
urlRequest.end();
|
||||
})
|
||||
})
|
||||
describe('ClientRequest API', function() {
|
||||
|
||||
it ('should be able to set a custom HTTP header', function() {
|
||||
assert(false)
|
||||
})
|
||||
})
|
||||
it ('should be able to abort an HTTP request', function() {
|
||||
assert(false)
|
||||
})
|
||||
})
|
||||
it ('should be able to pipe into a request', function() {
|
||||
assert(false)
|
||||
})
|
||||
it ('should be able to pipe from a response', function() {
|
||||
assert(false)
|
||||
})
|
||||
})
|
||||
it ('should be able to create a request with options', function() {
|
||||
assert(false)
|
||||
})
|
||||
})
|
||||
it ('should be able to specify a custom session', function() {
|
||||
assert(false)
|
||||
})
|
||||
})
|
||||
it ('should support chunked encoding', function() {
|
||||
assert(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
describe('IncomingMessage API', function() {
|
||||
it('should provide a Node.js-similar API', function() {
|
||||
assert(false)
|
||||
})
|
||||
it ('should not emit any event after close', function() {
|
||||
assert(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
it ('should be able to pipe from a response', function() {
|
||||
assert(false)
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in a new issue