• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 12 years ago
  • Updated over 4 years ago

Reviews

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

Repository Details

Painless http testing in Go

testflight

Build Status

Installation

go get github.com/drewolson/testflight
import "github.com/drewolson/testflight"

Usage

testflight makes it simple to test your http servers in Go. Suppose you're using pat to create a simple http handler, like so:

func Handler() http.Handler {
	m := pat.New()

	m.Get("/hello/:name", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		io.WriteString(w, "hello, "+req.URL.Query().Get(":name"))
	}))

	m.Post("/post/form", http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
		req.ParseForm()
		name := req.Form.Get("name")
		w.WriteHeader(201)
		io.WriteString(w, name+" created")
	}))

	return m
}

Let's use testflight to test our handler. Keep in mind that testflight is doing full-stack http tests. We're also using assert for test assertions.

func TestGet(t *testing.T) {
	testflight.WithServer(Handler(), func(r *testflight.Requester) {
		response := r.Get("/hello/drew")

		assert.Equal(t, 200, response.StatusCode)
		assert.Equal(t, "hello, drew", response.Body)
	})
}

func TestPostWithForm(t *testing.T) {
	testflight.WithServer(Handler(), func(r *testflight.Requester) {
		response := r.Post("/post/form", testflight.FORM_ENCODED, "name=Drew")

		assert.Equal(t, 201, response.StatusCode)
		assert.Equal(t, "Drew created", response.Body)
	})
}

The testflight.Requester struct has the following methods: Get, Post, Put, Delete and Do. Do accepts an *http.Request for times when you need more explicit control of your request. See testflight_test.go for more usage information.

Testing Websockets

Testflight also allows you to perform full-stack testing of websockets. You'll want to import both the testflight and testflight/ws packages.

import (
    "github.com/drewolson/testflight"
    "github.com/drewolson/testflight/ws"
)

Now, let's make a handler with a websocket route.

func Handler() http.Handler {
	mux := http.NewServeMux()

	mux.Handle("/websocket", websocket.Handler(func(ws *websocket.Conn) {
		var name string
		websocket.Message.Receive(ws, &name)
		websocket.Message.Send(ws, "Hello, "+name)
	}))

	return mux
}

Finally, let's write the test.

func TestWebSocket(t *testing.T) {
    testflight.WithServer(Handler(), func(r *testflight.Requester) {
        connection := ws.Connect(r, "/websocket")

        connection.SendMessage("Drew")
        message, _ := connection.ReceiveMessage()
        assert.Equal(t, "Hello, Drew", message)
    })
}

Contributing

Requires go >= 1.11.

Run the tests.

go test -v ./...

Now write new tests, fix them and send me a pull request!

More Repositories

1

ex_spec

BDD-like syntax for ExUnit
Elixir
99
star
2

vim_dotfiles

my vim dotfiles
Vim Script
43
star
3

ruby_focused_unit_test_vim

run a focused ruby unit test in vim
Vim Script
40
star
4

blox

Example app using Phoenix and Ecto. Obviously it's a blog. An ugly one.
Elixir
19
star
5

pipe

RSS filtering and aggregation
Ruby
17
star
6

clip

A CLI option parser for Gleam
Gleam
17
star
7

epicbot

A Slack Bot for Epic Card Game
PureScript
12
star
8

dotfiles

dotfiles
Shell
12
star
9

diggr

ruby wrapper for the digg api with nice syntax
Ruby
9
star
10

def_macro

macros for ruby
Ruby
5
star
11

purescript-biscotti-cookie

Utilities for parsing and generating cookie headers in PureScript
PureScript
5
star
12

aoc-hs

Advent of Code in Haskell
Haskell
5
star
13

geologist

talk to your minecraft server with http
JavaScript
4
star
14

gnodego

A node wrapper around GNU Go
CoffeeScript
3
star
15

epicbot-hs

A Slack Bot for Epic Card Game -- in Haskell!
Haskell
3
star
16

params_cleaner

Don't use this, use strong parameters
Ruby
3
star
17

bridge-diagrams

Generate plain text bridge diagrams
Haskell
3
star
18

effectful-scotty-example

A simple example application using effectful and scotty
Haskell
2
star
19

purescript-biscotti-session

PureScript Session Management
PureScript
2
star
20

block-chainable

easier ruby DSLs with blocks
Ruby
2
star
21

advent_of_code_2016

Advent of Code 2016 in Elixir
Elixir
2
star
22

aoc-ocaml

Advent of Code in OCaml
OCaml
2
star
23

aoc-gleam

Advent of Code in Gleam
Gleam
1
star
24

concur-testing

Dhall
1
star
25

pie-highlight.vim

Vim Script
1
star
26

uofc-graphql

Python
1
star
27

advent_of_code_2017

Advent of Code 2017
Elixir
1
star
28

timebomb

make your cucumber step definitions time aware
Ruby
1
star
29

vimux-elixir-test

Run ExUnit tests in vimux
Vim Script
1
star
30

ctf_3_solutions

Solutions to Stripe CTF 3
Go
1
star
31

purescript-httpure-contrib-biscotti

Helpers for using Biscotti Cookies and Sessions with HTTPure applications
PureScript
1
star
32

purescript-deques

PureScript Deques
PureScript
1
star