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

export wstest #340

Open
Himani-relan opened this issue May 16, 2022 · 4 comments
Open

export wstest #340

Himani-relan opened this issue May 16, 2022 · 4 comments
Milestone

Comments

@Himani-relan
Copy link

Himani-relan commented May 16, 2022

Pipe function here: https://github.com/nhooyr/websocket/blob/master/internal/test/wstest/pipe.go#L17 is inside the internal package which means it can't be imported by other projects.
However it should be external so that it can be imported by other projects. This could help in unit testing of the projects

@Himani-relan Himani-relan changed the title Pipe function shouldn't inside internal package Pipe function shouldn May 16, 2022
@Himani-relan Himani-relan changed the title Pipe function shouldn Pipe function shouldn't be internal to the project May 16, 2022
@nhooyr
Copy link
Contributor

nhooyr commented Mar 5, 2023

I figured you'd always want to use httptest instead to setup two real websockets. But I suppose this can be released under a wstest package.

@nhooyr nhooyr changed the title Pipe function shouldn't be internal to the project export wstest Mar 5, 2023
@nhooyr nhooyr added this to the v1.9.0 milestone Sep 28, 2023
@leosunmo
Copy link

I found this internal package useful for testing. I have a struct that stays around for the duration of a websocket connection, and it has a *websocket.Conn field. For testing I don't know how to use httptest effectively to set up a dummy connection (if what I'm testing doesn't actually need to send anything over the Conn).

Some advice on how I could use standard httptest to (succinctly) create a *websocket.Conn, or any comment on potentially exporting this package would be appreciated!

@mafredri
Copy link
Member

@leosunmo thanks for sharing your experience.

A fairly simple example of using httptest can be found in the echo example over here:

s := httptest.NewServer(echoServer{

I'll think about how we can help guide users in writing tests better and we will also consider exporting the wstest package. This project has very limited use of wstest.Pipe, however, so I'd love to hear more about use-cases where it has been useful!

@leosunmo
Copy link

leosunmo commented Aug 15, 2024

Thanks for the quick reply and example code!

That approach works really well when I am testing the client part, but less well when I am testing the server part. For example, I want to test how well my server handles a client not responding to the Close handshake. I do something like this:

r := manualWSReq(t, "")
w := httptest.NewRecorder()
buf := bytes.NewBuffer(nil)

conn, err := websocket.Accept(w, r, nil)
if err != nil {
	t.Fatalf("failed to accept ws req: %s", err)
}

which leads to
failed to accept ws req: failed to accept WebSocket connection: http.ResponseWriter does not implement http.Hijacker

This is presumable the same issue as #455.

I want to pass that websocket.Conn I get out of websocket.Accept to some "connection manager" or other internal struct as part of my testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants