question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[javalin-testtools] Provide a way to configure HttpClient / OkHttpClient for tests, unify with io.javalin.testing?

See original GitHub issue

Describe the feature Consider providing a way to configure / alter default configuration for HttpClient / OkHttpClient to customise it (e.g. persist cookies between http calls). Please note that OkHttpClient (present in javalin-testtools) differs in Cookie Management with Unirest (used in javalin for tests) - maybe we should unify io.javalin.testing with javalin-testtools?

Additional context I was playing around with ctx.sessionAttribute:

public class Application {

    public Javalin app;

    public Application() {
        app = Javalin
                .create()
                .routes(() -> {
                    get("/store-session", (ctx) -> {
                        ctx.sessionAttribute("test", "tast");
                    });
                    get("/read-session", (ctx) -> {
                        String value = ctx.sessionAttribute("test");
                        if (value == null) {
                            ctx.result("empty");
                            ctx.status(404);
                        } else {
                            ctx.result(value);
                        }
                    });
                });
    }
}

and I wanted to test it using javalin-testtools:

class ApplicationTest {

    @Test
    void GET_read_session_should_return_404_when_first_request() {
        JavalinTest.test(new Application().app, (server, client) -> {
            // when
            Response response = client.get("/read-session");

            // then
            assertEquals(404, response.code());
            assertEquals("empty", response.body().string());
        });
    }

    @Test
    void GET_read_session_should_return_200_when_stored_first() {
        JavalinTest.test(new Application().app, (server, client) -> {
            // given
            client.get("/store-session");

            // when
            Response response = client.get("/read-session");

            // then
            assertEquals(200, response.code());
            assertEquals("tast", response.body().string());
        });
    }
}

Unfortunately cookies are not stored between calls because HttpClient invokes empty constructor for OkHttpClient which does not store cookies:

internal var cookieJar: CookieJar = CookieJar.NO_COOKIES

Full example - https://github.com/mat-mik/javalin-test-cookie

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tipsycommented, Jun 24, 2022

Either option is fine with me! I think I do prefer if we make the current JavalinTest instantiable though, it would make having a specific configuration less painful.

1reaction
tipsycommented, Jun 24, 2022

We could try to expose some param via it to alter cookies management

I meant we could add OkHttpClient as a config option.

By the way - does it make any sense to clear cookies by default when HttpClient is not storing them?

No, I just assumed that it did store them.

But would you consider it as a breaking change?

It would be a breaking change, but we are on 5.0-SNAPSHOT on master now, so it’s a good time for breaking changes. OkHttp is “better” than Unirest though, so I think i want to keep that.

Or maybe we could change HttpClient to an interface

We could do that, but it sounds like a bit of work. If you want to do it, I’d be happy to review though!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Testing Javalin Applications
Since Javalin is a library, there are no requirements for how tests must be written. This guide will outline a few common approaches....
Read more >
How to Test Java HTTP Client Usages (e.g. OkHttp, Apache ...
Test Java classes that use a common HTTP client (e.g. OkHttp, Apache HttpClient, etc.) without mocking the internals by using MockWebServer.
Read more >
An Acceptance Testing tool for Javalin 3 - GitHub
javalin -test is a tool that will launch a short-lived javalin server in your tests. From here, you can register your request handlers...
Read more >
Unit Testing OkHttp - tomaytotomato.com
In the test setup() method we start an instance of the MockWebServer and get the URL of it. This allows us to configure...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found