This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
chore(refactor): Pull proxy logic out and add unit tests around it. #22
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e9460ce
Fix naming.
heathkit 26f7591
Pulling the proxy part out into a separate class.
heathkit a22172e
Trying out commands as event emitters.
heathkit 49bc92c
Fix tests and formatting.
heathkit b1db171
Try out manual formatting.
heathkit f20f04b
Add unit tests for command parsing using barriers.
heathkit ba07840
Testing with nock and fake streams.
heathkit 16a7cb3
Add tests for proxy.
heathkit 99a9e0a
Clang format
heathkit d0124f3
Improve barrier test.
heathkit f3f81df
Call barriers one at a time, implement other feedback.
heathkit 81f6b06
Comment fixes.
heathkit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Trying out commands as event emitters.
- Loading branch information
commit a22172eb2b5c911690d738493733d285cb4efa31
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,72 +1,81 @@ | ||
import {parseWebDriverCommand} from './webdriverCommands'; | ||
import {parseWebDriverCommand, WebDriverCommand} from './webdriver_commands'; | ||
import * as http from 'http'; | ||
import * as url from 'url'; | ||
|
||
/** | ||
* A proxy that understands WebDriver commands. Users can add middleware (similar to middleware in | ||
* express) that will be called before | ||
* forwarding the request to WebDriver or forwarding the response to the client. | ||
*/ | ||
export class WebdriverProxy { | ||
export class WebDriverProxy { | ||
barriers: WebDriverBarrier[]; | ||
seleniumAddress: string; | ||
|
||
constructor() { | ||
constructor(seleniumAddress: string) { | ||
this.barriers = []; | ||
this.seleniumAddress = seleniumAddress; | ||
} | ||
|
||
addBarrier(barrier: WebDriverBarrier) { | ||
this.barriers.push(barrier); | ||
} | ||
|
||
addMiddleware(middleware: WebDriverMiddleware) { | ||
async requestListener(originalRequest: http.IncomingMessage, response: http.ServerResponse) { | ||
|
||
} | ||
let command = parseWebDriverCommand(originalRequest.url, originalRequest.method); | ||
|
||
requestListener(originalRequest: http.IncomingMessage, response: http.ServerResponse) { | ||
let reqData = ''; | ||
originalRequest.on('data', (d) => { | ||
reqData += d; | ||
}); | ||
originalRequest.on('end', () => { | ||
command.handleData(reqData); | ||
}); | ||
|
||
// TODO: What happens when barriers error? return a client error? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should reply to the initial response with a 500 and an error message chosen by the barrier. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe, if the barrier rejects its promise, we use that value as the error. |
||
for (let barrier of this.barriers) { | ||
await barrier.onCommand(command); | ||
} | ||
|
||
let stabilized = Promise.resolve(null); | ||
let parsedUrl = url.parse(this.seleniumAddress); | ||
let options: http.RequestOptions = {}; | ||
options.method = originalRequest.method; | ||
options.path = parsedUrl.path + originalRequest.url; | ||
options.hostname = parsedUrl.hostname; | ||
options.port = parseInt(parsedUrl.port); | ||
options.headers = originalRequest.rawHeaders; | ||
|
||
// If the command is not a proxy command, it's a regular webdriver command. | ||
if (self.shouldStabilize(originalRequest.url)) { | ||
stabilized = self.sendRequestToStabilize(originalRequest); | ||
originalRequest.on('error', (err) => { | ||
response.writeHead(500); | ||
response.end(err); | ||
}); | ||
|
||
// TODO: Log waiting for Angular. | ||
} | ||
let forwardedRequest = http.request(options, (seleniumResponse) => { | ||
response.writeHead(seleniumResponse.statusCode, seleniumResponse.headers); | ||
let respData = ''; | ||
seleniumResponse.on('data', (d) => { | ||
respData += d; | ||
response.write(d); | ||
}); | ||
seleniumResponse.on('end', () => { | ||
command.handleResponse(seleniumResponse.statusCode, respData); | ||
response.end(); | ||
}); | ||
seleniumResponse.on('error', (err) => { | ||
response.writeHead(500); | ||
response.end(err); | ||
}); | ||
}); | ||
|
||
stabilized.then( | ||
() => { | ||
let seleniumRequest = self.createSeleniumRequest( | ||
originalRequest.method, originalRequest.url, function(seleniumResponse) { | ||
response.writeHead(seleniumResponse.statusCode, seleniumResponse.headers); | ||
seleniumResponse.pipe(response); | ||
seleniumResponse.on('error', (err) => { | ||
response.writeHead(500); | ||
response.write(err); | ||
response.end(); | ||
}); | ||
}); | ||
let reqData = ''; | ||
originalRequest.on('error', (err) => { | ||
response.writeHead(500); | ||
response.write(err); | ||
response.end(); | ||
}); | ||
originalRequest.on('data', (d) => { | ||
reqData += d; | ||
seleniumRequest.write(d); | ||
}); | ||
originalRequest.on('end', () => { | ||
let command = | ||
parseWebDriverCommand(originalRequest.url, originalRequest.method, reqData); | ||
if (this.logger) { | ||
this.logger.logWebDriverCommand(command); | ||
} | ||
seleniumRequest.end(); | ||
}); | ||
}, | ||
(err) => { | ||
response.writeHead(500); | ||
response.write(err); | ||
response.end(); | ||
}); | ||
originalRequest.on('data', (d) => { | ||
forwardedRequest.write(d); | ||
}); | ||
originalRequest.on('end', () => { | ||
forwardedRequest.end(); | ||
}); | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Class-level comment for WebDriverBarrier would be nice. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
export class WebDriverMiddleware { | ||
onRequest() { | ||
} | ||
export interface WebDriverBarrier { | ||
onCommand(command: WebDriverCommand): Promise<void>; | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think http is unused here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I'll turn on the no-unused-variable lint check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, rather, TypeScript 2.1 now has --noUnusedLocals