OkHttp3Test.groovy 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import io.opentelemetry.auto.test.base.HttpClientTest
  17. import okhttp3.Headers
  18. import okhttp3.MediaType
  19. import okhttp3.OkHttpClient
  20. import okhttp3.Request
  21. import okhttp3.RequestBody
  22. import okhttp3.internal.http.HttpMethod
  23. import spock.lang.Timeout
  24. import java.util.concurrent.TimeUnit
  25. @Timeout(5)
  26. class OkHttp3Test extends HttpClientTest {
  27. def client = new OkHttpClient.Builder()
  28. .connectTimeout(CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
  29. .readTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
  30. .writeTimeout(READ_TIMEOUT_MS, TimeUnit.MILLISECONDS)
  31. .build()
  32. @Override
  33. int doRequest(String method, URI uri, Map<String, String> headers, Closure callback) {
  34. def body = HttpMethod.requiresRequestBody(method) ? RequestBody.create(MediaType.parse("text/plain"), "") : null
  35. def request = new Request.Builder()
  36. .url(uri.toURL())
  37. .method(method, body)
  38. .headers(Headers.of(headers)).build()
  39. def response = client.newCall(request).execute()
  40. callback?.call()
  41. return response.code()
  42. }
  43. boolean testRedirects() {
  44. false
  45. }
  46. }