Skip to content

Instantly share code, notes, and snippets.

@craigfurman
Created June 23, 2023 14:56
Show Gist options
  • Save craigfurman/827bfe35c9b0711e77cf49687f936d4a to your computer and use it in GitHub Desktop.
Save craigfurman/827bfe35c9b0711e77cf49687f936d4a to your computer and use it in GitHub Desktop.
Does golang's net/http.Client set a default Host header? Nope!
package headers
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestHostHeader(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
require.Equal(t, "localhost", req.Header.Get("Host"))
w.WriteHeader(204)
})
server := httptest.NewServer(handler)
resp, err := server.Client().Get(server.URL + "/whatever")
require.NoError(t, err)
require.Equal(t, 204, resp.StatusCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment