Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support User-Agent change and deletion from -H, simplify UI for headers #649

Merged
merged 6 commits into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions fhttp/http_client.go
Expand Up @@ -274,10 +274,19 @@ func (h *HTTPOptions) AddAndValidateExtraHeader(hdr string) error {
}
key := strings.TrimSpace(s[0])
value := strings.TrimSpace(s[1])
if strings.EqualFold(key, "host") {
switch strings.ToLower(key) {
case "host":
log.LogVf("Will be setting special Host header to %s", value)
h.hostOverride = value
} else {
case "user-agent":
if value == "" {
ldemailly marked this conversation as resolved.
Show resolved Hide resolved
log.Infof("User-Agent: header removed.")
h.extraHeaders.Del(key)
} else {
log.LogVf("User-Agent being Set to %s", value)
h.extraHeaders.Set(key, value)
}
default:
log.LogVf("Setting regular extra header %s: %s", key, value)
h.extraHeaders.Add(key, value)
log.Debugf("headers now %+v", h.extraHeaders)
Expand Down
24 changes: 19 additions & 5 deletions fhttp/http_test.go
Expand Up @@ -67,13 +67,22 @@ func TestGetHeaders(t *testing.T) {
if err == nil {
t.Errorf("Expected error for header without value, did not get one")
}
o.ResetHeaders()
o.InitHeaders()
h = o.AllHeaders()
if h.Get("Host") != "" {
t.Errorf("After reset Host header should be nil, got '%v'", h.Get("Host"))
}
if len(h) != 1 {
t.Errorf("Header count mismatch after reset, got %d instead of 1. %+v", len(h), h)
}
// test user-agent delete:
o.AddAndValidateExtraHeader("UsER-AgENT:")
h = o.AllHeaders()
if len(h) != 0 {
t.Errorf("Header count mismatch after reset, got %d instead of 1", len(h))
t.Errorf("Header count mismatch after delete, got %d instead of 0. %+v", len(h), h)
}
if h.Get("User-Agent") != "" {
t.Errorf("User-Agent header should be empty after delete, got '%v'", h.Get("User-Agent"))
}
}

Expand Down Expand Up @@ -1120,6 +1129,11 @@ func TestDebugHandlerSortedHeaders(t *testing.T) {
o.AddAndValidateExtraHeader("CCC: ccc")
o.AddAndValidateExtraHeader("ZZZ: zzz")
o.AddAndValidateExtraHeader("AAA: aaa")
// test that headers usually Add (list) but stay in order of being set
ldemailly marked this conversation as resolved.
Show resolved Hide resolved
o.AddAndValidateExtraHeader("BBB: aa2")
// test that User-Agent is special, only last value is kept - and replaces the default jrpc.UserAgent
o.AddAndValidateExtraHeader("User-Agent: ua1")
o.AddAndValidateExtraHeader("User-Agent: ua2")
client, _ := NewClient(&o)
now := time.Now()
code, data, header := client.Fetch() // used to panic/bug #127
Expand All @@ -1140,13 +1154,13 @@ func TestDebugHandlerSortedHeaders(t *testing.T) {
"Host: localhost:%d\n"+
"Aaa: aaa\n"+
"Accept-Encoding: gzip\n"+
"Bbb: bbb\n"+
"Bbb: bbb,aa2\n"+
"Ccc: ccc\n"+
"Content-Length: 4\n"+
"Content-Type: application/octet-stream\n"+
"User-Agent: %s\n"+
"User-Agent: ua2\n"+
"Zzz: zzz\n\n"+
"body:\n\nabcd\n", a.Port, jrpc.UserAgent)
"body:\n\nabcd\n", a.Port)
if body != expected {
t.Errorf("Get body: %s not as expected: %s", body, expected)
}
Expand Down
1 change: 0 additions & 1 deletion rapi/restHandler.go
Expand Up @@ -266,7 +266,6 @@ func RESTRunHandler(w http.ResponseWriter, r *http.Request) { //nolint:funlen
httpopts := &fhttp.HTTPOptions{}
httpopts.HTTPReqTimeOut = timeout // to be normalized in init 0 replaced by default value
httpopts = httpopts.Init(url)
httpopts.ResetHeaders()
httpopts.DisableFastClient = stdClient
httpopts.SequentialWarmup = sequentialWarmup
httpopts.Insecure = httpsInsecure
Expand Down
7 changes: 1 addition & 6 deletions ui/templates/main.html
Expand Up @@ -53,12 +53,7 @@ <h1>Φορτίο (fortio) v{{.Version}}{{if not .DoLoad}} control UI{{end}}</h1>
No Catch-Up (qps is a ceiling): <input type="checkbox" name="nocatchup" /><br />
Percentiles: <input type="text" name="p" size="20" value="50, 75, 90, 99, 99.9" /> <br />
Histogram Resolution: <input type="text" name="r" size="8" value="0.0001" /> <br />
Headers: <br />
{{ range $name, $vals := .Headers }}{{range $val := $vals}}
<input type="text" name="H" size=40 value="{{$name}}: {{ $val }}" /> <br />
{{end}}{{end}} <!-- 3 extra header lines, TODO(#283): add a JS 'more headers' button -->
<input type="text" name="H" size=40 value="" /> <br />
<input type="text" name="H" size=40 value="" /> <br />
Extra Headers:<br />
<input type="text" name="H" size=40 value="" /> <br />
<button type="button" onclick="addCustomHeader()">+</button>
<br />
Expand Down
5 changes: 1 addition & 4 deletions ui/uihandler.go
Expand Up @@ -185,8 +185,6 @@ func Handler(w http.ResponseWriter, r *http.Request) {
httpopts := &fhttp.HTTPOptions{}
httpopts.HTTPReqTimeOut = timeout // to be normalized in init 0 replaced by default value
httpopts = httpopts.Init(url)
defaultHeaders := httpopts.AllHeaders()
httpopts.ResetHeaders()
httpopts.DisableFastClient = stdClient
httpopts.SequentialWarmup = sequentialWarmup
httpopts.Insecure = httpsInsecure
Expand Down Expand Up @@ -221,7 +219,6 @@ func Handler(w http.ResponseWriter, r *http.Request) {
}
err := mainTemplate.Execute(w, &struct {
R *http.Request
Headers http.Header
Version string
LogoPath string
DebugPath string
Expand All @@ -237,7 +234,7 @@ func Handler(w http.ResponseWriter, r *http.Request) {
DoStop bool
DoLoad bool
}{
r, defaultHeaders, version.Short(), logoPath, debugPath, echoPath, chartJSPath,
r, version.Short(), logoPath, debugPath, echoPath, chartJSPath,
startTime.Format(time.ANSIC), url, labels, runid,
fhttp.RoundDuration(time.Since(startTime)), durSeconds, urlHostPort, mode == stop, mode == run,
})
Expand Down