1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | "use strict";
|
20 |
|
21 | var fluid = require("infusion"),
|
22 | kettle = fluid.registerNamespace("kettle"),
|
23 | http = require("http"),
|
24 | jqUnit = fluid.require("node-jqunit", require, "jqUnit");
|
25 |
|
26 | fluid.require("ws", require, "kettle.npm.ws");
|
27 |
|
28 | fluid.registerNamespace("kettle.test");
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 | fluid.defaults("kettle.test.cookieJar", {
|
35 | gradeNames: ["fluid.component"],
|
36 | members: {
|
37 | cookie: "",
|
38 | parser: "@expand:kettle.npm.cookieParser({that}.options.secret)"
|
39 | }
|
40 | });
|
41 |
|
42 | fluid.defaults("kettle.test.request", {
|
43 | gradeNames: ["fluid.component"],
|
44 | invokers: {
|
45 | send: "fluid.notImplemented"
|
46 | },
|
47 | hostname: "localhost",
|
48 | port: 8081,
|
49 | path: "/",
|
50 | storeCookies: false,
|
51 | termMap: {}
|
52 | });
|
53 |
|
54 |
|
55 |
|
56 | fluid.defaults("kettle.test.request.http", {
|
57 | gradeNames: ["kettle.test.request"],
|
58 | events: {
|
59 | onComplete: null,
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | send: null
|
70 | },
|
71 | listeners: {
|
72 | "send.prepareRequest": {
|
73 | funcName: "kettle.test.request.http.prepareRequest",
|
74 | args: ["{that}", "{arguments}.0"],
|
75 | priority: "before:sendPayload"
|
76 | },
|
77 | "send.sendPayload": {
|
78 | funcName: "kettle.test.request.http.sendPayload",
|
79 | args: ["{that}", "{arguments}.0"]
|
80 | }
|
81 | },
|
82 | invokers: {
|
83 | send: {
|
84 | funcName: "kettle.test.request.http.send",
|
85 | args: [
|
86 | "{that}",
|
87 | "{cookieJar}",
|
88 | "{that}.events.onComplete.fire",
|
89 | "{arguments}.0",
|
90 | "{arguments}.1"
|
91 | ]
|
92 | }
|
93 | }
|
94 | });
|
95 |
|
96 |
|
97 | fluid.defaults("kettle.test.request.httpCookie", {
|
98 | gradeNames: ["kettle.test.request.http"],
|
99 | storeCookies: true
|
100 | });
|
101 |
|
102 | kettle.test.request.http.prepareRequest = function (that, optsObject) {
|
103 |
|
104 | var cookieJar = optsObject.cookieJar,
|
105 | callback = optsObject.callback,
|
106 | directOptions = optsObject.directOptions;
|
107 |
|
108 | if (that.nativeRequest) {
|
109 | fluid.fail("You cannot reuse a kettle.test.request.http object once it has been sent - please construct a fresh component for this request");
|
110 | }
|
111 |
|
112 | var requestOptions = kettle.dataSource.URL.prepareRequestOptions(that.options, cookieJar, directOptions, kettle.dataSource.URL.requestOptions, that.resolveUrl);
|
113 |
|
114 | fluid.log("Sending a " + (requestOptions.method || "GET") + " request to: ", requestOptions.path, " on port " + requestOptions.port);
|
115 |
|
116 | var req = that.nativeRequest = http.request(requestOptions, function (res) {
|
117 | that.nativeResponse = res;
|
118 | var data = "";
|
119 | res.setEncoding("utf8");
|
120 |
|
121 | res.on("data", function (chunk) {
|
122 | data += chunk;
|
123 | });
|
124 |
|
125 | res.on("close", function (err) {
|
126 | if (err) {
|
127 | fluid.fail("Error making request to " + requestOptions.path + ": " + err.message);
|
128 | }
|
129 | });
|
130 |
|
131 | res.on("end", function () {
|
132 | var cookie = res.headers["set-cookie"];
|
133 | var pseudoReq = {};
|
134 | if (cookie && that.options.storeCookies) {
|
135 | if (fluid.isDestroyed(cookieJar)) {
|
136 | fluid.fail("Stored cookie in destroyed jar");
|
137 | }
|
138 | cookieJar.cookie = cookie;
|
139 |
|
140 |
|
141 | pseudoReq = {
|
142 | headers: {
|
143 | cookie: cookie[0]
|
144 | }
|
145 | };
|
146 |
|
147 |
|
148 | cookieJar.parser(pseudoReq, {}, fluid.identity);
|
149 | }
|
150 | callback(data, that, pseudoReq);
|
151 | });
|
152 | });
|
153 |
|
154 |
|
155 |
|
156 | req.shouldKeepAlive = false;
|
157 |
|
158 | req.on("error", function (err) {
|
159 | jqUnit.fail("Error making request to " + requestOptions.path + ": " + err.message);
|
160 | });
|
161 | };
|
162 |
|
163 | kettle.test.request.http.sendPayload = function (that, optsObject) {
|
164 |
|
165 | var req = that.nativeRequest,
|
166 | payload = optsObject.payload;
|
167 | if (payload) {
|
168 | payload = typeof payload === "string" ? payload : JSON.stringify(payload);
|
169 | req.setHeader("Content-Type", req.getHeader("Content-Type") || "application/json");
|
170 | req.setHeader("Content-Length", payload.length);
|
171 | }
|
172 |
|
173 | if (payload) {
|
174 | req.write(payload);
|
175 | }
|
176 |
|
177 | req.end();
|
178 | };
|
179 |
|
180 |
|
181 |
|
182 | kettle.test.request.http.send = function (that, cookieJar, callback, payload, directOptions) {
|
183 |
|
184 | var optsObject = {
|
185 | cookieJar: cookieJar,
|
186 | callback: callback,
|
187 | payload: payload,
|
188 | directOptions: directOptions
|
189 | };
|
190 |
|
191 | that.events.send.fire(optsObject);
|
192 | };
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 | kettle.test.assertResponseStatusCode = function (options, defaultCode) {
|
208 | var statusCode = options.statusCode || defaultCode;
|
209 | jqUnit.assertEquals(options.message + " statusCode", statusCode, options.request.nativeResponse.statusCode);
|
210 | };
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | kettle.test.assertJSONResponse = function (options) {
|
218 | var data;
|
219 | try {
|
220 | data = kettle.JSON.parse(options.string);
|
221 | } catch (e) {
|
222 | throw kettle.upgradeError(e, "\nwhile parsing HTTP response as JSON");
|
223 | }
|
224 | jqUnit.assertDeepEq(options.message, options.expected, data);
|
225 | kettle.test.assertResponseStatusCode(options, 200);
|
226 | };
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 |
|
235 | kettle.test.assertResponse = function (options) {
|
236 | if (options.plainText) {
|
237 | if (options.expectedSubstring) {
|
238 | jqUnit.assertTrue(options.message, options.string.indexOf(options.expectedSubstring) !== -1);
|
239 | } else {
|
240 | jqUnit.assertEquals(options.message, options.expected, options.string);
|
241 | }
|
242 | kettle.test.assertResponseStatusCode(options, 200);
|
243 | } else {
|
244 | kettle.test.assertJSONResponse(options);
|
245 | }
|
246 | };
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 | kettle.test.assertErrorResponse = function (options) {
|
258 | var data = options.plainText ? {message: options.string} : kettle.JSON.parse(options.string);
|
259 | if (!options.plainText) {
|
260 | jqUnit.assertEquals(options.message + " isError field set", true, data.isError);
|
261 | }
|
262 | var errorTexts = fluid.makeArray(options.errorTexts);
|
263 | fluid.each(errorTexts, function (errorText) {
|
264 | jqUnit.assertTrue(options.message + " - message text \"" + data.message + "\" must contain " + errorText, data.message.indexOf(errorText) >= 0);
|
265 | });
|
266 | kettle.test.assertResponseStatusCode(options, 500);
|
267 | };
|