{"id":1051,"date":"2013-01-11T17:29:06","date_gmt":"2013-01-11T16:29:06","guid":{"rendered":"https:\/\/www.unicoda.com\/?p=1051"},"modified":"2013-01-12T18:21:20","modified_gmt":"2013-01-12T17:21:20","slug":"request-add-noproxy-configuration","status":"publish","type":"post","link":"https:\/\/www.unicoda.com\/?p=1051","title":{"rendered":"Request: noproxy configuration"},"content":{"rendered":"<p>Like with npm and for the same reasons, it would be a great idea to have a noproxy configuration in request.<br \/>\nSo here is the pull request!<\/p>\n<pre>if(self.noproxy) {\r\n  if(typeof self.noproxy == 'string') {\r\n    if(self.noproxy.search(self.uri.hostname) !== -1) {\r\n      delete self.proxy\r\n    }\r\n  }\r\n}<\/pre>\n<p>Really simple. If the hostname is in the noproxy string, we delete the proxy parameter so that it won&rsquo;t be used.<\/p>\n<p>And the test which validate the modification:<\/p>\n<pre>\/*\r\n** Test noproxy configuration.\r\n**\r\n** We create a server and a proxy.\r\n** Server listens on localhost:80.\r\n** Proxy listens on localhost:8080.\r\n** The proxy redirects all requests to \/proxy on the server.\r\n** On the server, \/proxy sends \"proxy\" .\r\n** When server is directly requested, it answers with \"noproxy\" .\r\n**\r\n**\r\n** So we perform 2 tests, both with proxy equal to \"http:\/\/localhost:8080\".\r\n** -A test is performed with noproxy equal to \"null\". In this case,\r\n** the server responds with \"proxy\" because the proxy is used.\r\n** -In the other test, noproxy equal \"localhost, example.com\".\r\n** Since localhost is part of noproxy, request is made directly\r\n** to the server and proxy is ignored.\r\n*\/\r\n\r\nvar assert = require(\"assert\")\r\n  , http = require('http')\r\n  , request = require('..\/main.js')\r\n  \/\/We create a server and a proxy.\r\n  , server = http.createServer(function(req, res){\r\n      res.statusCode = 200\r\n      if(req.url == '\/proxy') {\r\n        res.end('proxy')\r\n      } else {\r\n        res.end('noproxy')\r\n      }\r\n    })\r\n  , proxy = http.createServer(function (req, res) {\r\n      res.statusCode = 200\r\n      var url = 'http:\/\/localhost:80\/proxy'\r\n      var x = request(url)\r\n      req.pipe(x)\r\n      x.pipe(res)\r\n    })\r\n    ;\r\n\r\n\/\/Launch server and proxy\r\nvar initialize = function (cb) {\r\n  server.listen(80, 'localhost', function () {\r\n    proxy.listen(8080, 'localhost', cb)\r\n  })\r\n}\r\n\r\n\/\/Tests\r\ninitialize(function () {\r\n  \/\/Checking the route for server and proxy\r\n  request.get(\"http:\/\/localhost:80\/test\", function (err, res, body) {\r\n    assert.equal(res.statusCode, 200)\r\n    request.get(\"http:\/\/localhost:80\/proxy\", function (err, res2, body) {\r\n      assert.equal(res2.statusCode, 200)\r\n      request.get(\"http:\/\/localhost:8080\/test\", function (err, res3, body) {\r\n        assert.equal(res3.statusCode, 200)\r\n        makeNoProxyTest(function () {\r\n          makeProxyTest(function () {\r\n            closeServer(server)\r\n            closeServer(proxy)\r\n          })\r\n        })\r\n      })\r\n    })\r\n  })\r\n})\r\n\r\n\/\/Request with noproxy\r\nvar makeNoProxyTest = function (cb) {\r\n  request ({\r\n    url: 'http:\/\/localhost:80\/test',\r\n    proxy: 'http:\/\/localhost:8080',\r\n    noproxy: 'localhost, example.com'\r\n  }, function (err, res, body) {\r\n    assert.equal(body, 'noproxy')\r\n    cb()\r\n  })\r\n}\r\n\r\n\/\/Request with proxy\r\nvar makeProxyTest = function (cb) {\r\n  request ({\r\n    url: 'http:\/\/localhost:80\/test',\r\n    proxy: 'http:\/\/localhost:8080',\r\n    noproxy: 'null'\r\n  }, function (err, res, body) {\r\n    assert.equal(body, 'proxy')\r\n    cb()\r\n  })\r\n}\r\n\r\nvar closeServer = function (s) {\r\n  s.close()\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Like with npm and for the same reasons, it would be a great idea to have a noproxy configuration in request. So here is the pull request! if(self.noproxy) { if(typeof self.noproxy == &lsquo;string&rsquo;) { if(self.noproxy.search(self.uri.hostname) !== -1) { delete self.proxy } } } Really simple. If the hostname is in the noproxy string, we delete &hellip; <a href=\"https:\/\/www.unicoda.com\/?p=1051\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;Request: noproxy configuration&nbsp;&raquo;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[13,27,81,34,97],"class_list":["post-1051","post","type-post","status-publish","format-standard","hentry","category-code","tag-javascript","tag-nodejs","tag-noproxy","tag-pull-request","tag-request"],"_links":{"self":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1051","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1051"}],"version-history":[{"count":3,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1051\/revisions"}],"predecessor-version":[{"id":1062,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1051\/revisions\/1062"}],"wp:attachment":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}