go websocket client for unit testing of a websocket handler

Overview

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)
	}
}
You might also like...
Belajar golang unit test

perintah eksekusi di root folder : go test -v ./... assertion ambil dari framewo

siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.
siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.

siusiu (suite-suite harmonics) a suite used to manage the suite, designed to free penetration testing engineers from learning and using various security tools, reducing the time and effort spent by penetration testing engineers on installing tools, remembering how to use tools.

A yaml data-driven testing format together with golang testing library

Specimen Yaml-based data-driven testing Specimen is a yaml data format for data-driven testing. This enforces separation between feature being tested

Golang HTTP client testing framework

flute Golang HTTP client testing framework Presentation https://speakerdeck.com/szksh/flute-golang-http-client-testing-framework Overview flute is the

A WebDriver client and acceptance testing library for Go
A WebDriver client and acceptance testing library for Go

Agouti Agouti is a library for writing browser-based acceptance tests in Google Go. It provides Gomega matchers and plays nicely with Ginkgo or Spec.

mock server to aid testing the jaguar-java client API

stripe-mock stripe-mock is a mock HTTP server that responds like the real Stripe API. It can be used instead of Stripe's test mode to make test suites

Client tool for testing HTTP server timeouts

HTTP timeout test client While testing Go HTTP server timeouts I wrote this little tool to help me test. It allows for slowing down header write and b

HttpRunner 是一个开源的 API 测试工具,支持 HTTP(S) / HTTP2 / WebSocket / RPC 等网络协议,涵盖接口测试、性能测试、数字体验监测等测试类型。简单易用,功能强大,具有丰富的插件化机制和高度的可扩展能力。
HttpRunner 是一个开源的 API 测试工具,支持 HTTP(S) / HTTP2 / WebSocket / RPC 等网络协议,涵盖接口测试、性能测试、数字体验监测等测试类型。简单易用,功能强大,具有丰富的插件化机制和高度的可扩展能力。

HttpRunner HttpRunner 是一个开源的 API 测试工具,支持 HTTP(S)/HTTP2/WebSocket/RPC 等网络协议,涵盖接口测试、性能测试、数字体验监测等测试类型。简单易用,功能强大,具有丰富的插件化机制和高度的可扩展能力。 HttpRunner 用户调研问卷 持续

Fortio load testing library, command line tool, advanced echo server and web UI in go (golang). Allows to specify a set query-per-second load and record latency histograms and other useful stats.
Fortio load testing library, command line tool, advanced echo server and web UI in go (golang). Allows to specify a set query-per-second load and record latency histograms and other useful stats.

Fortio Fortio (Φορτίο) started as, and is, Istio's load testing tool and now graduated to be its own project. Fortio is also used by, among others, Me

Comments
  • go get with this repo is broken on pre-go-1.10 machines

    go get with this repo is broken on pre-go-1.10 machines

    Due to Golang's insane lack of semantic versioning, this repo is now broken for our codebase, which uses go 1.8. The Gophers suggest baking the version number of the tool into the repo name, and creating new repos for each major version of the tool.

    Would it be possible to create a wstest-v2 or wstest-go-1-10 repo for the new go 1.10-related changes, and leave this repo with the original API? To work around this, I had to fork the repo and roll back the last 6 commits to get it back to a compatible state.

    I understand that this is a lot of work, but this repo simply no longer works for anything other than go 1.10 without using a third-party package manager.

    opened by iamtheddrman 4
  • clear write deadline after write to prevent connection state corruption

    clear write deadline after write to prevent connection state corruption

    The problem that I see is that the state is corrupted after the deadline is reached, even if writing has completed successfully, and can't be used, even if a new deadline is set before writing is attempted again. I don't believe this is the correct behavior for WriteDeadline. My fix may not be correct either, but it served my purpose for now, and was quick.

    opened by trey-jones 3
  • Races

    Races

    • Fixes rare race conditions found in pipe testing suite
    • Add docs
    • Nicer logger function
    • Fix some bugs in code that were found after fixing the pipe package.
    • Travis: increase the amount of tests to have higher robustness of the pipe package (bugs that were found only once in +/-10 runs)
    opened by posener 1
  • Client should handle connection errors

    Client should handle connection errors

    If a client fail to connect (wrong path or scheme for example) it is stuck. The Connect method should return the right error for those kind of scenarios.

    bug 
    opened by posener 0
Releases(v1.2.0)
Owner
Eyal Posener
Eyal Posener
A mock of Go's net package for unit/integration testing

netmock: Simulate Go network connections netmock is a Go package for simulating net connections, including delays and disconnects. This is work in pro

Lucas Wolf 1 Oct 27, 2021
Go Unit Testing Clean Arch

Golang Unit Testing Tutorial melakukan unit testing di Golang yang sudah menerapkan clean architecture Menjalankan Service PSQL_HOST=<IP Database Serv

Edward Suwirya 2 Feb 12, 2022
Vault mock - Mock of Hashicorp Vault used for unit testing

vault_mock Mock of Hashicorp Vault used for unit testing Notice This is a person

Elliot Rotenstein 0 Jan 19, 2022
Easier way to unit test terraform

Unit testing terraform (WIP) Disclaimer Currently, the only way to compare values is using JSON query path and all types are strings. want := terraf

Thiago Nache Carvalho 51 Aug 16, 2022
gostub is a library to make stubbing in unit tests easy

gostub gostub is a library to make stubbing in unit tests easy. Getting started Import the following package: github.com/prashantv/gostub Click here t

Prashant Varanasi 265 Dec 28, 2022
gomonkey is a library to make monkey patching in unit tests easy

gomonkey is a library to make monkey patching in unit tests easy, and the core idea of monkey patching comes from Bouke, you can read this blogpost for an explanation on how it works.

Zhang Xiaolong 1.4k Jan 4, 2023
How we can run unit tests in parallel mode with failpoint injection taking effect and without injection race

This is a simple demo to show how we can run unit tests in parallel mode with failpoint injection taking effect and without injection race. The basic

amyangfei 1 Oct 31, 2021
A simple yet intuitive golang unit test framework.

gotest Intuitive and simple golang testing framework which helps in writing unit tests in a way which improves the readability of the test. Here is an

null 1 Jan 1, 2022
Benchmarking deferent Fibonacci functions and algorithms with running unit test

GoFibonacciBench Benchmarking deferent Fibonacci functions and algorithms with running unit test ... Introduction: Fibonacci numbers are special kinds

null 2 Feb 27, 2022
This repository includes consumer driven contract test for provider, unit test and counter api.

This repository includes consumer driven contract test for provider, unit test and counter api.

Ahmet Zümberoğlu 0 Feb 1, 2022