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

Use mock http servers from okhttp #1441

Merged
merged 20 commits into from
May 19, 2023

Conversation

Semernitskaya
Copy link
Contributor

@Semernitskaya Semernitskaya commented Apr 11, 2023

Description

  • All tests in the project that used com.github.restdriver.clientdriver.ClientDriver as mock web server were migrated to okhttp3.mockwebserver.MockWebServer instead.
  • MockWebServerUtil util class was introduced to simplify migration and add functionality missing in the new okhttp3.mockwebserver.MockWebServer.
  • MockWebServerUtil class is duplicated across modules and can be reviewed only in one place. The same duplication approach was used for the existing MockSetup class. In the future we can avoid copying and pasting them by introducing a new project module that will be used only with the test scope (see discussion Use mock http servers from okhttp #1441 (comment)).
  • com.github.restdriver.clientdriver.ClientDriver allows you to define response for a specific request and then use verify() method to ensure that driver got this exact request. For okhttp3.mockwebserver.MockWebServer you usually define common response for all possible requests, then check the request you actually got (using server.takeRequest(...) method). Multiple verify() methods were added to MockWebServerUtil class to simplify such check. See https://github.com/zalando/riptide/pull/1441/files#diff-c2b11cfb53a03add49f5f20b1efdbd3c238230a2ec547eed41218d6c8dbdfff6R95 for a simple example of old validation vs new validation.

Motivation and Context

Fixes #1392.
Migration to the new mock test server is required because com.github.restdriver.clientdriver.ClientDriver doesn't support latest Jetty version and thus blocks migration to the Spring Boot 3 & Spring 6.
Different alternatives were analysed and discussed in the task, see #1392 (comment) for more details.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation. (Not applicable as changes are in test classes only)
  • I have updated the documentation accordingly. (Not applicable as changes are in test classes only)
  • I have added tests to cover my changes. (Not applicable as changes are in test classes only)


reference.set(URI.create("http://www.example.net/"));
http.get().call(pass()).join();
try {
Copy link
Contributor Author

@Semernitskaya Semernitskaya May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously invocation was to real pages http://www.example.net/ and http://www.example.org/, so part

driver.addExpectation(
                onRequestTo("http://www.example.org/"),
                giveEmptyResponse());

wasn't actually used

I've changed the test to use the same base URL but different paths (/path1 and /path2). Another approach would involve continuing to use the pages http://www.example.net/ and http://www.example.org/ while completely removing the mock server from this test. However, it is not clear how to verify in this scenario that the URL has changed between invocations.

.withHeader("X-Content-Encoding", "gzip") // written by Jetty's GzipHandler
.withBody(equalTo("{}"), "application/json"),
giveResponse("", "text/plain"));
server.enqueue(textMockResponse(""));
Copy link
Contributor Author

@Semernitskaya Semernitskaya May 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot use Jetty's GzipHandler anymore, so we cannot test header inserted by it (X-Content-Encoding).
I'm checking header inserted by the plugin and that body was actually compressed (by decompressing it and checking the result)

@fatroom
Copy link
Member

fatroom commented May 19, 2023

Thank you for your contribution!

@fatroom
Copy link
Member

fatroom commented May 19, 2023

👍

@fatroom fatroom merged commit 53f8e14 into zalando:main May 19, 2023
1 check passed
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 this pull request may close these issues.

Consolidate usage of http mock servers in tests
2 participants