Test::Mojo to test WS client?

25 views
Skip to first unread message

Felipe Gasper

unread,
Jan 22, 2020, 10:34:51 AM1/22/20
to mojol...@googlegroups.com
Hello,

I see t/mojolicious/websocket_lite_app.t in the Mojolicious distribution, but this seems to test Mojo as a WS server. Are there any examples of using Test::Mojo--or any other tool--to test Mojo-powered WS clients?

Thank you!

-FG

Dan Book

unread,
Jan 22, 2020, 10:39:03 AM1/22/20
to mojol...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/80877B50-2AD9-4C15-9410-52D74C104161%40felipegasper.com.

Dan Book

unread,
Jan 22, 2020, 10:41:21 AM1/22/20
to mojol...@googlegroups.com
You mentioned using Test::Mojo - the test file you mentioned does this, it tests both client and server.

-Dan

Felipe Gasper

unread,
Jan 22, 2020, 12:25:56 PM1/22/20
to mojol...@googlegroups.com
Hi Dan,

Thank you for your response!

So, maybe what I need isn’t Test::Mojo. I have:

-----
package t::MockApp;

use Mojo::Base -strict;

use Mojolicious::Lite;

websocket '/my-websocket-endpoint' => sub { ... }
-----

… and I’d like my tested $ua (Mojo::UserAgent->new()) to call logic defined in t::MockApp, rather than going out to the real world. I’d also like to define other mock server classes and have other parts of the test use those classes instead.

I’ve tried “$ua->server->app($TEST_MOJO_APP) if $TEST_MOJO_APP” in my tested code, but that doesn’t seem to work. (The client times out rather than connecting to my WS server logic.)


-FG
> To view this discussion on the web visit https://groups.google.com/d/msgid/mojolicious/CABMkAVVE1dvq-wv5HqTcXDExqUpOa4_%3DsZCZDh%3DZ7zJGvwDs4Q%40mail.gmail.com.

Felipe Gasper

unread,
Jan 22, 2020, 1:32:14 PM1/22/20
to mojol...@googlegroups.com

> On Jan 22, 2020, at 12:25 PM, Felipe Gasper <fel...@felipegasper.com> wrote:
>
> Hi Dan,
>
> Thank you for your response!
>
> So, maybe what I need isn’t Test::Mojo. I have:
>
> -----
> package t::MockApp;
>
> use Mojo::Base -strict;
>
> use Mojolicious::Lite;
>
> websocket '/my-websocket-endpoint' => sub { ... }
> -----
>
> … and I’d like my tested $ua (Mojo::UserAgent->new()) to call logic defined in t::MockApp, rather than going out to the real world. I’d also like to define other mock server classes and have other parts of the test use those classes instead.
>
> I’ve tried “$ua->server->app($TEST_MOJO_APP) if $TEST_MOJO_APP” in my tested code, but that doesn’t seem to work. (The client times out rather than connecting to my WS server logic.)

Follow-up: I have this working but only if I alter the tested logic to give a relative URL rather than an absolute URL to $tx->websocket().

Is there any tooling in place for handling absolute URLs in such setups? I can override $tx->websocket() but wonder if there’s any ready-made sugar for this.

Thank you!

-FG
Reply all
Reply to author
Forward
0 new messages