Bad practice to send requests twice using the same connection in the same test?

A coworker has written a controller test where he occasionally sends a test request twice in one test using the same connection. Here is an example (lines 81 and 84):
This was not previously a problem as the tests had passed. However, I am modifying the router to require JWT authentication, which involves setting the request header at one point. This causes “(Plug.Conn.AlreadySentError) the response was already sent”. I suspect it has to do with line 84 where the same connection was involved in another request. Is this bad practice? If not, how do I resolve the (Plug.Conn.AlreadySentError)? Thanks

It’s usually fine to reuse the conn, perhaps recycle helps?

If not, I think the big is in your implementation rather than in the tests.

Ps: it’s much easier to read if you actually copy and paste code and annotate it using standard markdown, than to put a screenshot, which is bad for smaller bandwidths or data plans.


Yeah, I’ll make sure to use the markdown in the future.

I inserted conn = recycle(conn) right before the second (conn, path, params), but the headers were not recycled into the new conn. Also, after discussing with my coworker, I am deciding to limit each test to 1 http request. Thanks for the tip though, I’ll consider using recycle in the future for similar issues.

If I were doing this, I’d probably do:

result_conn = put conn, ...

# do stuff

result_conn = get conn, ...

That way conn is never rebound and is always the fresh conn provided to the test. However, I agree that making two requests in the same test is probably a code smell.