forked from jed/localhose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
68 lines (52 loc) · 1.68 KB
/
test.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
// Any file using Localhose needs to be run
// with superuser permissions, a la:
//
// sudo node ./test.js
//
// In this test, we reroute nodejs.org to our
// local server for comedic effect.
var assert = require( "assert" )
, http = require( "http" )
, localhose = require( "localhose" ).unset()
, url = require( "url" ).parse( "http://nodejs.org/" )
, sassyTitle = "rYah'S aweSOmE homEpagE!"
, server = http.createServer( handler )
server.listen( 80 )
getTitle( url, function( title ) {
console.log( "\nfetching title for " + url.host )
console.log( "- expected title: node.js" )
console.log( "- actual title: " + title )
assert.equal( title, "node.js" )
localhose.set( url.host )
getTitle( url, function( title ) {
console.log( "\nfetching title for " + url.host )
console.log( "- expected title: " + sassyTitle )
console.log( "- actual title: " + title )
assert.equal( title, sassyTitle )
localhose.unset()
getTitle( url, function( title ) {
console.log( "\nfetching title for " + url.host )
console.log( "- expected title: node.js" )
console.log( "- actual title: " + title )
assert.equal( title, "node.js" )
server.close()
})
})
})
function handler( req, res ) {
res.writeHead( 200, { "Content-Type": "text/html" } )
res.end(
"<html>" +
"<head><title>" + sassyTitle + "</title></head>" +
"<body>welcome to " + sassyTitle + "</body>" +
"</html>"
)
}
function getTitle( uri, cb ) {
var body = ""
http.get( url, function( res ) {
res
.on( "data", function( data ){ body += data } )
.on( "end", function(){ cb( ( /<title>([^<]+)/( body ) || [] )[ 1 ] ) } )
})
}