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

[BUG] SttpMockServerClient doesnt handle query params correctly #4219

Open
Husky22 opened this issue Dec 22, 2024 · 0 comments
Open

[BUG] SttpMockServerClient doesnt handle query params correctly #4219

Husky22 opened this issue Dec 22, 2024 · 0 comments

Comments

@Husky22
Copy link

Husky22 commented Dec 22, 2024

Tapir version: 1.11.10

Scala version: 3.5.2

Describe the bug
When using query Parameters in an endpoint, the Mock Server expectation contains only a path parameter with the query params encoded

"path" : "/api/v1/person?hi=test"

but the resulting request encodes the query Parameters in an object :

{
    "path" : "/api/v1/person",
    "queryStringParameters" : {
      "hi" : [ "test" ]
    }
}

Mock Server cannot handle this deviation and the test fails.

How to reproduce?
I adapted the minimal example to also use one query parameter

//> using dep com.softwaremill.sttp.tapir::tapir-core:1.11.10
//> using dep com.softwaremill.sttp.tapir::tapir-json-circe:1.11.10
//> using dep com.softwaremill.sttp.tapir::sttp-mock-server:1.11.10
//> using dep com.softwaremill.sttp.client3::core:3.9.8
//> using dep org.mock-server:mockserver-netty:5.15.0
//> using dep org.scalatest::scalatest:3.2.19

package sttp.tapir.examples.testing

import io.circe.generic.auto._
import org.mockserver.integration.ClientAndServer.startClientAndServer
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach}
import sttp.client3.{TryHttpURLConnectionBackend, UriContext}
import sttp.tapir.DecodeResult.Value
import sttp.tapir._
import sttp.tapir.client.sttp.SttpClientInterpreter
import sttp.tapir.generic.auto._
import sttp.tapir.json.circe.jsonBody
import sttp.tapir.server.mockserver._

import scala.util.Success

class SttpMockServerClientExample extends AnyFlatSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEach:
  behavior of "SttpMockServerClient"

  private val baseUri = uri"http://localhost:1080"

  private val backend = TryHttpURLConnectionBackend()

  private val mockServer = startClientAndServer(1080)

  private val mockServerClient = SttpMockServerClient(baseUri = baseUri, backend)

  override def afterEach(): Unit = mockServerClient.clear

  override def afterAll(): Unit = mockServer.stop()

  case class SampleIn(name: String, age: Int)
  case class SampleOut(greeting: String)

  private val jsonEndpoint = endpoint
    .in("api" / "v1" / "person")
    .put
    .in(jsonBody[SampleIn])
    .out(jsonBody[SampleOut])

  it should "test json endpoint" in {
    val sampleIn = SampleIn("John", 23)
    val sampleOut = SampleOut("Hello, John!")

    val actual = for {
      _ <- mockServerClient
        .whenInputMatches(jsonEndpoint)((), sampleIn)
        .thenSuccess(sampleOut)

      resp <- SttpClientInterpreter()
        .toRequest(jsonEndpoint, baseUri = Some(baseUri))
        .apply(sampleIn)
        .send(backend)

      _ <- mockServerClient
        .verifyRequest(jsonEndpoint, VerificationTimes.exactlyOnce)((), sampleIn)
    } yield resp.body

    actual shouldEqual Success(Value(Right(sampleOut)))
  }

Additional information
My guess is that we will need to expand ExpectationRequestDefinition with queryStringParameters and update its constructor. I would like to create a pull request if this approach is fine with you.

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