Skip to content

Commit

Permalink
Extend Proxy options with proxy type (#3326)
Browse files Browse the repository at this point in the history
* Extend Proxy options with proxy type

* Fix ctor overloads

* Update changelog

* Fix typo
  • Loading branch information
markushi authored Apr 8, 2024
1 parent ab55b2c commit 60eb716
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Add support for Spring Rest Client ([#3199](https://github.com/getsentry/sentry-java/pull/3199))
- Extend Proxy options with proxy type ([#3326](https://github.com/getsentry/sentry-java/pull/3326))

### Fixes

Expand Down
4 changes: 4 additions & 0 deletions sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -2447,13 +2447,17 @@ public final class io/sentry/SentryOptions$Proxy {
public fun <init> ()V
public fun <init> (Ljava/lang/String;Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/net/Proxy$Type;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Ljava/net/Proxy$Type;Ljava/lang/String;Ljava/lang/String;)V
public fun getHost ()Ljava/lang/String;
public fun getPass ()Ljava/lang/String;
public fun getPort ()Ljava/lang/String;
public fun getType ()Ljava/net/Proxy$Type;
public fun getUser ()Ljava/lang/String;
public fun setHost (Ljava/lang/String;)V
public fun setPass (Ljava/lang/String;)V
public fun setPort (Ljava/lang/String;)V
public fun setType (Ljava/net/Proxy$Type;)V
public fun setUser (Ljava/lang/String;)V
}

Expand Down
40 changes: 32 additions & 8 deletions sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.sentry.util.thread.IMainThreadChecker;
import io.sentry.util.thread.NoOpMainThreadChecker;
import java.io.File;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -2676,26 +2677,41 @@ public static final class Proxy {
private @Nullable String port;
private @Nullable String user;
private @Nullable String pass;
private @Nullable java.net.Proxy.Type type;

public Proxy() {
this(null, null, null, null, null);
}

public Proxy(@Nullable String host, @Nullable String port) {
this(host, port, null, null, null);
}

public Proxy(@Nullable String host, @Nullable String port, @Nullable java.net.Proxy.Type type) {
this(host, port, type, null, null);
}

public Proxy(
final @Nullable String host,
final @Nullable String port,
final @Nullable String user,
final @Nullable String pass) {
this(host, port, null, user, pass);
}

public Proxy(
final @Nullable String host,
final @Nullable String port,
final @Nullable java.net.Proxy.Type type,
final @Nullable String user,
final @Nullable String pass) {
this.host = host;
this.port = port;
this.type = type;
this.user = user;
this.pass = pass;
}

public Proxy() {
this(null, null, null, null);
}

public Proxy(@Nullable String host, @Nullable String port) {
this(host, port, null, null);
}

public @Nullable String getHost() {
return host;
}
Expand Down Expand Up @@ -2727,6 +2743,14 @@ public void setUser(final @Nullable String user) {
public void setPass(final @Nullable String pass) {
this.pass = pass;
}

public @Nullable java.net.Proxy.Type getType() {
return type;
}

public void setType(final @Nullable java.net.Proxy.Type type) {
this.type = type;
}
}

public static final class Cron {
Expand Down
8 changes: 7 additions & 1 deletion sentry/src/main/java/io/sentry/transport/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ public HttpConnection(
final String host = optionsProxy.getHost();
if (port != null && host != null) {
try {
final @NotNull Proxy.Type type;
if (optionsProxy.getType() != null) {
type = optionsProxy.getType();
} else {
type = Proxy.Type.HTTP;
}
InetSocketAddress proxyAddr = new InetSocketAddress(host, Integer.parseInt(port));
proxy = new Proxy(Proxy.Type.HTTP, proxyAddr);
proxy = new Proxy(type, proxyAddr);
} catch (NumberFormatException e) {
options
.getLogger()
Expand Down
4 changes: 3 additions & 1 deletion sentry/src/test/java/io/sentry/SentryOptionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.sentry.backpressure.NoOpBackpressureMonitor
import io.sentry.util.StringUtils
import org.mockito.kotlin.mock
import java.io.File
import java.net.Proxy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
Expand Down Expand Up @@ -350,7 +351,7 @@ class SentryOptionsTest {
externalOptions.environment = "environment"
externalOptions.release = "release"
externalOptions.serverName = "serverName"
externalOptions.proxy = SentryOptions.Proxy("example.com", "8090")
externalOptions.proxy = SentryOptions.Proxy("example.com", "8090", Proxy.Type.SOCKS)
externalOptions.setTag("tag1", "value1")
externalOptions.setTag("tag2", "value2")
externalOptions.enableUncaughtExceptionHandler = false
Expand Down Expand Up @@ -391,6 +392,7 @@ class SentryOptionsTest {
assertNotNull(options.proxy)
assertEquals("example.com", options.proxy!!.host)
assertEquals("8090", options.proxy!!.port)
assertEquals(java.net.Proxy.Type.SOCKS, options.proxy!!.type)
assertEquals(mapOf("tag1" to "value1", "tag2" to "value2"), options.tags)
assertFalse(options.isEnableUncaughtExceptionHandler)
assertEquals(true, options.enableTracing)
Expand Down
22 changes: 22 additions & 0 deletions sentry/src/test/java/io/sentry/transport/HttpConnectionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,28 @@ class HttpConnectionTest {
verify(fixture.authenticatorWrapper, never()).setDefault(any())
}

@Test
fun `When Proxy type is not set, it defaults to HTTP`() {
fixture.proxy = Proxy("proxy.example.com", "8080")
val transport = fixture.getSUT()

transport.send(createEnvelope())

assertEquals(Type.HTTP, transport.proxy!!.type())
}

@Test
fun `When Proxy type is set to SOCKS, HTTP connection uses it`() {
fixture.proxy = Proxy("proxy.example.com", "8080").apply {
type = Type.SOCKS
}
val transport = fixture.getSUT()

transport.send(createEnvelope())

assertEquals(Type.SOCKS, transport.proxy!!.type())
}

@Test
fun `sets common headers and on http connection`() {
val transport = fixture.getSUT()
Expand Down

0 comments on commit 60eb716

Please sign in to comment.