Fixing http test using local http mock server

dev
mzack 2021-11-10 18:04:53 +01:00
parent 0e46d3e041
commit 56b7d78fed
1 changed files with 23 additions and 1 deletions

View File

@ -1,12 +1,15 @@
package offlinehttp package offlinehttp
import ( import (
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/http/httptest"
"net/http/httputil" "net/http/httputil"
"testing" "testing"
"time" "time"
"github.com/julienschmidt/httprouter"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -154,11 +157,30 @@ Server: Google Frontend
} }
t.Run("test-live-response-with-content-length", func(t *testing.T) { t.Run("test-live-response-with-content-length", func(t *testing.T) {
var ts *httptest.Server
router := httprouter.New()
router.GET("/", httprouter.Handle(func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Header().Add("Server", "Google Frontend")
fmt.Fprintf(w, "%s", `<!DOCTYPE html>
<html>
<head>
<title>Firing Range</title>
</head>
<body>
<h1>Version 0.48</h1>
<h1>What is the Firing Range?</h1>
<p>
</body>
</html>`)
}))
ts = httptest.NewServer(router)
defer ts.Close()
client := &http.Client{ client := &http.Client{
Timeout: 3 * time.Second, Timeout: 3 * time.Second,
} }
data, err := client.Get("https://golang.org/doc/install") data, err := client.Get(ts.URL)
require.Nil(t, err, "could not dial url") require.Nil(t, err, "could not dial url")
defer data.Body.Close() defer data.Body.Close()