-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestConnector.js
154 lines (127 loc) · 5.04 KB
/
TestConnector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const path = require("path");
const fs = require("fs");
const md5 = require("md5");
const cp = require('child_process');
var TestConnector = function()
{
this.fetch = function(url,options)
{
try
{
if(TestConnector.test_base_dir == "")
throw "You need to set TestConnector.test_base_dir to __dirname from inside your test script";
var fixture_path = TestConnector.fixturePath(TestConnector.test_base_dir,url,options);
var output = "";
if(TestConnector.nocache)
throw new Error("Force load");
var output = fs.readFileSync(fixture_path).toString();
}
catch(e)
{
var live_url = url.replace("key=dummy&token=dummy","key="+TestConnector.live_key+"&token="+TestConnector.live_token);
var header_string = "";
var std_data = true;
if(options.headers)
{
header_string = '--header "'+new IterableCollection(options.headers).implodeValues('" --header "',function(elem,key)
{
return key+": "+elem;
})+'" ';
}
if(options.payload)
{
if(options.headers)
{
if(options.headers["content-type"] == "application/json")
{
std_data = false;
try
{
options.payload = JSON.parse(options.payload);
}
catch(e)
{
}
var data_string = JSON.stringify(new IterableCollection(options.payload).find(function(elem,key)
{
if(key == "key")
return TestConnector.live_key;
else if(key == "token")
return TestConnector.live_token;
else
return elem;
}).obj);
}
}
if(std_data)
{
var data_string = new IterableCollection(options.payload).implode("&",function(elem,key)
{
if(key == "key")
return TestConnector.live_key;
else if(key == "token")
return TestConnector.live_token;
else
return encodeURIComponent(elem);
});
}
var cmd = 'curl '+header_string+'--data "'+data_string.split('"').join('\\"')+'" --request '+options.method.toUpperCase()+" --url \""+live_url+'\"';
}
else
var cmd = "curl "+header_string+"--request "+options.method.toUpperCase()+" --url \""+live_url+'\"';
var stdout = cp.execSync(cmd,{ stdio: ['pipe', 'pipe', 'ignore']});
if(stdout)
{
if((TestConnector.test_base_dir != "") && (!TestConnector.nocache))
{
fs.writeFileSync(fixture_path,stdout);
var output = fs.readFileSync(fixture_path).toString();
}
else
var output = stdout.toString();
try
{
JSON.parse(stdout);
}
catch(e)
{
throw new InvalidRequestException(stdout);
}
}
}
if(!output)
{
console.log("No test return content for: "+fixture_path);
console.log(JSON.stringify(options));
console.log(url);
console.log("Output: "+output);
}
return output;
}
}
TestConnector.sequencer = 0;
TestConnector.use_sequencer = false;
TestConnector.test_base_dir = "";
TestConnector.live_key = process.argv[2];
TestConnector.live_token = process.argv[3];
TestConnector.prefix = "";
TestConnector.nocache = false;
TestConnector.fixturePath = function(base_dir,url,options)
{
var signature = md5(url+JSON.stringify(options));
if(TestConnector.use_sequencer)
{
signature = signature+TestConnector.sequencer.toString();
TestConnector.sequencer++;
}
var api_cache_dir = path.resolve(base_dir,"./trello_api_fixtures/").toString();
!fs.existsSync(api_cache_dir) && fs.mkdirSync(api_cache_dir);
var fixture_dir = path.resolve(base_dir,"./trello_api_fixtures/"+path.basename(process.argv[1])).toString();
!fs.existsSync(fixture_dir) && fs.mkdirSync(fixture_dir);
var fixture_path = fixture_dir+"/"+TestConnector.prefix+signature;
return fixture_path;
}
function writeInfo_(msg)
{
console.log(msg);
}