Created
June 23, 2023 14:56
-
-
Save craigfurman/827bfe35c9b0711e77cf49687f936d4a to your computer and use it in GitHub Desktop.
Does golang's net/http.Client set a default Host header? Nope!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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