@@ -7,6 +7,7 @@ var Log = require('./logger'),
7
7
qs = require ( 'querystring' ) ,
8
8
utils = require ( './utils' ) ,
9
9
config = require ( '../lib/config' ) ,
10
+ proxyServer = require ( './proxy' ) . proxyServer ,
10
11
chalk = require ( 'chalk' ) ;
11
12
12
13
var mimeTypes = {
@@ -30,7 +31,7 @@ function getTestBrowserInfo(worker) {
30
31
31
32
exports . Server = function Server ( bsClient , workers ) {
32
33
33
- function handleFile ( filename , request , response ) {
34
+ function handleFile ( filename , request , response , doNotUseProxy ) {
34
35
var url_parts = url . parse ( request . url , true ) ;
35
36
var query = url_parts . query ;
36
37
@@ -40,95 +41,122 @@ exports.Server = function Server(bsClient, workers) {
40
41
logger . debug ( '[%s] Acknowledged' , getTestBrowserInfo ( worker ) ) ;
41
42
}
42
43
43
- fs . exists ( filename , function ( exists ) {
44
- if ( ! exists ) {
45
- response . writeHead ( 404 , {
46
- 'Content-Type' : 'text/plain'
47
- } ) ;
48
- response . write ( '404 Not Found\n' ) ;
49
- response . end ( ) ;
50
- return ;
51
- }
44
+ var getReporterPatch = function ( mimeType ) {
45
+ var scripts = [
46
+ 'json2.js' ,
47
+ 'browserstack.js' ,
48
+ ] ;
49
+
50
+ var framework_scripts = {
51
+ 'qunit' : [ 'qunit-plugin.js' ] ,
52
+ 'jasmine' : [ 'jasmine-jsreporter.js' , 'jasmine-plugin.js' ] ,
53
+ 'jasmine2' : [ 'jasmine2-plugin.js' ] ,
54
+ 'mocha' : [ 'mocha-plugin.js' ]
55
+ } ;
52
56
53
- if ( fs . lstatSync ( filename ) . isDirectory ( ) ) {
54
- filename = filename + ( filename . lastIndexOf ( '/' ) === filename . length - 1 ? '' : '/' ) + 'index.html' ;
57
+ var filePath = path . relative ( process . cwd ( ) , filename ) ;
58
+ var pathMatches ;
59
+
60
+ if ( typeof config . test_path === 'object' ) {
61
+ pathMatches = ( config . test_path . indexOf ( filePath ) !== - 1 ) ;
62
+ } else {
63
+ pathMatches = ( filePath === config . test_path ) ;
55
64
}
56
65
57
- fs . readFile ( filename , { encoding : 'utf8' } , function ( err , file ) {
66
+ if ( pathMatches && mimeType === 'text/html' ) {
67
+ var framework = config [ 'test_framework' ] ;
68
+ var tag_name = ( framework === 'mocha' ) ? 'head' : 'body' ;
69
+ var patch = '$1' ;
58
70
59
- if ( err ) {
60
- response . writeHead ( 500 , {
61
- 'Content-Type' : 'text/plain'
71
+ scripts . forEach ( function ( script ) {
72
+ patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
73
+ } ) ;
74
+
75
+ // adding framework scripts
76
+ if ( framework === 'jasmine' ) {
77
+ framework_scripts [ 'jasmine' ] . forEach ( function ( script ) {
78
+ patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
79
+ } ) ;
80
+ patch += '<script type="text/javascript">jasmine.getEnv().addReporter(new jasmine.JSReporter());</script>\n' ;
81
+ } else if ( framework === 'jasmine2' ) {
82
+ framework_scripts [ 'jasmine2' ] . forEach ( function ( script ) {
83
+ patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
84
+ } ) ;
85
+ } else if ( framework === 'mocha' ) {
86
+ framework_scripts [ 'mocha' ] . forEach ( function ( script ) {
87
+ patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
88
+ } ) ;
89
+ patch += '<script type="text/javascript">mocha.reporter(Mocha.BrowserStack);</script>\n' ;
90
+ } else {
91
+ framework_scripts [ 'qunit' ] . forEach ( function ( script ) {
92
+ patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
62
93
} ) ;
63
- response . write ( err + '\n' ) ;
64
- response . end ( ) ;
65
- return ;
66
94
}
95
+ patch += '</' + tag_name + '>' ;
96
+ return patch ;
97
+ }
98
+ } ;
67
99
68
- var mimeType = mimeTypes [ path . extname ( filename ) . split ( '.' ) [ 1 ] ] ;
69
- response . writeHead ( 200 , {
70
- 'Content-Type' : mimeType + '; charset=utf-8' ,
71
- } ) ;
100
+ var writeResponse = function ( err , data ) {
72
101
73
- var scripts = [
74
- 'json2.js' ,
75
- 'browserstack.js' ,
76
- ] ;
102
+ if ( err ) {
103
+ response . writeHead ( 500 , {
104
+ 'Content-Type' : 'text/plain'
105
+ } ) ;
106
+ response . write ( err + '\n' ) ;
107
+ response . end ( ) ;
108
+ return ;
109
+ }
77
110
78
- var framework_scripts = {
79
- 'qunit' : [ 'qunit-plugin.js' ] ,
80
- 'jasmine' : [ 'jasmine-jsreporter.js' , 'jasmine-plugin.js' ] ,
81
- 'jasmine2' : [ 'jasmine2-plugin.js' ] ,
82
- 'mocha' : [ 'mocha-plugin.js' ]
83
- } ;
111
+ var mimeType = mimeTypes [ path . extname ( filename ) . split ( '.' ) [ 1 ] ] ;
112
+ response . writeHead ( 200 , {
113
+ 'Content-Type' : mimeType + '; charset=utf-8' ,
114
+ } ) ;
115
+ var tag_name = ( config [ 'test_framework' ] === 'mocha' ) ? 'head' : 'body' ;
116
+ var matcher = new RegExp ( '(.*)<\/' + tag_name + '>' ) ; ///(.*)<\/body>/;
117
+ var patch = getReporterPatch ( mimeType ) ;
118
+ data = data . replace ( matcher , patch ) ;
84
119
85
- var filePath = path . relative ( process . cwd ( ) , filename ) ;
86
- var pathMatches ;
120
+ response . write ( data ) ;
121
+ response . end ( ) ;
122
+ } ;
87
123
88
- if ( typeof config . test_path === 'object' ) {
89
- pathMatches = ( config . test_path . indexOf ( filePath ) !== - 1 ) ;
90
- } else {
91
- pathMatches = ( filePath === config . test_path ) ;
124
+ if ( ! doNotUseProxy && config . test_server ) {
125
+ proxyServer . onRequest ( request , response , config . test_server , function ( remote_response , response_data ) {
126
+ var mimeType = mimeTypes [ path . extname ( filename ) . split ( '.' ) [ 1 ] ] ;
127
+ var final_data = response_data ;
128
+ var headers = remote_response . headers ;
129
+ if ( mimeType === 'text/html' ) {
130
+ var matcher = / ( .* ) < \/ h e a d > / ;
131
+ var patch = getReporterPatch ( mimeType ) ;
132
+ final_data = response_data . replace ( matcher , patch ) ;
133
+ headers [ 'content-length' ] = final_data . length ;
92
134
}
93
- if ( pathMatches && mimeType === 'text/html' ) {
94
- var framework = config [ 'test_framework' ] ;
95
- var tag_name = ( framework === 'mocha' ) ? 'head' : 'body' ;
96
- var matcher = new RegExp ( '(.*)<\/' + tag_name + '>' ) ; ///(.*)<\/body>/;
97
- var patch = '$1' ;
98
- scripts . forEach ( function ( script ) {
99
- patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
100
- } ) ;
135
+ response . writeHead ( remote_response . statusCode , headers ) ;
136
+ response . write ( final_data ) ;
137
+ response . end ( ) ;
138
+ return ;
139
+ } ) ;
101
140
102
- // adding framework scripts
103
- if ( framework === 'jasmine' ) {
104
- framework_scripts [ 'jasmine' ] . forEach ( function ( script ) {
105
- patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
106
- } ) ;
107
- patch += '<script type="text/javascript">jasmine.getEnv().addReporter(new jasmine.JSReporter());</script>\n' ;
108
- } else if ( framework === 'jasmine2' ) {
109
- framework_scripts [ 'jasmine2' ] . forEach ( function ( script ) {
110
- patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
111
- } ) ;
112
- } else if ( framework === 'mocha' ) {
113
- framework_scripts [ 'mocha' ] . forEach ( function ( script ) {
114
- patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
115
- } ) ;
116
- patch += '<script type="text/javascript">mocha.reporter(Mocha.BrowserStack);</script>\n' ;
117
- } else {
118
- framework_scripts [ 'qunit' ] . forEach ( function ( script ) {
119
- patch += '<script type="text/javascript" src="/_patch/' + script + '"></script>\n' ;
120
- } ) ;
121
- }
122
- patch += '</' + tag_name + '>' ;
141
+ } else {
123
142
124
- file = file . replace ( matcher , patch ) ;
143
+ fs . exists ( filename , function ( exists ) {
144
+ if ( ! exists ) {
145
+ response . writeHead ( 404 , {
146
+ 'Content-Type' : 'text/plain'
147
+ } ) ;
148
+ response . write ( '404 Not Found\n' ) ;
149
+ response . end ( ) ;
150
+ return ;
125
151
}
126
152
153
+ if ( fs . lstatSync ( filename ) . isDirectory ( ) ) {
154
+ filename = filename + ( filename . lastIndexOf ( '/' ) === filename . length - 1 ? '' : '/' ) + 'index.html' ;
155
+ }
127
156
128
- response . write ( file ) ;
129
- response . end ( ) ;
157
+ fs . readFile ( filename , { encoding : 'utf8' } , writeResponse ) ;
130
158
} ) ;
131
- } ) ;
159
+ }
132
160
}
133
161
134
162
function parseBody ( body ) {
@@ -267,7 +295,7 @@ exports.Server = function Server(bsClient, workers) {
267
295
response . end ( ) ;
268
296
} ,
269
297
'_patch' : function patchHandler ( uri , body , request , response ) {
270
- handleFile ( path . join ( __dirname , uri ) , request , response ) ;
298
+ handleFile ( path . join ( __dirname , uri ) , request , response , true ) ;
271
299
} ,
272
300
'_default' : function defaultHandler ( uri , body , request , response ) {
273
301
handleFile ( path . join ( process . cwd ( ) , uri ) , request , response ) ;
0 commit comments