-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): migrate Proxy to ES2015 (#3490)
We used to share single instance of this class between all scenarios, but this is not necessary as each scenario starts proxy on demand and stops it in [After hook](https://github.com/karma-runner/karma/blob/master/test/e2e/step_definitions/hooks.js#L7). It should be cleaner to have an independent instance for each scenario.
- Loading branch information
Showing
2 changed files
with
35 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,50 @@ | ||
const http = require('http') | ||
const httpProxy = require('http-proxy') | ||
|
||
function Proxy () { | ||
const self = this | ||
self.running = false | ||
module.exports = class Proxy { | ||
constructor () { | ||
this.running = false | ||
|
||
self.proxy = httpProxy.createProxyServer({ | ||
target: 'http://localhost:9876' | ||
}) | ||
this.proxy = httpProxy.createProxyServer({ | ||
target: 'http://localhost:9876' | ||
}) | ||
|
||
self.proxy.on('error', function proxyError (err, req, res) { | ||
console.log('support/proxy onerror', err) | ||
}) | ||
this.proxy.on('error', (err) => { | ||
console.log('support/proxy onerror', err) | ||
}) | ||
|
||
self.server = http.createServer(function (req, res) { | ||
const url = req.url | ||
const match = url.match(self.proxyPathRegExp) | ||
if (match) { | ||
req.url = '/' + match[1] | ||
self.proxy.web(req, res) | ||
} else { | ||
res.statusCode = 404 | ||
res.statusMessage = 'Not found' | ||
res.end() | ||
} | ||
}) | ||
this.server = http.createServer((req, res) => { | ||
const url = req.url | ||
const match = url.match(this.proxyPathRegExp) | ||
if (match) { | ||
req.url = '/' + match[1] | ||
this.proxy.web(req, res) | ||
} else { | ||
res.statusCode = 404 | ||
res.statusMessage = 'Not found' | ||
res.end() | ||
} | ||
}) | ||
|
||
self.server.on('clientError', (err, socket) => { | ||
console.log('support/proxy clientError', err) | ||
}) | ||
this.server.on('clientError', (err) => { | ||
console.log('support/proxy clientError', err) | ||
}) | ||
} | ||
|
||
self.start = function (port, proxyPath, callback) { | ||
self.proxyPathRegExp = new RegExp('^' + proxyPath + '(.*)') | ||
self.server.listen(port, function (error) { | ||
self.running = !error | ||
start (port, proxyPath, callback) { | ||
this.proxyPathRegExp = new RegExp('^' + proxyPath + '(.*)') | ||
this.server.listen(port, (error) => { | ||
this.running = !error | ||
callback(error) | ||
}) | ||
} | ||
|
||
self.stop = function (callback) { | ||
if (self.running) { | ||
self.running = false | ||
self.server.close(callback) | ||
stop (callback) { | ||
if (this.running) { | ||
this.running = false | ||
this.server.close(callback) | ||
} else { | ||
callback() | ||
} | ||
} | ||
} | ||
|
||
module.exports = new Proxy() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters