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