basic.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. var concat = require('concat-stream')
  2. var http = require('http')
  3. var get = require('../')
  4. var selfSignedHttps = require('self-signed-https')
  5. var str = require('string-to-stream')
  6. var test = require('tape')
  7. var zlib = require('zlib')
  8. // Allow self-signed certs
  9. process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
  10. test('simple get', function (t) {
  11. t.plan(4)
  12. var server = http.createServer(function (req, res) {
  13. t.equal(req.url, '/path')
  14. res.statusCode = 200
  15. res.end('response')
  16. })
  17. server.listen(0, function () {
  18. var port = server.address().port
  19. get('http://localhost:' + port + '/path', function (err, res) {
  20. t.error(err)
  21. t.equal(res.statusCode, 200)
  22. res.pipe(concat(function (data) {
  23. t.equal(data.toString(), 'response')
  24. server.close()
  25. }))
  26. })
  27. })
  28. })
  29. test('follow redirects (up to 10)', function (t) {
  30. t.plan(13)
  31. var num = 1
  32. var server = http.createServer(function (req, res) {
  33. t.equal(req.url, '/' + num, 'visited /' + num)
  34. num += 1
  35. if (num <= 10) {
  36. res.statusCode = 301
  37. res.setHeader('Location', '/' + num)
  38. res.end()
  39. } else {
  40. res.statusCode = 200
  41. res.end('response')
  42. }
  43. })
  44. server.listen(0, function () {
  45. var port = server.address().port
  46. get('http://localhost:' + port + '/1', function (err, res) {
  47. t.error(err)
  48. t.equal(res.statusCode, 200)
  49. res.pipe(concat(function (data) {
  50. t.equal(data.toString(), 'response')
  51. server.close()
  52. }))
  53. })
  54. })
  55. })
  56. test('do not follow redirects', function (t) {
  57. t.plan(2)
  58. var server = http.createServer(function (req, res) {
  59. t.equal(req.url, '/1', 'visited /1')
  60. res.statusCode = 301
  61. res.setHeader('Location', '/2')
  62. res.end()
  63. })
  64. server.listen(0, function () {
  65. var port = server.address().port
  66. get({
  67. url: 'http://localhost:' + port + '/1',
  68. maxRedirects: 0
  69. }, function (err) {
  70. t.ok(err instanceof Error, 'got error')
  71. server.close()
  72. })
  73. })
  74. })
  75. test('follow redirects (11 is too many)', function (t) {
  76. t.plan(11)
  77. var num = 1
  78. var server = http.createServer(function (req, res) {
  79. t.equal(req.url, '/' + num, 'visited /' + num)
  80. num += 1
  81. res.statusCode = 301
  82. res.setHeader('Location', '/' + num)
  83. res.end()
  84. })
  85. server.listen(0, function () {
  86. var port = server.address().port
  87. get('http://localhost:' + port + '/1', function (err) {
  88. t.ok(err instanceof Error, 'got error')
  89. server.close()
  90. })
  91. })
  92. })
  93. test('custom headers', function (t) {
  94. t.plan(2)
  95. var server = http.createServer(function (req, res) {
  96. t.equal(req.headers['custom-header'], 'custom-value')
  97. res.statusCode = 200
  98. res.end('response')
  99. })
  100. server.listen(0, function () {
  101. var port = server.address().port
  102. get({
  103. url: 'http://localhost:' + port,
  104. headers: {
  105. 'custom-header': 'custom-value'
  106. }
  107. }, function (err, res) {
  108. t.error(err)
  109. res.resume()
  110. server.close()
  111. })
  112. })
  113. })
  114. test('gzip response', function (t) {
  115. t.plan(3)
  116. var server = http.createServer(function (req, res) {
  117. res.statusCode = 200
  118. res.setHeader('content-encoding', 'gzip')
  119. str('response').pipe(zlib.createGzip()).pipe(res)
  120. })
  121. server.listen(0, function () {
  122. var port = server.address().port
  123. get('http://localhost:' + port, function (err, res) {
  124. t.error(err)
  125. t.equal(res.statusCode, 200) // statusCode still works on gunzip stream
  126. res.pipe(concat(function (data) {
  127. t.equal(data.toString(), 'response')
  128. server.close()
  129. }))
  130. })
  131. })
  132. })
  133. test('deflate response', function (t) {
  134. t.plan(3)
  135. var server = http.createServer(function (req, res) {
  136. res.statusCode = 200
  137. res.setHeader('content-encoding', 'deflate')
  138. str('response').pipe(zlib.createDeflate()).pipe(res)
  139. })
  140. server.listen(0, function () {
  141. var port = server.address().port
  142. get('http://localhost:' + port, function (err, res) {
  143. t.error(err)
  144. t.equal(res.statusCode, 200) // statusCode still works on inflate stream
  145. res.pipe(concat(function (data) {
  146. t.equal(data.toString(), 'response')
  147. server.close()
  148. }))
  149. })
  150. })
  151. })
  152. test('https', function (t) {
  153. t.plan(4)
  154. var server = selfSignedHttps(function (req, res) {
  155. t.equal(req.url, '/path')
  156. res.statusCode = 200
  157. res.end('response')
  158. })
  159. server.listen(0, function () {
  160. var port = server.address().port
  161. get('https://localhost:' + port + '/path', function (err, res) {
  162. t.error(err)
  163. t.equal(res.statusCode, 200)
  164. res.pipe(concat(function (data) {
  165. t.equal(data.toString(), 'response')
  166. server.close()
  167. }))
  168. })
  169. })
  170. })
  171. test('redirect https to http', function (t) {
  172. t.plan(5)
  173. var httpPort = null
  174. var httpsPort = null
  175. var httpsServer = selfSignedHttps(function (req, res) {
  176. t.equal(req.url, '/path1')
  177. res.statusCode = 301
  178. res.setHeader('Location', 'http://localhost:' + httpPort + '/path2')
  179. res.end()
  180. })
  181. var httpServer = http.createServer(function (req, res) {
  182. t.equal(req.url, '/path2')
  183. res.statusCode = 200
  184. res.end('response')
  185. })
  186. httpsServer.listen(0, function () {
  187. httpsPort = httpsServer.address().port
  188. httpServer.listen(0, function () {
  189. httpPort = httpServer.address().port
  190. get('https://localhost:' + httpsPort + '/path1', function (err, res) {
  191. t.error(err)
  192. t.equal(res.statusCode, 200)
  193. res.pipe(concat(function (data) {
  194. t.equal(data.toString(), 'response')
  195. httpsServer.close()
  196. httpServer.close()
  197. }))
  198. })
  199. })
  200. })
  201. })
  202. test('redirect http to https', function (t) {
  203. t.plan(5)
  204. var httpsPort = null
  205. var httpPort = null
  206. var httpServer = http.createServer(function (req, res) {
  207. t.equal(req.url, '/path1')
  208. res.statusCode = 301
  209. res.setHeader('Location', 'https://localhost:' + httpsPort + '/path2')
  210. res.end()
  211. })
  212. var httpsServer = selfSignedHttps(function (req, res) {
  213. t.equal(req.url, '/path2')
  214. res.statusCode = 200
  215. res.end('response')
  216. })
  217. httpServer.listen(0, function () {
  218. httpPort = httpServer.address().port
  219. httpsServer.listen(0, function () {
  220. httpsPort = httpsServer.address().port
  221. get('http://localhost:' + httpPort + '/path1', function (err, res) {
  222. t.error(err)
  223. t.equal(res.statusCode, 200)
  224. res.pipe(concat(function (data) {
  225. t.equal(data.toString(), 'response')
  226. httpsServer.close()
  227. httpServer.close()
  228. }))
  229. })
  230. })
  231. })
  232. })
  233. test('post (text body)', function (t) {
  234. t.plan(4)
  235. var server = http.createServer(function (req, res) {
  236. t.equal(req.method, 'POST')
  237. res.statusCode = 200
  238. req.pipe(res)
  239. })
  240. server.listen(0, function () {
  241. var port = server.address().port
  242. var opts = {
  243. url: 'http://localhost:' + port,
  244. body: 'this is the body'
  245. }
  246. get.post(opts, function (err, res) {
  247. t.error(err)
  248. t.equal(res.statusCode, 200)
  249. res.pipe(concat(function (data) {
  250. t.equal(data.toString(), 'this is the body')
  251. server.close()
  252. }))
  253. })
  254. })
  255. })
  256. test('post (buffer body)', function (t) {
  257. t.plan(4)
  258. var server = http.createServer(function (req, res) {
  259. t.equal(req.method, 'POST')
  260. res.statusCode = 200
  261. req.pipe(res)
  262. })
  263. server.listen(0, function () {
  264. var port = server.address().port
  265. var opts = {
  266. url: 'http://localhost:' + port,
  267. body: new Buffer('this is the body')
  268. }
  269. get.post(opts, function (err, res) {
  270. t.error(err)
  271. t.equal(res.statusCode, 200)
  272. res.pipe(concat(function (data) {
  273. t.equal(data.toString(), 'this is the body')
  274. server.close()
  275. }))
  276. })
  277. })
  278. })
  279. test('get.concat', function (t) {
  280. t.plan(4)
  281. var server = http.createServer(function (req, res) {
  282. res.statusCode = 200
  283. res.end('blah blah blah')
  284. })
  285. server.listen(0, function () {
  286. var port = server.address().port
  287. get.concat('http://localhost:' + port, function (err, data, res) {
  288. t.error(err)
  289. t.equal(res.statusCode, 200)
  290. t.ok(Buffer.isBuffer(data), '`data` is type buffer')
  291. t.equal(data.toString(), 'blah blah blah')
  292. server.close()
  293. })
  294. })
  295. })
  296. test('access `req` object', function (t) {
  297. t.plan(2)
  298. var server = http.createServer(function (req, res) {
  299. res.statusCode = 200
  300. res.end('response')
  301. })
  302. server.listen(0, function () {
  303. var port = server.address().port
  304. var req = get('http://localhost:' + port, function (err, res) {
  305. t.error(err)
  306. res.resume() // discard data
  307. server.close()
  308. })
  309. req.on('socket', function () {
  310. t.pass('got `socket` event')
  311. })
  312. })
  313. })