Skip to content

Commit 72cb889

Browse files
authored
Move VERSION to okhttp3.OkHttp.VERSION (#6059)
It was previously okhttp3.VERSION, which is awkward because either you import the symbol (and then it's unclear whose VERSION you're talking about) or your fully qualify it (and that's unusual on its own when there are no conflicts.
1 parent 198900d commit 72cb889

7 files changed

Lines changed: 34 additions & 35 deletions

File tree

okhttp/src/main/java-templates/okhttp3/OkHttp.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,23 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
@file:JvmName("OkHttp")
1716
package okhttp3
1817

19-
const val VERSION = "$projectVersion"
18+
object OkHttp {
19+
/**
20+
* This is a string like "4.5.0-RC1", "4.5.0", or "4.6.0-SNAPSHOT" indicating the version of
21+
* OkHttp in the current runtime. Use this to include the OkHttp version in custom `User-Agent`
22+
* headers.
23+
*
24+
* Official OkHttp releases follow [semantic versioning][semver]. Versions with the `-SNAPSHOT`
25+
* qualifier are not unique and should only be used in development environments. If you create
26+
* custom builds of OkHttp please include a qualifier your version name, like "4.7.0-mycompany.3".
27+
* The version string is configured in the root project's `build.gradle`.
28+
*
29+
* Note that OkHttp's runtime version may be different from the version specified in your
30+
* project's build file due to the dependency resolution features of your build tool.
31+
*
32+
* [semver]: https://semver.org
33+
*/
34+
const val VERSION = "$projectVersion"
35+
}

okhttp/src/main/kotlin/okhttp3/internal/Util.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import okhttp3.EventListener
4444
import okhttp3.Headers
4545
import okhttp3.Headers.Companion.headersOf
4646
import okhttp3.HttpUrl
47+
import okhttp3.OkHttp
4748
import okhttp3.OkHttpClient
4849
import okhttp3.RequestBody.Companion.toRequestBody
4950
import okhttp3.Response
@@ -625,3 +626,5 @@ inline fun <T> Iterable<T>.filterList(predicate: T.() -> Boolean): List<T> {
625626
}
626627
return result
627628
}
629+
630+
const val userAgent = "okhttp/${OkHttp.VERSION}"

okhttp/src/main/kotlin/okhttp3/internal/Version.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

okhttp/src/test/java/okhttp3/AutobahnTester.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
import java.util.concurrent.TimeUnit;
2121
import java.util.concurrent.atomic.AtomicLong;
2222
import java.util.concurrent.atomic.AtomicReference;
23-
import okhttp3.internal.Version;
2423
import okio.ByteString;
2524

25+
import static okhttp3.internal.Util.userAgent;
26+
2627
/**
2728
* Exercises the web socket implementation against the <a
2829
* href="http://autobahn.ws/testsuite/">Autobahn Testsuite</a>.
@@ -131,7 +132,7 @@ private long getTestCount() throws IOException {
131132

132133
private void updateReports() {
133134
final CountDownLatch latch = new CountDownLatch(1);
134-
newWebSocket("/updateReports?agent=" + Version.userAgent, new WebSocketListener() {
135+
newWebSocket("/updateReports?agent=" + userAgent, new WebSocketListener() {
135136
@Override public void onClosing(WebSocket webSocket, int code, String reason) {
136137
webSocket.close(1000, null);
137138
latch.countDown();

okhttp/src/test/java/okhttp3/CallTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
import okhttp3.internal.DoubleInetAddressDns;
6363
import okhttp3.internal.RecordingOkAuthenticator;
6464
import okhttp3.internal.Util;
65-
import okhttp3.internal.Version;
6665
import okhttp3.internal.http.RecordingProxySelector;
6766
import okhttp3.internal.io.InMemoryFileSystem;
6867
import okhttp3.mockwebserver.Dispatcher;
@@ -95,6 +94,7 @@
9594
import static okhttp3.CipherSuite.TLS_DH_anon_WITH_AES_128_GCM_SHA256;
9695
import static okhttp3.TestUtil.awaitGarbageCollection;
9796
import static okhttp3.internal.Internal.addHeaderLenient;
97+
import static okhttp3.internal.Util.userAgent;
9898
import static okhttp3.tls.internal.TlsUtil.localhost;
9999
import static org.assertj.core.api.Assertions.assertThat;
100100
import static org.assertj.core.data.Offset.offset;
@@ -2711,7 +2711,7 @@ public void cancelWhileRequestHeadersAreSent_HTTP_2() throws Exception {
27112711
executeSynchronously("/");
27122712

27132713
RecordedRequest recordedRequest = server.takeRequest();
2714-
assertThat(recordedRequest.getHeader("User-Agent")).matches(Version.userAgent);
2714+
assertThat(recordedRequest.getHeader("User-Agent")).matches(userAgent);
27152715
}
27162716

27172717
@Test public void setFollowRedirectsFalse() throws Exception {
@@ -3024,7 +3024,7 @@ public void cancelWhileRequestHeadersAreSent_HTTP_2() throws Exception {
30243024

30253025
RecordedRequest connect = server.takeRequest();
30263026
assertThat(connect.getHeader("Private")).isNull();
3027-
assertThat(connect.getHeader("User-Agent")).isEqualTo(Version.userAgent);
3027+
assertThat(connect.getHeader("User-Agent")).isEqualTo(userAgent);
30283028
assertThat(connect.getHeader("Proxy-Connection")).isEqualTo("Keep-Alive");
30293029
assertThat(connect.getHeader("Host")).isEqualTo("android.com:443");
30303030

okhttp/src/test/java/okhttp3/OkHttpTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import org.assertj.core.api.Assertions.assertThat
1919
import org.junit.Test
2020

2121
class OkHttpTest {
22-
@Test
23-
fun testVersion() {
24-
assertThat(VERSION).matches("[0-9]+\\.[0-9]+\\.[0-9]+(-.+)?")
25-
}
22+
@Test
23+
fun testVersion() {
24+
assertThat(OkHttp.VERSION).matches("[0-9]+\\.[0-9]+\\.[0-9]+(-.+)?")
25+
}
2626
}

okhttp/src/test/java/okhttp3/URLConnectionTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import okhttp3.internal.Internal;
6464
import okhttp3.internal.RecordingAuthenticator;
6565
import okhttp3.internal.RecordingOkAuthenticator;
66-
import okhttp3.internal.Version;
6766
import okhttp3.internal.platform.Platform;
6867
import okhttp3.mockwebserver.MockResponse;
6968
import okhttp3.mockwebserver.MockWebServer;
@@ -94,6 +93,7 @@
9493
import static java.util.concurrent.TimeUnit.NANOSECONDS;
9594
import static okhttp3.internal.Internal.addHeaderLenient;
9695
import static okhttp3.internal.Util.immutableListOf;
96+
import static okhttp3.internal.Util.userAgent;
9797
import static okhttp3.internal.http.StatusLine.HTTP_PERM_REDIRECT;
9898
import static okhttp3.internal.http.StatusLine.HTTP_TEMP_REDIRECT;
9999
import static okhttp3.mockwebserver.SocketPolicy.DISCONNECT_AFTER_REQUEST;
@@ -1002,7 +1002,7 @@ private void initResponseCache() {
10021002
RecordedRequest connect = server.takeRequest();
10031003
assertThat(connect.getHeader("Private")).isNull();
10041004
assertThat(connect.getHeader("Proxy-Authorization")).isNull();
1005-
assertThat(connect.getHeader("User-Agent")).isEqualTo(Version.userAgent);
1005+
assertThat(connect.getHeader("User-Agent")).isEqualTo(userAgent);
10061006
assertThat(connect.getHeader("Host")).isEqualTo("android.com:443");
10071007
assertThat(connect.getHeader("Proxy-Connection")).isEqualTo("Keep-Alive");
10081008

@@ -3630,7 +3630,7 @@ private void zeroLengthPayload(String method) throws Exception {
36303630
assertContent("abc", getResponse(newRequest("/")));
36313631

36323632
RecordedRequest request = server.takeRequest();
3633-
assertThat(request.getHeader("User-Agent")).isEqualTo(Version.userAgent);
3633+
assertThat(request.getHeader("User-Agent")).isEqualTo(userAgent);
36343634
}
36353635

36363636
@Test public void urlWithSpaceInHost() {

0 commit comments

Comments
 (0)