New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #784 Enable BasicAuth through Selenide proxy server #785
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.codeborne.selenide; | ||
|
||
/** | ||
* Authentication schemes. | ||
* | ||
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#Authentication_schemes">Web HTTP reference</a> | ||
*/ | ||
public enum AuthenticationType { | ||
BASIC("Basic"), | ||
BEARER("Bearer"), | ||
DIGEST("Digest"), | ||
HOBA("HOBA"), | ||
MUTUAL("Mutual"), | ||
AWS4_HMAC_SHA256("AWS4-HMAC-SHA256"); | ||
|
||
private String value; | ||
|
||
AuthenticationType(final String value) { | ||
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 would remove |
||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.codeborne.selenide; | ||
|
||
import java.util.Base64; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
public final class Credentials { | ||
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 would move this class to package 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. @asolntsev 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. Yes, one parameter 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. Or we can just overload method and give ability to do open with |
||
private String login; | ||
private String password; | ||
|
||
public Credentials(final String login, final String password) { | ||
this.login = login; | ||
this.password = password; | ||
} | ||
|
||
/** | ||
* The resulting string is base64 encoded (YWxhZGRpbjpvcGVuc2VzYW1l). | ||
* | ||
* @return encoded string | ||
*/ | ||
public String encode() { | ||
final byte[] credentialsBytes = combine().getBytes(UTF_8); | ||
return Base64.getEncoder().encodeToString(credentialsBytes); | ||
} | ||
|
||
/** | ||
* Combine credentials with a colon (login:password). | ||
* | ||
* @return combined credentials | ||
*/ | ||
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. Do we really need javadoc for private methods? 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. @asolntsev 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. Let's make it public when it's really needed. Currently it's not used, so there is no reason to make it public. 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. Agree. Just remove javadoc since it's specially private method. |
||
private String combine() { | ||
return String.format("%s:%s", login, password); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,6 +101,28 @@ public static void open(String relativeOrAbsoluteUrl, String domain, String logi | |
mockModalDialogs(); | ||
} | ||
|
||
/** | ||
* | ||
* The main starting point in your tests. | ||
* <p> | ||
* Open browser and pass authentication using build-in proxy. | ||
* <p> | ||
* A common authenticationType is "Basic". See Web HTTP reference for other types. | ||
* <p> | ||
* {@code Configuration.fileDownload == Configuration.FileDownloadMode.PROXY;} - is required configuration. | ||
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 would rephrase it to something like "this method can only work if 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. @asolntsev Understood. Thanks! |
||
* | ||
* @param relativeOrAbsoluteUrl | ||
* @param authenticationType | ||
* @param login | ||
* @param password | ||
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 would remove all useless lines from javadoc, like 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. @asolntsev 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 agree, we can cleanup javadoc later. |
||
* | ||
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Proxy-Authorization">Web HTTP reference</a> | ||
* @see AuthenticationType | ||
*/ | ||
public static void open(String relativeOrAbsoluteUrl, AuthenticationType authenticationType, String login, String password) { | ||
navigator.open(relativeOrAbsoluteUrl, authenticationType, login, password); | ||
} | ||
|
||
/** | ||
* @see Selenide#open(URL, String, String, String) | ||
*/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
package com.codeborne.selenide.impl; | ||
|
||
import com.codeborne.selenide.AuthenticationType; | ||
import com.codeborne.selenide.Credentials; | ||
import com.codeborne.selenide.logevents.SelenideLog; | ||
import com.codeborne.selenide.logevents.SelenideLogger; | ||
import io.netty.handler.codec.http.HttpHeaders; | ||
import org.openqa.selenium.JavascriptExecutor; | ||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.WebDriverException; | ||
|
@@ -12,6 +15,7 @@ | |
import static com.codeborne.selenide.Configuration.baseUrl; | ||
import static com.codeborne.selenide.Configuration.captureJavascriptErrors; | ||
import static com.codeborne.selenide.WebDriverRunner.getAndCheckWebDriver; | ||
import static com.codeborne.selenide.WebDriverRunner.getSelenideProxy; | ||
import static com.codeborne.selenide.WebDriverRunner.getWebDriver; | ||
import static com.codeborne.selenide.WebDriverRunner.isIE; | ||
import static com.codeborne.selenide.logevents.LogEvent.EventStatus.PASS; | ||
|
@@ -39,6 +43,19 @@ public void open(URL url, String domain, String login, String password) { | |
navigateToAbsoluteUrl(url.toExternalForm()); | ||
} | ||
|
||
public void open(String relativeOrAbsoluteUrl, AuthenticationType authenticationType, String login, String password) { | ||
getAndCheckWebDriver(); | ||
Credentials credentials = new Credentials(login, password); | ||
String authorization = String.format("%s %s", authenticationType.getValue(), credentials.encode()); | ||
getSelenideProxy().addRequestFilter("headers.request", (request, contents, messageInfo) -> { | ||
final HttpHeaders headers = request.headers(); | ||
headers.add("Authorization", authorization); | ||
headers.add("Proxy-Authorization", authorization); | ||
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. @rosolko Isn't it a problem that we add this filter, but never remove? 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. @rosolko I found that BrowserMobProxy already has built-in AutoBasicAuth filter. It's already enabled by default. So, this method can be simplified:
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.
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.
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.
|
||
return null; | ||
}); | ||
open(relativeOrAbsoluteUrl); | ||
} | ||
|
||
protected String absoluteUrl(String relativeUrl) { | ||
return baseUrl + relativeUrl; | ||
} | ||
|
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 would add
private final
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.
@asolntsev Good catch!