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
 | 
					    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) {
 | 
					  _storeInternalData(chunk) {
 | 
				
			||||||
    this._data.push(chunk)
 | 
					    this._data.push(chunk)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -113,7 +121,7 @@ class ClientRequest extends EventEmitter {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!urlStr) {
 | 
					    if (!urlStr) {
 | 
				
			||||||
      let urlObj = {}
 | 
					      let urlObj = {}
 | 
				
			||||||
      const protocol = options.protocol || 'http'
 | 
					      const protocol = options.protocol || 'http:'
 | 
				
			||||||
      if (!kSupportedProtocols.has(protocol)) {
 | 
					      if (!kSupportedProtocols.has(protocol)) {
 | 
				
			||||||
        throw new Error('Protocol "' + protocol + '" not supported. ')
 | 
					        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)) {
 | 
					      if (options.path && / /.test(options.path)) {
 | 
				
			||||||
        // The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
 | 
					        // The actual regex is more like /[^A-Za-z0-9\-._~!$&'()*+,;=/:@]/
 | 
				
			||||||
        // with an additional rule for ignoring percentage-escaped characters
 | 
					        // with an additional rule for ignoring percentage-escaped characters
 | 
				
			||||||
| 
						 | 
					@ -143,7 +150,9 @@ class ClientRequest extends EventEmitter {
 | 
				
			||||||
        // an invalid request.
 | 
					        // an invalid request.
 | 
				
			||||||
        throw new TypeError('Request path contains unescaped characters.')
 | 
					        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)
 | 
					      urlStr = url.format(urlObj)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -252,10 +261,6 @@ class ClientRequest extends EventEmitter {
 | 
				
			||||||
      chunk = Buffer.from(chunk, encoding)
 | 
					      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
 | 
					    // Since writing to the network is asynchronous, we conservatively
 | 
				
			||||||
    // assume that request headers are written after delivering the first
 | 
					    // assume that request headers are written after delivering the first
 | 
				
			||||||
    // buffer to the network IO thread.
 | 
					    // buffer to the network IO thread.
 | 
				
			||||||
| 
						 | 
					@ -263,6 +268,10 @@ class ClientRequest extends EventEmitter {
 | 
				
			||||||
      this._url_request.setChunkedUpload(this.chunkedEncoding)
 | 
					      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.
 | 
					    // The write callback is fired asynchronously to mimic Node.js.
 | 
				
			||||||
    if (callback) {
 | 
					    if (callback) {
 | 
				
			||||||
      process.nextTick(callback)
 | 
					      process.nextTick(callback)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,10 +1,157 @@
 | 
				
			||||||
const assert = require('assert')
 | 
					const assert = require('assert')
 | 
				
			||||||
const {remote} = require('electron')
 | 
					const {remote} = require('electron')
 | 
				
			||||||
 | 
					const http = require('http')
 | 
				
			||||||
 | 
					const url = require('url')
 | 
				
			||||||
const {net} = remote
 | 
					const {net} = remote
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe.only('net module', function() {
 | 
					describe.only('net module', function() {
 | 
				
			||||||
 | 
					  this.timeout(0)
 | 
				
			||||||
  describe('HTTP basics', function() {
 | 
					  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);
 | 
					      this.timeout(30000);
 | 
				
			||||||
      let response_event_emitted = false;
 | 
					      let response_event_emitted = false;
 | 
				
			||||||
      let data_event_emitted = false;
 | 
					      let data_event_emitted = false;
 | 
				
			||||||
| 
						 | 
					@ -58,7 +205,7 @@ describe.only('net module', function() {
 | 
				
			||||||
      urlRequest.end();
 | 
					      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);
 | 
					      this.timeout(20000);
 | 
				
			||||||
      let response_event_emitted = false;
 | 
					      let response_event_emitted = false;
 | 
				
			||||||
      let data_event_emitted = false;
 | 
					      let data_event_emitted = false;
 | 
				
			||||||
| 
						 | 
					@ -116,8 +263,7 @@ describe.only('net module', function() {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
      urlRequest.end();
 | 
					      urlRequest.end();
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  })
 | 
					
 | 
				
			||||||
  describe('ClientRequest API', function() {
 | 
					 | 
				
			||||||
    it ('should be able to set a custom HTTP header', function() {
 | 
					    it ('should be able to set a custom HTTP header', function() {
 | 
				
			||||||
      assert(false)
 | 
					      assert(false)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
| 
						 | 
					@ -127,9 +273,6 @@ describe.only('net module', function() {
 | 
				
			||||||
    it ('should be able to pipe into a request', function() {
 | 
					    it ('should be able to pipe into a request', function() {
 | 
				
			||||||
      assert(false)
 | 
					      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() {
 | 
					    it ('should be able to create a request with options', function() {
 | 
				
			||||||
      assert(false)
 | 
					      assert(false)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
| 
						 | 
					@ -138,9 +281,17 @@ describe.only('net module', function() {
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
    it ('should support chunked encoding', function() {
 | 
					    it ('should support chunked encoding', function() {
 | 
				
			||||||
      assert(false)
 | 
					      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() {
 | 
					    it ('should not emit any event after close', function() {
 | 
				
			||||||
      assert(false)
 | 
					      assert(false)
 | 
				
			||||||
 | 
					  })
 | 
				
			||||||
 | 
					    it ('should be able to pipe from a response', function() {
 | 
				
			||||||
 | 
					      assert(false)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue