├── .gitignore
├── webroot
├── test.js
└── test.html
├── grunt.js
├── README.md
├── package.json
├── LICENSE-MIT
└── lib
└── tutorial.js
/.gitignore:
--------------------------------------------------------------------------------
1 | /node_modules/
2 |
--------------------------------------------------------------------------------
/webroot/test.js:
--------------------------------------------------------------------------------
1 | // A little bit of jQuery.
2 | $(function() {
3 | $("#test").text("This text was added to the DOM via jQuery.");
4 | });
5 |
--------------------------------------------------------------------------------
/webroot/test.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Test Page
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/grunt.js:
--------------------------------------------------------------------------------
1 | /*global config:true, task:true*/
2 | config.init({
3 | pkg: '',
4 | lint: {
5 | files: ['grunt.js', 'lib/**/*.js']
6 | },
7 | watch: {
8 | files: '',
9 | tasks: 'default'
10 | },
11 | jshint: {
12 | options: {
13 | curly: true,
14 | eqeqeq: true,
15 | immed: true,
16 | latedef: true,
17 | newcap: true,
18 | noarg: true,
19 | sub: true,
20 | undef: true,
21 | boss: true,
22 | eqnull: true,
23 | node: true,
24 | es5: true
25 | },
26 | globals: {}
27 | }
28 | });
29 |
30 | // Default task.
31 | task.registerTask('default', 'lint');
32 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # zombie <-> connect, without a physical port.
2 |
3 | Sample code showing how to get zombie.js to talk to connect via socket file.
4 |
5 | _(Which is necessary because relative URLs don't seem to work in zombie when file:// is used. Plus, file:// sucks.)_
6 |
7 | Tested in Node.js 0.6.8 on OS X.
8 |
9 | ## Take a look
10 |
11 | View [the JavaScript source](https://github.com/cowboy/node-zombie-connect-socket/blob/master/lib/tutorial.js) to see what I did.
12 |
13 | ## Try it out
14 | This isn't in npm.
15 |
16 | ```bash
17 | git clone git@github.com:cowboy/node-zombie-connect-socket.git
18 | cd node-zombie-connect-socket
19 | npm install
20 | node lib/tutorial.js
21 | ```
22 |
23 | ## FWIW
24 | I used this technique in grunt's [qunit task](https://github.com/cowboy/grunt/blob/master/tasks/qunit.js).
25 |
26 | ## License
27 | Copyright (c) 2012 "Cowboy" Ben Alman
28 | Licensed under the MIT license.
29 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "node-zombie-connect-socket",
3 | "description": "zombie <-> connect, without a physical port.",
4 | "version": "0.1.0",
5 | "homepage": "https://github.com/cowboy/node-zombie-connect-socket",
6 | "author": {
7 | "name": "\"Cowboy\" Ben Alman",
8 | "url": "http://benalman.com/"
9 | },
10 | "repository": {
11 | "type": "git",
12 | "url": "git@github.com:cowboy/node-zombie-connect-socket.git"
13 | },
14 | "bugs": {
15 | "url": "https://github.com/cowboy/node-zombie-connect-socket/issues"
16 | },
17 | "licenses": [
18 | {
19 | "type": "MIT",
20 | "url": "/blob/master/LICENSE-MIT"
21 | }
22 | ],
23 | "dependencies": {
24 | "connect": "~1.8.5",
25 | "hooker": "~0.2.3",
26 | "temporary": "~0.0.2",
27 | "zombie": "~0.12.13"
28 | },
29 | "devDependencies": {
30 | "grunt": "~0.2.9"
31 | },
32 | "keywords": [],
33 | "engines": {
34 | "node": ">= 0.6.0"
35 | },
36 | "main": "lib/tutorial"
37 | }
--------------------------------------------------------------------------------
/LICENSE-MIT:
--------------------------------------------------------------------------------
1 | Copyright (c) 2012 "Cowboy" Ben Alman
2 |
3 | Permission is hereby granted, free of charge, to any person
4 | obtaining a copy of this software and associated documentation
5 | files (the "Software"), to deal in the Software without
6 | restriction, including without limitation the rights to use,
7 | copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | copies of the Software, and to permit persons to whom the
9 | Software is furnished to do so, subject to the following
10 | conditions:
11 |
12 | The above copyright notice and this permission notice shall be
13 | included in all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 | OTHER DEALINGS IN THE SOFTWARE.
23 |
--------------------------------------------------------------------------------
/lib/tutorial.js:
--------------------------------------------------------------------------------
1 | /*
2 | * node-zombie-connect-socket
3 | * https://github.com/cowboy/node-zombie-connect-socket
4 | *
5 | * Copyright (c) 2012 "Cowboy" Ben Alman
6 | * Licensed under the MIT license.
7 | */
8 |
9 | // Built-in libs.
10 | var HTTP = require('http');
11 | var path = require('path');
12 |
13 | // My library for hooking methods of objects.
14 | var hooker = require('hooker');
15 | // Super-simple web server lib.
16 | var connect = require('connect');
17 | // Our headless browser of choice.
18 | var zombie = require('zombie');
19 |
20 | // Create a temp file to be used for the socket.
21 | var Tempfile = require('temporary/lib/file');
22 | var tempfile = new Tempfile();
23 |
24 | // Create simple web server.
25 | var server = connect(
26 | // Log incoming requests (for debugging).
27 | connect.logger(),
28 | // Serve static files from webroot directory.
29 | connect.static(path.join(__dirname, '../webroot'))
30 | );
31 |
32 | // Use socket file for webserver instead of a physical port like 9001.
33 | server.listen(tempfile.path);
34 |
35 | // Hook HTTP.request to use socket file for all http://cowboy/* requests,
36 | // leaving all other external files alone.
37 | hooker.hook(HTTP, 'request', function(options) {
38 | if (options.host === 'cowboy') {
39 | options.socketPath = tempfile.path;
40 | }
41 | });
42 |
43 | // Instead of a real physical host + port like 'http://localhost:9001/', use
44 | // made-up host for all local files (external files are requested normally).
45 | var url = 'http://cowboy/test.html';
46 |
47 | // Start zombie, requesting url through the "cowboy" host, which gets mapped
48 | // to the socket file, which our connect server is listening to.
49 | zombie.visit(url, {debug: true}, function(e, browser) {
50 | // Timeout after 10 seconds of nothing.
51 | browser.wait(10000, function(err, browser) {
52 | // Inspect the zombie browser's DOM.
53 | var document = browser.document;
54 | var elem = document.querySelector('#test');
55 | // This should log something!
56 | console.log('>>>', elem.textContent, '<<<');
57 |
58 | // Clean up.
59 | hooker.unhook(HTTP, 'request');
60 | tempfile.unlink();
61 | server.close();
62 |
63 | // Exit.
64 | console.log('All done!');
65 | process.exit(0);
66 | });
67 | });
68 |
--------------------------------------------------------------------------------