Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to proxy to mockserver with auth #1838

Open
yschimke opened this issue Feb 3, 2024 · 1 comment
Open

How to proxy to mockserver with auth #1838

yschimke opened this issue Feb 3, 2024 · 1 comment

Comments

@yschimke
Copy link

yschimke commented Feb 3, 2024

Describe the issue

I want to test a client handling of Proxy Auth using mockserver as both the proxy and the origin server.

But I can't get Auth to fail.

What you are trying to do

package okhttp3.containers

import assertk.assertThat
import assertk.assertions.contains
import java.net.HttpURLConnection
import java.net.Proxy
import java.net.URI
import okhttp3.containers.BasicMockServerTest.Companion.MOCKSERVER_IMAGE
import okio.buffer
import okio.source
import org.junit.jupiter.api.Test
import org.mockserver.client.MockServerClient
import org.mockserver.model.HttpRequest.request
import org.mockserver.model.HttpResponse.response
import org.mockserver.proxyconfiguration.ProxyConfiguration
import org.testcontainers.containers.MockServerContainer
import org.testcontainers.junit.jupiter.Container
import org.testcontainers.junit.jupiter.Testcontainers

@Testcontainers
class ProxyWithAuthTest {
  @Container
  val mockServer: MockServerContainer =
    MockServerContainer(MOCKSERVER_IMAGE)
      .withNetworkAliases("mockserver")

  @Test
  fun testUrlConnectionPlaintextProxied() {
    MockServerClient(mockServer.host, mockServer.serverPort).use { mockServerClient ->
      mockServerClient.withProxyConfiguration(
        ProxyConfiguration.proxyConfiguration(
          ProxyConfiguration.Type.HTTP,
          mockServerClient.remoteAddress(),
          "admin",
          "password"
        )
      )

      val request =
        request().withPath("/person")
          .withQueryStringParameter("name", "peter")

      mockServerClient
        .`when`(
          request,
        )
        .respond(response().withBody("Peter the person!"))

      val proxy = Proxy(
        Proxy.Type.HTTP,
        mockServerClient.remoteAddress(),
      )
      val url = URI(mockServer.endpoint + "/person?name=peter").toURL()
      val connection = url.openConnection(proxy) as HttpURLConnection
      assertThat(connection.inputStream.source().buffer().readUtf8()).contains("Peter the person")
    }
  }
}

MockServer version

5.15.0

MockServer Log

I can provide, but nothing interesting, and I suspect I'm using API wrong.

@yschimke
Copy link
Author

yschimke commented Feb 6, 2024

I'm guessing with testcontainers this needs to be configured before starting


  @Container
  val mockServer: MockServerContainer =
    MockServerContainer(MOCKSERVER_IMAGE)
      .withNetworkAliases("mockserver")
      .withCopyToContainer(
        MountableFile.forClasspathResource("/proxy-config.properties"),
        "/config/mockserver.properties"
      )
      .withLogConsumer {
        println(it.utf8StringWithoutLineEnding)
      }

But I'm still working on the correct config.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant