test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // TODO: write specs. :)
  2. var fs = require('fs'),
  3. client = require('./../../');
  4. process.env.DEBUG = true;
  5. var response_callback = function(err, resp, body){
  6. console.log(err);
  7. if(resp) console.log("Got status code " + resp.statusCode)
  8. console.log(body);
  9. }
  10. function simple_head(){
  11. client.head('http://www.amazon.com', response_callback);
  12. }
  13. function simple_get(){
  14. client.get('http://www.nodejs.org', response_callback);
  15. }
  16. function proxy_get(){
  17. client.get('https://www.google.com/search?q=nodejs', {proxy: 'http://localhost:1234'}, response_callback);
  18. }
  19. function auth_get(){
  20. client.get('https://www.twitter.com', {username: 'asd', password: '123'}, response_callback);
  21. }
  22. function simple_post(url){
  23. var data = {
  24. foo: 'bar',
  25. baz: {
  26. nested: 'attribute'
  27. }
  28. }
  29. client.post(url, data, response_callback);
  30. }
  31. function multipart_post(url){
  32. var filename = 'test_file.txt';
  33. var data = 'Plain text data.\nLorem ipsum dolor sit amet.\nBla bla bla.\n';
  34. fs.writeFileSync(filename, data);
  35. var black_pixel = Buffer.from("data:image/gif;base64,R0lGODlhAQABAIAAAAUEBAAAACwAAAAAAQABAAACAkQBADs=".replace(/^data:image\/\w+;base64,/, ""), "base64");
  36. var data = {
  37. foo: 'bar',
  38. bar: 'baz',
  39. nested: {
  40. my_document: { file: filename, content_type: 'text/plain' },
  41. even: {
  42. more: 'nesting'
  43. }
  44. },
  45. pixel: { filename: 'black_pixel.gif', buffer: black_pixel, content_type: 'image/gif' },
  46. field2: {value: JSON.stringify({"json":[ {"one":1}, {"two":2} ]}), content_type: 'application/json' }
  47. }
  48. client.post(url, data, {multipart: true}, function(err, resp, body){
  49. console.log(err);
  50. console.log("Got status code " + resp.statusCode)
  51. console.log(body);
  52. fs.unlink(filename);
  53. });
  54. }
  55. switch(process.argv[2]){
  56. case 'head':
  57. simple_head();
  58. break;
  59. case 'get':
  60. simple_get();
  61. break;
  62. case 'auth':
  63. auth_get();
  64. break;
  65. case 'proxy':
  66. proxy_get();
  67. break;
  68. case 'post':
  69. simple_post(process.argv[3] || 'http://posttestserver.com/post.php');
  70. break;
  71. case 'multipart':
  72. multipart_post(process.argv[3] || 'http://posttestserver.com/post.php?dir=example');
  73. break;
  74. case 'all':
  75. simple_head();
  76. simple_get();
  77. auth_get();
  78. proxy_get();
  79. simple_post(process.argv[3] || 'http://posttestserver.com/post.php');
  80. multipart_post(process.argv[3] || 'http://posttestserver.com/post.php?dir=example');
  81. break;
  82. default:
  83. console.log("Usage: ./test.js [head|get|auth|proxy|multipart]")
  84. }