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

Closed
Husky22 opened this issue Dec 22, 2024 · 4 comments · Fixed by #4220
Closed

[BUG] SttpMockServerClient doesnt handle query params correctly #4219

Husky22 opened this issue Dec 22, 2024 · 4 comments · Fixed by #4220

Comments

@Husky22
Copy link
Contributor

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.

@adamw
Copy link
Member

adamw commented Dec 23, 2024

Sure, PRs are very welcome :)

@Husky22
Copy link
Contributor Author

Husky22 commented Dec 23, 2024

Created one with my first attempt :) Had a few problems in the beginning getting the tests running in intellij as the compiler complained that "-Yretain-trees" was missing

@adamw
Copy link
Member

adamw commented Dec 24, 2024

Hm retain-trees should only be necessary for enumeratum tests (as specified in build.sbt). Were it the mock server tests which caused problems? Maybe we should update our readme

@Husky22
Copy link
Contributor Author

Husky22 commented Dec 24, 2024

Running the tests through sbt is fine but running them through Intellij test runner compiles too much so that the enumeratum tests are compiled without retain-trees being enabled. Maybe I am doing something wrong - I am not too versed in sbt. Also running sttpMockServer3/testOnly -- -z query compiles 88 source files every time, even though I only changed the implementation of the test. Otherwise thanks for this awesome library :) Having a lot of fun with it!

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

Successfully merging a pull request may close this issue.

2 participants