Repository: square/okhttp
File: android-test/src/androidTest/java/okhttp/android/test/OkHttpTest.kt (Kotlin)

Code before:

      var socketClass: String? = null

      val clientCertificates = HandshakeCertificates.Builder()
          .addPlatformTrustedCertificates()
          .addInsecureHost(server.hostName)
          .build()

      // Need fresh client to reset sslSocketFactoryOrNull
      client = OkHttpClient.Builder()
          .eventListenerFactory(clientTestRule.wrap(object : EventListener() {
        override fun connectionAcquired(call: Call, connection: Connection) {
          socketClass = connection.socket().javaClass.name
        }
      }))
          .sslSocketFactory(clientCertificates.sslSocketFactory(), clientCertificates.trustManager)
          .build()

      val response = client.newCall(request).execute()

      response.use {
        assertEquals(Protocol.HTTP_2, response.protocol)
        assertEquals(200, response.code)
        // see https://github.com/google/conscrypt/blob/b9463b2f74df42d85c73715a5f19e005dfb7b802/android/src/main/java/org/conscrypt/Platform.java#L613
        when {
            // TODO: how do we determine when we expect useEngineSocket to be true, resulting in org.conscrypt.Java8EngineSocket?
            Build.VERSION.SDK_INT >= 24 -> {
              // Depending on JDK, it can be either Java8FileDescriptorSocket or Java8EngineSocket
              try {
                assertEquals("org.conscrypt.Java8FileDescriptorSocket", socketClass)
              } catch (e: AssertionError) {
                assertEquals("org.conscrypt.Java8EngineSocket", socketClass)
              }
            }
            Build.VERSION.SDK_INT < 22 -> {
              assertEquals("org.conscrypt.KitKatPlatformOpenSSLSocketImplAdapter", socketClass)
            }
            else -> {
              assertEquals("org.conscrypt.ConscryptFileDescriptorSocket", socketClass)
            }
        }
        assertEquals(TlsVersion.TLS_1_3, response.handshake?.tlsVersion)
      }

      localhostInsecureRequest();
    } finally {
      Security.removeProvider("Conscrypt")
      client.close()
    }
  }


Review comment (suggestion):
this is maybe not worth doing, if the below or similar if branch is ok. Alternatively, we can switch to assertJ and use a startsWith assertion

Code after:

      var socketClass: String? = null

      val clientCertificates = HandshakeCertificates.Builder()
          .addPlatformTrustedCertificates()
          .addInsecureHost(server.hostName)
          .build()

      // Need fresh client to reset sslSocketFactoryOrNull
      client = OkHttpClient.Builder()
          .eventListenerFactory(clientTestRule.wrap(object : EventListener() {
        override fun connectionAcquired(call: Call, connection: Connection) {
          socketClass = connection.socket().javaClass.name
        }
      }))
          .sslSocketFactory(clientCertificates.sslSocketFactory(), clientCertificates.trustManager)
          .build()

      val response = client.newCall(request).execute()

      response.use {
        assertEquals(Protocol.HTTP_2, response.protocol)
        assertEquals(200, response.code)
        // see https://github.com/google/conscrypt/blob/b9463b2f74df42d85c73715a5f19e005dfb7b802/android/src/main/java/org/conscrypt/Platform.java#L613
        when {
            Build.VERSION.SDK_INT >= 24 -> {
              // Conscrypt 2.5+ defaults to SSLEngine-based SSLSocket
              assertEquals("org.conscrypt.Java8EngineSocket", socketClass)
            }
            Build.VERSION.SDK_INT < 22 -> {
              assertEquals("org.conscrypt.KitKatPlatformOpenSSLSocketImplAdapter", socketClass)
            }
            else -> {
              assertEquals("org.conscrypt.ConscryptFileDescriptorSocket", socketClass)
            }
        }
        assertEquals(TlsVersion.TLS_1_3, response.handshake?.tlsVersion)
      }

      localhostInsecureRequest();
    } finally {
      Security.removeProvider("Conscrypt")
      client.close()
    }
  }

  @Test
  fun testRequestUsesPlayProvider() {
    assumeNetwork()

    try {