Test Driven JavaScript Development- P19
Số trang: 20
Loại file: pdf
Dung lượng: 190.96 KB
Lượt xem: 17
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Test Driven JavaScript Development- P19:This book is about programming JavaScript for the real world, using the techniquesand workflow suggested by Test-Driven Development. It is about gaining confidencein your code through test coverage, and gaining the ability to fearlessly refactor andorganically evolve your code base. It is about writing modular and testable code. Itis about writing JavaScript that works in a wide variety of environments and thatdoesn’t get in your user’s way.
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P19 14.2 The Controller 353 Unfortunately, this doesn’t play out exactly as planned. The previous test, which also calls post, is now attempting to call addMessage on chatRoom, which is undefined in that test. We can fix the issue by moving the chatRoom stub into setUp as Listing 14.18 does. Listing 14.18 Sharing the chatRoom stub function controllerSetUp() { /* ... */ this.controller.chatRoom = { addMessage: stub() }; } All the tests go back to a soothing green, and we can turn our attention to the duplicated logic we just introduced in the second test. In particular, both tests simulates sending a request with a body. We can simplify the tests considerably by extracting this logic into the setup. Listing 14.19 shows the updated tests. Listing 14.19 Cleaning up post tests function controllerSetUp() { /* ... */ this.sendRequest = function (data) { var str = encodeURI(JSON.stringify(data)); this.req.emit(data, str.substring(0, str.length / 2)); this.req.emit(data, str.substring(str.length / 2)); this.req.emit(end); }; } testCase(exports, chatRoomController.post, { /* ... */ should parse request body as JSON: function (test) { var data = { data: { user: cjno, message: hi } }; JSON.parse = stub(data); this.controller.post(); this.sendRequest(data); test.equals(JSON.parse.args[0], JSON.stringify(data)); test.done(); }, /* ... */ });Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 354 Server-Side JavaScript with Node.js The cleaned up tests certainly are a lot easier to follow, and with the send- Request helper method, writing new tests that make requests will be easier as well. All tests pass and we can move on. 14.2.4.3 Malicious Data Notice that we are currently accepting messages completely unfiltered. This can lead to all kinds of scary situations, for instance consider the effects of the request in Listing 14.20 Listing 14.20 Malicious request { topic: message, data: { user: cjno, message: window.location = http://hacked; } } Before deploying an application like the one we are currently building we should take care to not blindly accept any end user data unfiltered. 14.2.5 Responding to Requests When the controller has added the message, it should respond and close the connec- tion. In most web frameworks, output buffering and closing the connection happen automatically behind the scenes. The HTTP server support in Node, however, was consciously designed with data streaming and long polling in mind. For this reason, data is never buffered, and connections are never closed until told to do so. http.ServerResponse objects offer a few methods useful to output a re- sponse, namely writeHead, which writes the status code and response headers; write, which writes a chunk to the response body; and finally end. 14.2.5.1 Status Code As there really isn’t much feedback to give the user when a message is added, Listing 14.21 simply expects post to respond with an empty “201 Created.” Listing 14.21 Expecting status code 201 function controllerSetUp() { /* ... */ var res = this.res = { writeHead: stub() };Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 14.2 The Controller 355 /* ... */ } testCase(exports, chatRoomController.post, { /* ... */ should write status header: function (test) { var data = { data: { user: cjno, message: hi } }; this.controller.post(); this.sendRequest(data); test.ok(this.res.writeHead.called); test.equals(this.res.writeHead.args[0], 201); test.done(); } }); Listing 14.22 faces the challenge and makes the actual call to writeHead. Listing 14.22 Setting the response code post: function () { /* ... */ this.request.addListener(end, function () { var data = JSON.parse(decodeURI(body)).data; this.chatRoom.addMessage(data.user, data.message); this.response.writeHead(201); }.bind(this)); } 14.2.5.2 Closing the Connection Once the headers have been written, we should make sure the connection is closed. Listing 14.23 shows the test. Listing 14.23 Expecting the response to be closed function controllerSetUp() { /* ... */ var res = this.res = { ...
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P19 14.2 The Controller 353 Unfortunately, this doesn’t play out exactly as planned. The previous test, which also calls post, is now attempting to call addMessage on chatRoom, which is undefined in that test. We can fix the issue by moving the chatRoom stub into setUp as Listing 14.18 does. Listing 14.18 Sharing the chatRoom stub function controllerSetUp() { /* ... */ this.controller.chatRoom = { addMessage: stub() }; } All the tests go back to a soothing green, and we can turn our attention to the duplicated logic we just introduced in the second test. In particular, both tests simulates sending a request with a body. We can simplify the tests considerably by extracting this logic into the setup. Listing 14.19 shows the updated tests. Listing 14.19 Cleaning up post tests function controllerSetUp() { /* ... */ this.sendRequest = function (data) { var str = encodeURI(JSON.stringify(data)); this.req.emit(data, str.substring(0, str.length / 2)); this.req.emit(data, str.substring(str.length / 2)); this.req.emit(end); }; } testCase(exports, chatRoomController.post, { /* ... */ should parse request body as JSON: function (test) { var data = { data: { user: cjno, message: hi } }; JSON.parse = stub(data); this.controller.post(); this.sendRequest(data); test.equals(JSON.parse.args[0], JSON.stringify(data)); test.done(); }, /* ... */ });Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 354 Server-Side JavaScript with Node.js The cleaned up tests certainly are a lot easier to follow, and with the send- Request helper method, writing new tests that make requests will be easier as well. All tests pass and we can move on. 14.2.4.3 Malicious Data Notice that we are currently accepting messages completely unfiltered. This can lead to all kinds of scary situations, for instance consider the effects of the request in Listing 14.20 Listing 14.20 Malicious request { topic: message, data: { user: cjno, message: window.location = http://hacked; } } Before deploying an application like the one we are currently building we should take care to not blindly accept any end user data unfiltered. 14.2.5 Responding to Requests When the controller has added the message, it should respond and close the connec- tion. In most web frameworks, output buffering and closing the connection happen automatically behind the scenes. The HTTP server support in Node, however, was consciously designed with data streaming and long polling in mind. For this reason, data is never buffered, and connections are never closed until told to do so. http.ServerResponse objects offer a few methods useful to output a re- sponse, namely writeHead, which writes the status code and response headers; write, which writes a chunk to the response body; and finally end. 14.2.5.1 Status Code As there really isn’t much feedback to give the user when a message is added, Listing 14.21 simply expects post to respond with an empty “201 Created.” Listing 14.21 Expecting status code 201 function controllerSetUp() { /* ... */ var res = this.res = { writeHead: stub() };Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 14.2 The Controller 355 /* ... */ } testCase(exports, chatRoomController.post, { /* ... */ should write status header: function (test) { var data = { data: { user: cjno, message: hi } }; this.controller.post(); this.sendRequest(data); test.ok(this.res.writeHead.called); test.equals(this.res.writeHead.args[0], 201); test.done(); } }); Listing 14.22 faces the challenge and makes the actual call to writeHead. Listing 14.22 Setting the response code post: function () { /* ... */ this.request.addListener(end, function () { var data = JSON.parse(decodeURI(body)).data; this.chatRoom.addMessage(data.user, data.message); this.response.writeHead(201); }.bind(this)); } 14.2.5.2 Closing the Connection Once the headers have been written, we should make sure the connection is closed. Listing 14.23 shows the test. Listing 14.23 Expecting the response to be closed function controllerSetUp() { /* ... */ var res = this.res = { ...
Tìm kiếm theo từ khóa liên quan:
thiết kế web giáo trình css kĩ thuật cắt html flash cơ bản giáo trình photoshopTài liệu liên quan:
-
Báo cáo thực tập: Đề tài thiết kế Web
77 trang 569 2 0 -
Đề thi thực hành môn Thiết kế Web - Trường Cao đẳng nghề Vĩnh Phúc
3 trang 269 2 0 -
182 trang 177 0 0
-
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 113 0 0 -
GIÁO TRÌNH LẬP TRÌNH WEB_PHẦN 2_BÀI 3
3 trang 103 0 0 -
Giáo trình Nhập môn thiết kế website
58 trang 83 0 0 -
Tài liệu giảng dạy Thiết kế giao diện Web - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
88 trang 72 0 0 -
81 trang 69 0 0
-
112 trang 64 0 0
-
Hướng dân sử dụng Navicat để Create , Backup , Restore Database
7 trang 63 0 0