Apollo Kotlin MockServer Help

Getting-started

Apollo MockServer is a KMP server for your HTTP and WebSocket tests.

Features:

  • Enqueue mocked HTTP responses

  • Dequeue recorded HTTP requests

  • Enqueue mocked WebSocket messages

  • Dequeue recorded WebSocket messages

  • JVM, Native and JS (Node) support

Apollo MockServer was initially developed for Apollo Kotlin integration tests and is provided as-is to the community.

Performance and compatibility are minimal. Do not use in production.

Non-goals:

  • HTTP2/HTTP3

  • HTTPS/TLS

  • Performance

Should a future version of Ktor or any other server framework provide a commonMain API, Apollo Kotlin MockServer would probably become deprecated.

In the meantime, add the dependency to your project and enjoy cross-platform integration tests!

[libraries] apollo-mockserver = "com.apollographql.mockserver:apollo-mockserver:0.1.0"

Enqueuing mock responses

To enqueue responses, use MockServer.enqueue:

// .use {} makes sure to release the resources at the end of the test MockServer().use { mockServer -> mockServer.enqueue( MockResponse.Builder() .statusCode(200) .body("Hello World") .build() ) // You can enqueue multiple responses if needed }

Reading recorded requests

Use MockServer.awaitRequest() to retrieve a recorded request that you can assert to check the behaviour of your client code.

// .use {} makes sure to release the resources at the end of the test MockServer().use { mockServer -> mockServer.enqueue(...) // Client code under test doHttpRequest(mockServer.url()) mockServer.awaitRequest().apply { // Assert the request here // assertEquals("GET", method) // ... } }

Next steps

Take a look at the Recipes or WebSockets pages to learn more.

Last modified: 19 July 2024