check / check (push) Canceled after 0s
Go's server adds a Content-Type it guesses from the body when the response's headers have no Content-Type key, which happened to some of the app's answers and not others, as the first part of the body or the headers went out first. When the app sends none, smallwebwaf now sets the key with no value on the client's response, which sends none. Model: opus-5-5
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package proxy
|
|
|
|
import (
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
)
|
|
|
|
func TestAnAnswerWithoutAContentTypeGetsNone(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(
|
|
func(w http.ResponseWriter, _ *http.Request) {
|
|
rq := &request{
|
|
h: &handler{config: &config.Config{}},
|
|
out: &responseWriter{ResponseWriter: w},
|
|
}
|
|
app := &http.Response{
|
|
StatusCode: http.StatusOK,
|
|
Header: http.Header{},
|
|
Body: http.NoBody,
|
|
}
|
|
|
|
err := rq.modifyResponse(app)
|
|
if err != nil {
|
|
t.Errorf("modifyResponse: %v", err)
|
|
}
|
|
|
|
// The headers go out with this, the first part of the body,
|
|
// which Go's server would guess a Content-Type from.
|
|
_, _ = io.WriteString(w, "<!doctype html><title>page</title>")
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
req, err := http.NewRequestWithContext(t.Context(), http.MethodGet, srv.URL,
|
|
http.NoBody)
|
|
if err != nil {
|
|
t.Fatalf("new request: %v", err)
|
|
}
|
|
|
|
res, err := srv.Client().Do(req)
|
|
if err != nil {
|
|
t.Fatalf("get: %v", err)
|
|
}
|
|
|
|
_ = res.Body.Close()
|
|
|
|
if res.Header.Values("Content-Type") != nil {
|
|
t.Errorf("client got Content-Type %q, want none",
|
|
res.Header.Get("Content-Type"))
|
|
}
|
|
}
|