[javalin-testtools] Provide a way to configure HttpClient / OkHttpClient for tests, unify with io.javalin.testing?
See original GitHub issueDescribe 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:
- Created a year ago
- Comments:5 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.I meant we could add
OkHttpClient
as a config option.No, I just assumed that it did store them.
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.
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!