Skip to content

How to start http server in background before running the test ? #1080

Answered by ysmood
noor-tg asked this question in Q&A
Discussion options

You must be logged in to vote

It depends how your http server is designed, such as:

package main_test

import (
	"net/http"
	"testing"

	"github.com/go-chi/chi/v5"
	"github.com/ysmood/got"
)

func TestServe(t *testing.T) {
	g := got.T(t)

	router := chi.NewRouter()

	router.Get("/home", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("welcome"))
	})

	r := g.Serve()
	r.Server.Handler = router

	g.Eq(g.Req("GET", r.URL("/home")).String(), "welcome")
}

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@noor-tg
Comment options

@ysmood
Comment options

Answer selected by noor-tg
@noor-tg
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants