util/httpm, all: add a test to make sure httpm is used consistently

Updates #cleanup

Change-Id: I7dbf8a02de22fc6b317ab5e29cc97792dd75352c
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
pull/9639/head
Brad Fitzpatrick 1 year ago committed by Brad Fitzpatrick
parent 73e53dcd1c
commit b775a3799e

@ -57,6 +57,7 @@ import (
"golang.org/x/crypto/blake2s" "golang.org/x/crypto/blake2s"
"tailscale.com/net/tshttpproxy" "tailscale.com/net/tshttpproxy"
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/util/httpm"
"tailscale.com/util/must" "tailscale.com/util/must"
) )
@ -335,7 +336,7 @@ func (c *Client) download(ctx context.Context, url, dst string, limit int64) ([]
quickCtx, cancel := context.WithTimeout(ctx, 30*time.Second) quickCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel() defer cancel()
headReq := must.Get(http.NewRequestWithContext(quickCtx, http.MethodHead, url, nil)) headReq := must.Get(http.NewRequestWithContext(quickCtx, httpm.HEAD, url, nil))
res, err := hc.Do(headReq) res, err := hc.Do(headReq)
if err != nil { if err != nil {
@ -349,7 +350,7 @@ func (c *Client) download(ctx context.Context, url, dst string, limit int64) ([]
} }
c.logf("Download size: %v", res.ContentLength) c.logf("Download size: %v", res.ContentLength)
dlReq := must.Get(http.NewRequestWithContext(ctx, http.MethodGet, url, nil)) dlReq := must.Get(http.NewRequestWithContext(ctx, httpm.GET, url, nil))
dlRes, err := hc.Do(dlReq) dlRes, err := hc.Do(dlReq)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err

@ -24,6 +24,7 @@ import (
"tailscale.com/tailcfg" "tailscale.com/tailcfg"
"tailscale.com/util/clientmetric" "tailscale.com/util/clientmetric"
"tailscale.com/util/goroutines" "tailscale.com/util/goroutines"
"tailscale.com/util/httpm"
"tailscale.com/version" "tailscale.com/version"
) )
@ -41,9 +42,9 @@ func (b *LocalBackend) handleC2N(w http.ResponseWriter, r *http.Request) {
w.Write(body) w.Write(body)
case "/update": case "/update":
switch r.Method { switch r.Method {
case http.MethodGet: case httpm.GET:
b.handleC2NUpdateGet(w, r) b.handleC2NUpdateGet(w, r)
case http.MethodPost: case httpm.POST:
b.handleC2NUpdatePost(w, r) b.handleC2NUpdatePost(w, r)
default: default:
http.Error(w, "bad method", http.StatusMethodNotAllowed) http.Error(w, "bad method", http.StatusMethodNotAllowed)

@ -43,6 +43,7 @@ import (
"tailscale.com/types/logger" "tailscale.com/types/logger"
"tailscale.com/types/netmap" "tailscale.com/types/netmap"
"tailscale.com/util/clientmetric" "tailscale.com/util/clientmetric"
"tailscale.com/util/httpm"
"tailscale.com/util/mak" "tailscale.com/util/mak"
"tailscale.com/util/multierr" "tailscale.com/util/multierr"
) )
@ -1752,7 +1753,7 @@ func (ss *sshSession) notifyControl(ctx context.Context, nodeKey key.NodePublic,
return return
} }
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) req, err := http.NewRequestWithContext(ctx, httpm.POST, url, bytes.NewReader(body))
if err != nil { if err != nil {
ss.logf("notifyControl: unable to create request:", err) ss.logf("notifyControl: unable to create request:", err)
return return

@ -0,0 +1,29 @@
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
package httpm
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestUsedConsistently(t *testing.T) {
cmd := exec.Command("git", "grep", "-l", "-F", "http.Method")
dir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
cmd.Dir = filepath.Join(dir, "../..")
matches, _ := cmd.Output()
for _, fn := range strings.Split(strings.TrimSpace(string(matches)), "\n") {
switch fn {
case "util/httpm/httpm.go", "util/httpm/httpm_test.go":
continue
}
t.Errorf("http.MethodFoo constant used in %s; use httpm.FOO instead", fn)
}
}
Loading…
Cancel
Save