File tree Expand file tree Collapse file tree
main/java/datadog/communication/http
test/groovy/datadog/communication/http Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ dependencies {
2020 testImplementation deps. bytebuddy
2121 testImplementation group : ' org.msgpack' , name : ' msgpack-core' , version : ' 0.8.20'
2222 testImplementation group : ' org.msgpack' , name : ' jackson-dataformat-msgpack' , version : ' 0.8.20'
23+ testImplementation group : ' com.squareup.okhttp3' , name : ' mockwebserver' , version : versions. okhttp_legacy
2324}
2425
2526ext {
3334 ' datadog.communication.http.OkHttpUtils' ,
3435 ' datadog.communication.http.OkHttpUtils.1' ,
3536 ' datadog.communication.http.OkHttpUtils.ByteBufferRequestBody' ,
37+ ' datadog.communication.http.OkHttpUtils.CustomListener' ,
3638 ' datadog.communication.http.OkHttpUtils.GZipByteBufferRequestBody' ,
3739 ' datadog.communication.http.OkHttpUtils.GZipRequestBodyDecorator' ,
3840 ' datadog.communication.http.OkHttpUtils.JsonRequestBody' ,
Original file line number Diff line number Diff line change 2323import okhttp3 .ConnectionSpec ;
2424import okhttp3 .Credentials ;
2525import okhttp3 .Dispatcher ;
26+ import okhttp3 .EventListener ;
2627import okhttp3 .HttpUrl ;
2728import okhttp3 .MediaType ;
2829import okhttp3 .OkHttpClient ;
@@ -100,6 +101,8 @@ public static OkHttpClient buildHttpClient(
100101 timeoutMillis );
101102 }
102103
104+ public abstract static class CustomListener extends EventListener {}
105+
103106 private static OkHttpClient buildHttpClient (
104107 final String unixDomainSocketPath ,
105108 final String namedPipe ,
@@ -115,6 +118,12 @@ private static OkHttpClient buildHttpClient(
115118 final OkHttpClient .Builder builder = new OkHttpClient .Builder ();
116119
117120 builder
121+ .eventListenerFactory (
122+ call -> {
123+ Request request = call .request ();
124+ CustomListener listener = request .tag (CustomListener .class );
125+ return listener != null ? listener : EventListener .NONE ;
126+ })
118127 .connectTimeout (timeoutMillis , MILLISECONDS )
119128 .writeTimeout (timeoutMillis , MILLISECONDS )
120129 .readTimeout (timeoutMillis , MILLISECONDS )
Original file line number Diff line number Diff line change 1+ package datadog.communication.http
2+
3+ import okhttp3.Call
4+ import okhttp3.Request
5+ import okhttp3.mockwebserver.MockResponse
6+ import okhttp3.mockwebserver.MockWebServer
7+ import spock.lang.Shared
8+ import spock.lang.Specification
9+
10+ class OkHttpUtilsTest extends Specification {
11+
12+ @Shared
13+ MockWebServer server = new MockWebServer ()
14+
15+ def cleanupSpec () {
16+ server. shutdown()
17+ }
18+
19+ def " It is possible to register a custom listener for HTTP requests" () {
20+ setup :
21+ def url = server. url(" /" )
22+ def client = OkHttpUtils . buildHttpClient(url, 1000 )
23+ def listener = new TestListener ()
24+
25+ server. enqueue(new MockResponse ())
26+
27+ when :
28+ def request = new Request.Builder ()
29+ .url(url)
30+ .get()
31+ .tag(OkHttpUtils.CustomListener , listener)
32+ .build()
33+ client. newCall(request). execute()
34+
35+ then :
36+ listener. notified
37+ }
38+
39+ private static final class TestListener extends OkHttpUtils. CustomListener {
40+ private boolean notified
41+
42+ void callStart (Call call ) {
43+ notified = true
44+ }
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments