• Stars
    star
    102
  • Rank 323,866 (Top 7 %)
  • Language
    Go
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

go websocket client for unit testing of a websocket handler

wstest

Build Status codecov GoDoc Go Report Card

A websocket client for unit-testing a websocket server

The gorilla organization provides full featured websocket implementation that the standard library lacks.

The standard library provides a httptest.ResponseRecorder struct that test an http.Handler without ListenAndServe, but is helpless when the connection is being hijacked by an http upgrader. As for testing websockets, it has the httptest.NewServer that actually listens on a socket on an arbitrary port.

This package provides a NewDialer function to test just the http.Handler that upgrades the connection to a websocket session. It runs the handler function in a goroutine without listening on any port. The returned websocket.Dialer then can be used to dial and communicate with the given handler.

Get

go get -u github.com/posener/wstest

Examples

See the example test.

An example how to modify a test function from using httptest.Server to use wstest.NewDialer function.

func TestHandler(t *testing.T) {
	var err error

	h := &myHandler{}
-	s := httptest.NewServer(h)
-	defer s.Close()
-	d := websocket.Dialer{}
+	d := wstest.NewDialer(h)

-	c, resp, err := d.Dial("ws://" + s.Listener.Addr().String() + "/ws", nil)
+	c, resp, err := d.Dial("ws://" + "whatever" + "/ws", nil)
	if err != nil {
		t.Fatal(err)
	}
	
	if got, want := resp.StatusCode, http.StatusSwitchingProtocols; got != want {
		t.Errorf("resp.StatusCode = %q, want %q", got, want)
	}
	
	err = c.WriteJSON("test")
	if err != nil {
		t.Fatal(err)
	}
}

More Repositories

1

complete

bash completion written in go + bash completion for go command
Go
909
star
2

goreadme

Generate readme file from Go doc. Now available as a Github action!
Go
211
star
3

goaction

Write Github actions in Go
Go
204
star
4

h2conn

HTTP2 client-server full-duplex connection
Go
126
star
5

gitfs

A complete solution for static files in Go code
Go
124
star
6

sharedsecret

Implementation of Shamir's Secret Sharing algorithm.
Go
65
star
7

tarfs

An implementation of the FileSystem interface for tar files.
Go
59
star
8

cmd

The standard library flag package with its missing features
Go
41
star
9

ctxutil

utils for Go context
Go
25
star
10

client-timing

An HTTP client for go-server-timing middleware. Enables automatic timing propagation through HTTP calls between servers.
Go
24
star
11

order

More readable and easier ordering and comparison tasks
Go
21
star
12

mock-import

A helper mocking function to mask ImportErrors
Python
17
star
13

orm

Go Typed ORM
Go
16
star
14

context

A proof of concept implementation of scoped context
Go
16
star
15

h2demo

Code examples for blog post
Go
15
star
16

fuzzing

Easy fuzzing with go-fuzz
Go
14
star
17

script

Easily write scripts with Go. Improvements for https://github.com/bitfield/script.
Go
13
star
18

eztables

iptables in web browser
Go
10
star
19

fcontext

Go Context with (pseudo) constant access-time
Go
10
star
20

auth

Painless OAuth authentication middleware
Go
8
star
21

dont

A towel, is about the most massively useful thing an interstellar hitchhiker can have
Go
6
star
22

tiler

A Go port of https://github.com/nuno-faria/tiler.
Go
6
star
23

flag

Like the flag package, but with bash completion support!
Go
5
star
24

goaction-example

Simplest goaction example
Go
5
star
25

goreadme-server

Github App for goreadme package
Go
4
star
26

formatter

A library for formatting text - indenting and line wrapping
Go
3
star
27

wsbeam

WebSocket HTTP handler that can be used to beam (broadcast) data to all connections
Go
3
star
28

contexttest

Test package for context implementations
Go
3
star
29

chrome-github-godoc

Chrome extension that replaces Github view of git commit messages with useful godoc.org synopsis
JavaScript
3
star
30

tmplt

A small wrapper around Go templates for handling simple templates
Go
2
star
31

learn

Go
2
star
32

goaction-issues-example

Goaction example for using Github APIs
Go
2
star
33

meetups

An archive of my meetup lectures
Go
1
star
34

grpcgw

Convenience comes with grpc-ecosystem/grpc-gateway
Go
1
star
35

go

Go
1
star
36

fsutil

Go
1
star
37

state-logger

A logging tool to log on state changes
Go
1
star
38

ps1

A lightweight script that sets a nice shell prompt
Shell
1
star
39

githubapp

oauth2 Github app authentication client
Go
1
star