var fs = require('fs');
var path = require('path');
+var url = require('url');
+
+var override = require('./utils/object').override;
var DEFAULT_TIMEOUT = 5000;
};
this.options.inliner.timeout = this.options.inliner.timeout || DEFAULT_TIMEOUT;
- this.options.inliner.request = this.options.inliner.request || {};
+ this.options.inliner.request = override(
+ /* jshint camelcase: false */
+ proxyOptionsFrom(process.env.HTTP_PROXY || process.env.http_proxy),
+ this.options.inliner.request || {}
+ );
};
function missingDirectory(filepath) {
return fs.existsSync(filepath) && fs.statSync(filepath).isDirectory();
}
+function proxyOptionsFrom(httpProxy) {
+ return httpProxy ?
+ {
+ hostname: url.parse(httpProxy).hostname,
+ port: parseInt(url.parse(httpProxy).port)
+ } :
+ {};
+}
+
CleanCSS.prototype.minify = function (data, callback) {
var context = {
stats: {},
var exec = require('child_process').exec;
var fs = require('fs');
var http = require('http');
+var httpProxy = require('http-proxy');
var path = require('path');
+var url = require('url');
var SourceMapConsumer = require('source-map').SourceMapConsumer;
var isWindows = process.platform == 'win32';
}
})
})
+ .addBatch({
+ 'HTTP proxy': unixOnlyContext({
+ topic: function () {
+ var self = this;
+ this.proxied = false;
+
+ var proxy = httpProxy.createProxyServer();
+ this.proxyServer = http.createServer(function (req, res) {
+ self.proxied = true;
+ proxy.web(req, res, { target: 'http://' + url.parse(req.url).host }, function () {});
+ });
+ this.proxyServer.listen(8081);
+
+ this.server = http.createServer(function (req, res) {
+ res.writeHead(200);
+ res.end('a{color:red}');
+ });
+ this.server.listen(8080);
+
+ exec('echo "@import url(http://127.0.0.1:8080/test.css);" | HTTP_PROXY=http://127.0.0.1:8081 ./bin/cleancss', this.callback);
+ },
+ 'proxies the connection': function () {
+ assert.isTrue(this.proxied);
+ },
+ 'gives right output': function (error, stdout) {
+ assert.equal(stdout, 'a{color:red}');
+ },
+ teardown: function () {
+ this.proxyServer.close();
+ this.server.close();
+ }
+ })
+ })
.addBatch({
'ie7 compatibility': binaryContext('--compatibility ie7 ./test/fixtures/unsupported/selectors-ie7.css', {
'should not transform source': function (error, stdout) {
-/* jshint unused: false */
+/* jshint unused: false, camelcase: false */
var vows = require('vows');
var assert = require('assert');
this.server.destroy();
nock.disableNetConnect();
}
- },
+ }
+}).addBatch({
'of a proxied resource': {
topic: function () {
+ var self = this;
+ nock.enableNetConnect();
+
this.proxied = false;
+ this.reqMocks = nock('http://assets.127.0.0.1')
+ .get('/styles.css')
+ .reply(200, 'a{color:red}');
+
+ var proxy = httpProxy.createProxyServer();
+ this.proxyServer = http.createServer(function (req, res) {
+ self.proxied = true;
+ proxy.web(req, res, { target: 'http://' + url.parse(req.url).host }, function () {});
+ });
+ this.proxyServer.listen(8080, function () {
+ var options = {
+ inliner: {
+ request: {
+ hostname: '127.0.0.1',
+ port: 8080
+ }
+ }
+ };
+
+ new CleanCSS(options).minify('@import url(http://assets.127.0.0.1/styles.css);', self.callback);
+ });
+ enableDestroy(this.proxyServer);
+ },
+ 'proxies the connection': function () {
+ assert.isTrue(this.proxied);
+ },
+ 'gets right output': function (errors, minified) {
+ assert.equal(minified.styles, 'a{color:red}');
+ },
+ teardown: function () {
+ assert.isTrue(this.reqMocks.isDone());
+ nock.cleanAll();
+ this.proxyServer.destroy();
+ }
+ }
+}).addBatch({
+ 'of a proxied resource via env variables': {
+ topic: function () {
var self = this;
+ nock.enableNetConnect();
+
+ this.reqMocks = nock('http://assets.127.0.0.1')
+ .get('/styles.css')
+ .reply(200, 'a{color:red}');
+
var proxy = httpProxy.createProxyServer();
+ this.proxied = false;
this.proxyServer = http.createServer(function (req, res) {
self.proxied = true;
- proxy.web(req, res, { target: 'http://' + url.parse(req.url).host });
+ proxy.web(req, res, { target: 'http://' + url.parse(req.url).host }, function (e) { console.log(e); });
+ });
+ this.proxyServer.listen(8081, function () {
+ process.env.http_proxy = 'http://127.0.0.1:8081';
+ new CleanCSS().minify('@import url(http://assets.127.0.0.1/styles.css);', self.callback);
});
- this.proxyServer.listen(8080);
enableDestroy(this.proxyServer);
+ },
+ 'proxies the connection': function () {
+ assert.isTrue(this.proxied);
+ },
+ 'gets right output': function (errors, minified) {
+ assert.equal(minified.styles, 'a{color:red}');
+ },
+ teardown: function () {
+ assert.isTrue(this.reqMocks.isDone());
+ nock.cleanAll();
+ this.proxyServer.destroy();
+ delete process.env.http_proxy;
+ }
+ }
+}).addBatch({
+ 'of a proxied resource via env variables overridden by options': {
+ topic: function () {
+ var self = this;
+ nock.enableNetConnect();
this.reqMocks = nock('http://assets.127.0.0.1')
.get('/styles.css')
.reply(200, 'a{color:red}');
- var options = {
- inliner: {
- request: {
- hostname: '127.0.0.1',
- port: 8080
+ var proxy = httpProxy.createProxyServer();
+ this.proxied = false;
+ this.proxyServer = http.createServer(function (req, res) {
+ self.proxied = true;
+ proxy.web(req, res, { target: 'http://' + url.parse(req.url).host }, function () {});
+ });
+ this.proxyServer.listen(8082, function () {
+ var options = {
+ inliner: {
+ request: {
+ hostname: '127.0.0.1',
+ port: 8082
+ }
}
- }
- };
+ };
- new CleanCSS(options).minify('@import url(http://assets.127.0.0.1/styles.css);', this.callback);
+ process.env.http_proxy = 'http://some-fake-proxy:8082';
+ new CleanCSS(options).minify('@import url(http://assets.127.0.0.1/styles.css);', self.callback);
+ });
+ enableDestroy(this.proxyServer);
},
'proxies the connection': function () {
assert.isTrue(this.proxied);
assert.isTrue(this.reqMocks.isDone());
nock.cleanAll();
this.proxyServer.destroy();
+ delete process.env.http_proxy;
}
}
}).export(module);