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 all commits
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
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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import * as http from 'http'; | ||
import * as url from 'url'; | ||
|
||
import {parseWebDriverCommand, WebDriverCommand} from './webdriver_commands'; | ||
|
||
/** | ||
* A proxy that understands WebDriver commands. Users can add barriers (similar to middleware in | ||
* express) that will be called before forwarding the request to WebDriver. The proxy will wait for | ||
* each barrier to finish, calling them in the order in which they were added. | ||
*/ | ||
export class WebDriverProxy { | ||
barriers: WebDriverBarrier[]; | ||
seleniumAddress: string; | ||
|
||
constructor(seleniumAddress: string) { | ||
this.barriers = []; | ||
this.seleniumAddress = seleniumAddress; | ||
} | ||
|
||
addBarrier(barrier: WebDriverBarrier) { | ||
this.barriers.push(barrier); | ||
} | ||
|
||
async handleRequest(originalRequest: http.IncomingMessage, response: http.ServerResponse) { | ||
let command = parseWebDriverCommand(originalRequest.url, originalRequest.method); | ||
|
||
let replyWithError = (err) => { | ||
response.writeHead(500); | ||
response.write(err.toString()); | ||
response.end(); | ||
}; | ||
|
||
// Process barriers in order, one at a time. | ||
try { | ||
for (let barrier of this.barriers) { | ||
await barrier.onCommand(command); | ||
} | ||
} catch (err) { | ||
replyWithError(err); | ||
// Don't call through if a barrier fails. | ||
return; | ||
} | ||
|
||
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.headers; | ||
|
||
let forwardedRequest = http.request(options); | ||
|
||
// clang-format off | ||
let reqData = ''; | ||
originalRequest.on('data', (d) => { | ||
reqData += d; | ||
forwardedRequest.write(d); | ||
}).on('end', () => { | ||
command.handleData(reqData); | ||
forwardedRequest.end(); | ||
}).on('error', replyWithError); | ||
|
||
forwardedRequest.on('response', (seleniumResponse) => { | ||
response.writeHead(seleniumResponse.statusCode, seleniumResponse.headers); | ||
|
||
let respData = ''; | ||
seleniumResponse.on('data', (d) => { | ||
respData += d; | ||
response.write(d); | ||
}).on('end', () => { | ||
command.handleResponse(seleniumResponse.statusCode, respData); | ||
response.end(); | ||
}).on('error', replyWithError); | ||
|
||
}).on('error', replyWithError); | ||
// clang-format on | ||
} | ||
} | ||
|
||
/** | ||
* When the proxy receives a WebDriver command, it will call onCommand() for each of it's barriers. | ||
* Barriers may return a promise for the proxy to wait for before proceeding. If the promise is | ||
* rejected, the proxy will reply with an error code and the result of the promise and the command | ||
* will not be forwarded to Selenium. | ||
*/ | ||
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
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.
Class-level comment for WebDriverBarrier would be nice.
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.
Done