Skip to content

Commit

Permalink
Add tests for URLConnection wrappers (embrace-io#35)
Browse files Browse the repository at this point in the history
## Goal

Add tests to verify that our network wrappers actually call the appropriate methods that they are wrapping. Along the way, added some more comments and did some renaming.
  • Loading branch information
bidetofevil authored Oct 31, 2023
2 parents cc08fa6 + 472e9e5 commit ad56d48
Show file tree
Hide file tree
Showing 8 changed files with 760 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@

import javax.net.ssl.SSLPeerUnverifiedException;

/**
* An interface that duplicates the functionality of HttpURLConnection whose implementation can then wrap an actual HttpURLConnection's
* implementation so calls can be forwarded from this interface's implementation to the wrapped HttpURLConnection implementation.
* Having this separate interface allows the actual wrappers ({@link EmbraceHttpUrlConnectionImpl}/{@link EmbraceHttpsUrlConnectionImpl})
* to share an implementation whilst exposing separate interfaces for what they're wrapping.
*/
interface EmbraceHttpUrlConnection {

void addRequestProperty(@NonNull String key, @Nullable String value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import java.util.List;
import java.util.Map;

/**
* An implementation of HttpURLConnection that forwards calls to an {@link EmbraceUrlConnectionDelegate}
*/
class EmbraceHttpUrlConnectionImpl<T extends HttpURLConnection> extends HttpURLConnection {

private final EmbraceHttpUrlConnection embraceHttpUrlConnectionDelegate;
Expand All @@ -23,8 +26,12 @@ class EmbraceHttpUrlConnectionImpl<T extends HttpURLConnection> extends HttpURLC
* @param enableWrapIoStreams true if we should transparently ungzip the response, else false
*/
public EmbraceHttpUrlConnectionImpl(T connection, boolean enableWrapIoStreams) {
this(connection, new EmbraceUrlConnectionDelegate<>(connection, enableWrapIoStreams));
}

EmbraceHttpUrlConnectionImpl(T connection, EmbraceUrlConnectionDelegate<T> delegate) {
super(connection.getURL());
embraceHttpUrlConnectionDelegate = new EmbraceUrlConnectionDelegate<>(connection, enableWrapIoStreams);
embraceHttpUrlConnectionDelegate = delegate;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import javax.net.ssl.SSLSocketFactory;

/**
* Additional methods for HTTPS network calls
* {@link EmbraceHttpUrlConnection} plus additional methods for HTTPS network calls
*/
interface EmbraceHttpsUrlConnection extends EmbraceHttpUrlConnection {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ class EmbraceHttpsUrlConnectionImpl<T extends HttpsURLConnection> extends HttpsU
* @param enableWrapIoStreams true if we should transparently ungzip the response, else false
*/
public EmbraceHttpsUrlConnectionImpl(T connection, boolean enableWrapIoStreams) {
this(connection, new EmbraceUrlConnectionDelegate<>(connection, enableWrapIoStreams));
}

EmbraceHttpsUrlConnectionImpl(T connection, EmbraceUrlConnectionDelegate<T> delegate) {
super(connection.getURL());
embraceHttpsUrlConnectionDelegate = new EmbraceUrlConnectionDelegate<>(connection, enableWrapIoStreams);
embraceHttpsUrlConnectionDelegate = delegate;
}

@Override
Expand Down Expand Up @@ -342,12 +346,12 @@ public void setHostnameVerifier(HostnameVerifier verifier) {
this.embraceHttpsUrlConnectionDelegate.setHostnameVerifier(verifier);
}


@Override
public Principal getLocalPrincipal() {
return embraceHttpsUrlConnectionDelegate.getLocalPrincipal();
}


@Override
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
return embraceHttpsUrlConnectionDelegate.getPeerPrincipal();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import kotlin.jvm.functions.Function0;

/**
* Wraps @{link HttpUrlConnection} to log network calls to Embrace. The wrapper also wraps the
* Wraps {@link HttpURLConnection} to log network calls to Embrace. The wrapper also wraps the
* InputStream to get an accurate count of bytes received if a Content-Length is not provided by
* the server.
* <p>
Expand Down
Loading

0 comments on commit ad56d48

Please sign in to comment.