Skip to content

Latest commit

 

History

7 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

testserv - Control a test HTTP server with simple instructions

Build Status Go Report Card PkgGoDev

Getting Started

Install testserv:

$ go get github.com/gogama/testserv

Import the testserv package and create a test HTTP server to start testing!

package main

import (
	"fmt"
	"io/ioutil"
	"net/http/httptest"
	"testing"
	"time"

	"github.com/gogama/testserv"
)

func TestMyThing(t *testing.T) {
	server := httptest.NewServer(&testserv.Handler{
		Inst: []testserv.Instruction{
			// Server will delay half a second, then return a 200 OK response
			// containing the given body.
			{
				HeaderDelay: 500*time.Millisecond,
				StatusCode: 200,
				Body: []byte(`{}`),
			},
		},
	})
	client := server.Client()

	resp, err := client.Get(server.URL)
	if err != nil {
		fmt.Printf("Error sending request: %s\n", err)
		return
	}

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Printf("Error reading body: %s\n", err)
		return
	}

	fmt.Printf("StatusCode: %d\n", resp.StatusCode)
	// 200
	fmt.Printf("Body: %s\n", body)
	// {}
}

About

A test HTTP server you can control with simple instructions

Resources

Stars

2 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages