UNPKG

5.99 kBJavaScriptView Raw
1/**
2 * Kettle Socket Support Tests
3 *
4 * Copyright 2013 OCAD University
5 *
6 * Licensed under the New BSD license. You may not use this file except in
7 * compliance with this License.
8 *
9 * You may obtain a copy of the License at
10 * https://github.com/fluid-project/kettle/blob/master/LICENSE.txt
11 */
12
13"use strict";
14
15var fluid = require("infusion"),
16 kettle = require("../kettle.js"),
17 jqUnit = fluid.require("node-jqunit", require, "jqUnit");
18
19kettle.loadTestingSupport();
20
21fluid.defaults("kettle.tests.ws.testSocket.handler", {
22 gradeNames: "kettle.request.ws",
23 listeners: {
24 onReceiveMessage: "kettle.tests.ws.testSocket.receiveMessage"
25 }
26});
27
28kettle.tests.ws.successResponse = {
29 success: true
30};
31
32
33kettle.tests.ws.messageCount = 0;
34
35kettle.tests.ws.testSocket.receiveMessage = function (request, data) {
36 jqUnit.assertDeepEq("Socket message data is correct", {
37 index: kettle.tests.ws.messageCount++,
38 test: true
39 }, data);
40 var promise = request.sendMessage(kettle.tests.ws.successResponse);
41 // Interestingly we can do close EITHER synchronously OR asynchronously, but
42 // we cannot do terminate EITHER synchronously OR asynchronously - so there's little
43 // value currently to listening to the promise, but it's there as a courtesy
44 if (data.index === 1) {
45 request.ws.close(); // leave this here for early warning if synchronous close fails in future
46 promise.then(function () {
47 request.ws.close();
48 });
49 }
50};
51
52fluid.defaults("kettle.tests.ws.testGet.handler", {
53 gradeNames: "kettle.request.http",
54 invokers: {
55 handleRequest: {
56 funcName: "kettle.tests.ws.testGet.handleRequest"
57 }
58 }
59});
60
61kettle.tests.ws.testGet.handleRequest = function (request) {
62 jqUnit.assertTrue("The request was received.", true);
63 request.events.onSuccess.fire(kettle.tests.ws.successResponse);
64};
65
66kettle.tests.ws.testSocketResponse = function (data) {
67 jqUnit.assertDeepEq("Socket message delivered confirmed", {
68 success: true
69 }, data);
70};
71
72kettle.tests.ws.testSocketError = function (err, that) {
73 kettle.test.assertErrorResponse({
74 request: that,
75 message: "Received WebSockets error event applying to plain HTTP endpoint",
76 string: err,
77 errorTexts: ["WebSockets", "HTTP"],
78 statusCode: 400
79 });
80};
81
82fluid.defaults("kettle.tests.ws.testClose.handler", {
83 gradeNames: "kettle.request.ws",
84 listeners: {
85 onReceiveMessage: "kettle.tests.ws.testClose.receiveMessage"
86 }
87});
88
89kettle.tests.ws.testClose.receiveMessage = function (request) {
90 // close the WebSocket with the status code for normal closure
91 request.ws.close(1000);
92};
93
94kettle.tests.ws.assertCloseResponse = function (response) {
95 jqUnit.assertEquals("Socket close response code is 1000", 1000, response.code);
96};
97
98kettle.tests.ws.testDefs = {
99 name: "WebSockets tests",
100 expect: 17,
101 config: {
102 configName: "kettle.tests.webSockets.config",
103 configPath: "%kettle/tests/configs"
104 },
105 components: {
106 httpRequest: {
107 type: "kettle.test.request.http"
108 },
109 badHttpRequest: { // A "bad" request that attempts to connect to a WS endpoint via plain HTTP
110 type: "kettle.test.request.http",
111 options: {
112 path: "/socket_path"
113 }
114 },
115 wsRequest: {
116 type: "kettle.test.request.ws",
117 options: {
118 path: "/socket_path"
119 }
120 },
121 badWsRequest: {
122 type: "kettle.test.request.ws",
123 options: {
124 path: "/"
125 }
126 },
127 closingWsRequest: {
128 type: "kettle.test.request.ws",
129 options: {
130 path: "/close_path"
131 }
132 }
133 },
134 sequence: [{
135 func: "{wsRequest}.connect"
136 }, {
137 event: "{wsRequest}.events.onConnect",
138 listener: "jqUnit.assert",
139 args: "Received WebSockets connection event"
140 }, {
141 func: "{wsRequest}.send",
142 args: {
143 index: 0,
144 test: true
145 }
146 }, {
147 event: "{wsRequest}.events.onReceiveMessage",
148 listener: "kettle.tests.ws.testSocketResponse"
149 }, {
150 func: "{wsRequest}.send",
151 args: {
152 index: 1,
153 test: true
154 }
155 }, {
156 event: "{wsRequest}.events.onReceiveMessage",
157 listener: "kettle.tests.ws.testSocketResponse"
158 }, {
159 func: "{httpRequest}.send"
160 }, {
161 event: "{httpRequest}.events.onComplete",
162 listener: "kettle.test.assertJSONResponse",
163 args: {
164 message: "Received standard response to HTTP handler attached to ws-aware server",
165 expected: kettle.tests.ws.successResponse,
166 string: "{arguments}.0",
167 request: "{httpRequest}"
168 }
169 }, {
170 func: "{badHttpRequest}.send"
171 }, {
172 event: "{badHttpRequest}.events.onComplete",
173 listener: "kettle.test.assertErrorResponse",
174 args: {
175 message: "Received 426 error for plain HTTP request to WebSockets endpoint",
176 errorTexts: "WebSockets",
177 statusCode: 426,
178 string: "{arguments}.0",
179 request: "{badHttpRequest}"
180 }
181 }, {
182 func: "{badWsRequest}.connect"
183 }, {
184 event: "{badWsRequest}.events.onError",
185 listener: "kettle.tests.ws.testSocketError"
186 }, {
187 func: "{closingWsRequest}.connect"
188 }, {
189 event: "{closingWsRequest}.events.onConnect",
190 listener: "jqUnit.assert",
191 args: "Received WebSockets connection event"
192 }, {
193 func: "{closingWsRequest}.send",
194 args: {}
195 }, {
196 event: "{closingWsRequest}.events.onClose",
197 listener: "kettle.tests.ws.assertCloseResponse",
198 args: "{arguments}.1"
199 }
200]
201};
202
203kettle.test.bootstrapServer(kettle.tests.ws.testDefs);