-
-
Notifications
You must be signed in to change notification settings - Fork 768
feat: use own User Agent #1779
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
Merged
Merged
feat: use own User Agent #1779
Changes from all commits
Commits
Show all changes
73 commits
Select commit
Hold shift + click to select a range
51d7c1e
feat: support directConnect
KazuCocoa 65e494f
add an argument for directConnect
KazuCocoa 05d0976
add flag in android drivers
KazuCocoa 5c3f950
add config
KazuCocoa a5c3baa
add for ios
KazuCocoa 48b42e8
add config to other drivers
KazuCocoa 55ad185
update imports, license
KazuCocoa d0ff28a
use a tag
KazuCocoa 16dd824
fix lint
KazuCocoa 0125a97
update part of
KazuCocoa 138fbd4
fix lint
KazuCocoa 03833d7
create a new client with given config and factory
KazuCocoa 2a89299
add constructor
KazuCocoa 97e8b62
refer to this
KazuCocoa 821c89d
update ios
KazuCocoa 08a4d01
upadte others as well
KazuCocoa 0afaf50
refer to super
KazuCocoa e6ea969
raise an exception
KazuCocoa 9ee52cb
keep clientconfig as part of appiumclientconfig
KazuCocoa 11c54b5
tweak
KazuCocoa e7a751a
tweak
KazuCocoa 790857d
modify address
KazuCocoa fc96519
fix typo
KazuCocoa 1751304
tweak base url
KazuCocoa 160cf8e
fix the default local service
KazuCocoa 8862d75
do not use orElse
KazuCocoa d923503
fixlint
KazuCocoa 139161b
update null case
KazuCocoa 3b576aa
tweak a bit
KazuCocoa 5358125
Merge branch 'master' into direct-connect
KazuCocoa ca93f6e
update to extend client config
KazuCocoa bd1f78c
follows client config
KazuCocoa bab6830
tweak docstring
KazuCocoa 8d9b785
rever some unnecessary changes
KazuCocoa 839df21
run tests in internal as unitTests, add user agent definition
KazuCocoa ec63fb1
add tests, tweak some lines
KazuCocoa 0460169
add a key
KazuCocoa 6db723c
add docstring
KazuCocoa c2676ee
remove unnecessary argument
KazuCocoa 7326811
chore: add comments
KazuCocoa 397f596
chore: tweak
KazuCocoa 9cc5e4a
chore: add UA in a new session request
KazuCocoa f2dc84a
fix: lint
KazuCocoa c00697d
chore: remove done todo
KazuCocoa b1a9413
Merge branch 'master' into direct-connect
KazuCocoa 9ab8547
remove user agent stuff
KazuCocoa e455f52
remove ua stuff more
KazuCocoa f6570d2
remove UA stuff
KazuCocoa 69ff238
collect similar process into one method
KazuCocoa 7b2ff2a
tweak
KazuCocoa bd0395b
add a class object
KazuCocoa 0a11b8a
Merge branch 'direct-connect' of github.com:KazuCocoa/java-client int…
KazuCocoa bfdd997
tweak
KazuCocoa 6d3d3ec
remove a few lines
KazuCocoa a834d3b
Revert "remove user agent stuff"
KazuCocoa 3a6aa98
Revert "remove ua stuff more"
KazuCocoa a3a0367
Revert "remove UA stuff"
KazuCocoa b26ab69
Merge branch 'master' into useragent
KazuCocoa 70889d0
rename
KazuCocoa 88258d6
fix conflict
KazuCocoa 778d5b6
refer to com.google.common.net.HttpHeaders
KazuCocoa 73c2a85
tweak the logic to include UA
KazuCocoa 1d38839
use parameterized test
KazuCocoa 1b49768
use parameterized more, defined const
KazuCocoa f0809f5
fix lint
KazuCocoa f457154
Merge branch 'master' into useragent
KazuCocoa 8b548ca
modify the logic to customize the UA, add tests
KazuCocoa 71c15ed
Merge branch 'useragent' of github.com:KazuCocoa/java-client into use…
KazuCocoa 5ad80f8
match as case insensitive
KazuCocoa 128e583
tweak, fixed addHeader not to add headers twice
KazuCocoa 405a5ec
add annotation
KazuCocoa 407bc18
define a method to build UA
KazuCocoa e95af13
Merge branch 'master' into useragent
KazuCocoa 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
91 changes: 91 additions & 0 deletions
91
src/main/java/io/appium/java_client/AppiumUserAgentFilter.java
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,91 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.appium.java_client; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.net.HttpHeaders; | ||
import io.appium.java_client.internal.Config; | ||
import org.openqa.selenium.remote.http.AddSeleniumUserAgent; | ||
import org.openqa.selenium.remote.http.Filter; | ||
import org.openqa.selenium.remote.http.HttpHandler; | ||
|
||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Manage Appium Client configurations. | ||
*/ | ||
|
||
public class AppiumUserAgentFilter implements Filter { | ||
|
||
public static final String VERSION_KEY = "appiumClient.version"; | ||
|
||
private static final String USER_AGENT_PREFIX = "appium/"; | ||
|
||
/** | ||
* A default User Agent name for Appium Java client. | ||
* e.g. appium/8.2.0 (selenium/4.5.0 (java mac)) | ||
*/ | ||
public static final String USER_AGENT = buildUserAgentHeaderValue(AddSeleniumUserAgent.USER_AGENT); | ||
|
||
private static String buildUserAgentHeaderValue(@Nonnull String previousUA) { | ||
return String.format("%s%s (%s)", | ||
USER_AGENT_PREFIX, Config.main().getValue(VERSION_KEY, String.class), previousUA); | ||
} | ||
|
||
/** | ||
* Returns true if the given User Agent includes "appium/", which | ||
* implies the User Agent already has the Appium UA by this method. | ||
* The matching is case-insensitive. | ||
* @param userAgent the User Agent in the request headers. | ||
* @return whether the given User Agent includes Appium UA | ||
* like by this filter. | ||
*/ | ||
@VisibleForTesting | ||
public static boolean containsAppiumName(@Nullable String userAgent) { | ||
KazuCocoa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return userAgent != null && userAgent.toLowerCase().contains(USER_AGENT_PREFIX.toLowerCase()); | ||
} | ||
|
||
/** | ||
* Returns the User Agent. If the given UA already has | ||
* {@link USER_AGENT_PREFIX}, it returns the UA. | ||
* IF the given UA does not have {@link USER_AGENT_PREFIX}, | ||
* it returns UA with the Appium prefix. | ||
* @param userAgent the User Agent in the request headers. | ||
* @return the User Agent for the request | ||
*/ | ||
public static String buildUserAgent(@Nullable String userAgent) { | ||
if (userAgent == null) { | ||
mykola-mokhnach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return USER_AGENT; | ||
} | ||
|
||
if (containsAppiumName(userAgent)) { | ||
return userAgent; | ||
} | ||
|
||
return buildUserAgentHeaderValue(userAgent); | ||
} | ||
|
||
@Override | ||
public HttpHandler apply(HttpHandler next) { | ||
|
||
return req -> { | ||
req.setHeader(HttpHeaders.USER_AGENT, buildUserAgent(req.getHeader(HttpHeaders.USER_AGENT))); | ||
return next.execute(req); | ||
}; | ||
} | ||
} |
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 +1,2 @@ | ||
selenium.version=@selenium.version@ | ||
appiumClient.version=@appiumClient.version@ |
67 changes: 67 additions & 0 deletions
67
src/test/java/io/appium/java_client/internal/AppiumUserAgentFilterTest.java
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,67 @@ | ||
package io.appium.java_client.internal; | ||
|
||
import io.appium.java_client.AppiumUserAgentFilter; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class AppiumUserAgentFilterTest { | ||
@Test | ||
void validateUserAgent() { | ||
assertTrue(AppiumUserAgentFilter.USER_AGENT.startsWith("appium/")); | ||
} | ||
|
||
public static Stream<Arguments> userAgentParams() { | ||
return Stream.of( | ||
Arguments.of("selenium/4.5.0 (java mac)", false), | ||
Arguments.of("appium/8.2.0 (selenium/4.5.0 (java mac))", true), | ||
Arguments.of("APPIUM/8.2.0 (selenium/4.5.0 (java mac))", true), | ||
Arguments.of("something (Appium/8.2.0 (selenium/4.5.0 (java mac)))", true), | ||
Arguments.of("something (appium/8.2.0 (selenium/4.5.0 (java mac)))", true) | ||
); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("userAgentParams") | ||
void validUserAgentIfContainsAppiumName(String userAgent, boolean expected) { | ||
assertEquals(AppiumUserAgentFilter.containsAppiumName(userAgent), expected); | ||
} | ||
|
||
@Test | ||
void validBuildUserAgentNoUA() { | ||
assertEquals(AppiumUserAgentFilter.buildUserAgent(null), AppiumUserAgentFilter.USER_AGENT); | ||
} | ||
|
||
@Test | ||
void validBuildUserAgentNoAppium1() { | ||
String ua = AppiumUserAgentFilter.buildUserAgent("selenium/4.5.0 (java mac)"); | ||
assertTrue(ua.startsWith("appium/")); | ||
assertTrue(ua.endsWith("selenium/4.5.0 (java mac))")); | ||
} | ||
|
||
@Test | ||
void validBuildUserAgentNoAppium2() { | ||
String ua = AppiumUserAgentFilter.buildUserAgent("customSelenium/4.5.0 (java mac)"); | ||
assertTrue(ua.startsWith("appium/")); | ||
assertTrue(ua.endsWith("customSelenium/4.5.0 (java mac))")); | ||
} | ||
|
||
@Test | ||
void validBuildUserAgentAlreadyHasAppium1() { | ||
// Won't modify since the UA already has appium prefix | ||
String ua = AppiumUserAgentFilter.buildUserAgent("appium/8.1.0 (selenium/4.5.0 (java mac))"); | ||
assertEquals("appium/8.1.0 (selenium/4.5.0 (java mac))", ua); | ||
} | ||
|
||
@Test | ||
void validBuildUserAgentAlreadyHasAppium2() { | ||
// Won't modify since the UA already has appium prefix | ||
String ua = AppiumUserAgentFilter.buildUserAgent("something (appium/8.1.0 (selenium/4.5.0 (java mac)))"); | ||
assertEquals("something (appium/8.1.0 (selenium/4.5.0 (java mac)))", ua); | ||
} | ||
} |
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
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.
@SrinivasanTarget Do you usually bump this version manually upon releasing the package or there is some script for that? Shall we sync more java client release scripts/tutorials about this change?
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.
@mykola-mokhnach I do this manually. maybe its good to come up with a script or leaving it to dependabot can also be a good thing to do when there is a release.
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.
would you have some time to document all steps of the release procedure?
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.
Yes I will do it in the following days.