aboutsummaryrefslogtreecommitdiff
path: root/src/net/http
diff options
context:
space:
mode:
authorapocelipes <seve3r@outlook.com>2024-07-24 10:39:48 +0000
committerGopher Robot <gobot@golang.org>2024-07-25 00:20:13 +0000
commit1d717951f518a9e818e8b98d4daed17756c394ca (patch)
tree9d0d3097e9f47c5e89171e15d05706ea55e2c9ee /src/net/http
parent792a26130347c9b9db344ba56f86645679a1a9d9 (diff)
downloadgo-1d717951f518a9e818e8b98d4daed17756c394ca.tar.xz
net: use slices and maps to clean up tests
Replace reflect.DeepEqual with slices.Equal/maps.Equal, which is much faster. Change-Id: I54600fb63a56460c11d3d5af9072da585e31b1a2 GitHub-Last-Rev: 08c1445ad5be94d071e8ceb4b060b8f4ab0d77ba GitHub-Pull-Request: golang/go#67606 Reviewed-on: https://go-review.googlesource.com/c/go/+/587816 Reviewed-by: Ian Lance Taylor <iant@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/net/http')
-rw-r--r--src/net/http/cgi/host_test.go4
-rw-r--r--src/net/http/clientserver_test.go2
-rw-r--r--src/net/http/fs_test.go6
-rw-r--r--src/net/http/http_test.go4
-rw-r--r--src/net/http/httputil/reverseproxy_test.go4
-rw-r--r--src/net/http/request_test.go17
-rw-r--r--src/net/http/serve_test.go7
-rw-r--r--src/net/http/sniff_test.go4
-rw-r--r--src/net/http/transport_test.go7
9 files changed, 29 insertions, 26 deletions
diff --git a/src/net/http/cgi/host_test.go b/src/net/http/cgi/host_test.go
index 7fe0e6257d..8ecfa19f6b 100644
--- a/src/net/http/cgi/host_test.go
+++ b/src/net/http/cgi/host_test.go
@@ -16,9 +16,9 @@ import (
"net/http/httptest"
"os"
"path/filepath"
- "reflect"
"regexp"
"runtime"
+ "slices"
"strings"
"testing"
"time"
@@ -510,7 +510,7 @@ func TestRemoveLeadingDuplicates(t *testing.T) {
}
for _, tt := range tests {
got := removeLeadingDuplicates(tt.env)
- if !reflect.DeepEqual(got, tt.want) {
+ if !slices.Equal(got, tt.want) {
t.Errorf("removeLeadingDuplicates(%q) = %q; want %q", tt.env, got, tt.want)
}
}
diff --git a/src/net/http/clientserver_test.go b/src/net/http/clientserver_test.go
index 3dc440dde1..0c2142a063 100644
--- a/src/net/http/clientserver_test.go
+++ b/src/net/http/clientserver_test.go
@@ -274,7 +274,7 @@ func testChunkedResponseHeaders(t *testing.T, mode testMode) {
if mode == http2Mode {
wantTE = nil
}
- if !reflect.DeepEqual(res.TransferEncoding, wantTE) {
+ if !slices.Equal(res.TransferEncoding, wantTE) {
t.Errorf("TransferEncoding = %v; want %v", res.TransferEncoding, wantTE)
}
if got, haveCL := res.Header["Content-Length"]; haveCL {
diff --git a/src/net/http/fs_test.go b/src/net/http/fs_test.go
index 2ffffbf0b3..3149ca35ac 100644
--- a/src/net/http/fs_test.go
+++ b/src/net/http/fs_test.go
@@ -24,9 +24,9 @@ import (
"os/exec"
"path"
"path/filepath"
- "reflect"
"regexp"
"runtime"
+ "slices"
"strconv"
"strings"
"testing"
@@ -516,7 +516,7 @@ func testServeFileContentType(t *testing.T, mode testMode) {
if err != nil {
t.Fatal(err)
}
- if h := resp.Header["Content-Type"]; !reflect.DeepEqual(h, want) {
+ if h := resp.Header["Content-Type"]; !slices.Equal(h, want) {
t.Errorf("Content-Type mismatch: got %v, want %v", h, want)
}
resp.Body.Close()
@@ -1448,7 +1448,7 @@ func TestFileServerCleanPath(t *testing.T) {
rr := httptest.NewRecorder()
req, _ := NewRequest("GET", "http://foo.localhost"+tt.path, nil)
FileServer(fileServerCleanPathDir{&log}).ServeHTTP(rr, req)
- if !reflect.DeepEqual(log, tt.wantOpen) {
+ if !slices.Equal(log, tt.wantOpen) {
t.Logf("For %s: Opens = %q; want %q", tt.path, log, tt.wantOpen)
}
if rr.Code != tt.wantCode {
diff --git a/src/net/http/http_test.go b/src/net/http/http_test.go
index 2e7e024e20..df9812fc94 100644
--- a/src/net/http/http_test.go
+++ b/src/net/http/http_test.go
@@ -12,8 +12,8 @@ import (
"io/fs"
"net/url"
"os"
- "reflect"
"regexp"
+ "slices"
"strings"
"testing"
)
@@ -41,7 +41,7 @@ func TestForeachHeaderElement(t *testing.T) {
foreachHeaderElement(tt.in, func(v string) {
got = append(got, v)
})
- if !reflect.DeepEqual(got, tt.want) {
+ if !slices.Equal(got, tt.want) {
t.Errorf("foreachHeaderElement(%q) = %q; want %q", tt.in, got, tt.want)
}
}
diff --git a/src/net/http/httputil/reverseproxy_test.go b/src/net/http/httputil/reverseproxy_test.go
index eac8b7ec81..67d0e50593 100644
--- a/src/net/http/httputil/reverseproxy_test.go
+++ b/src/net/http/httputil/reverseproxy_test.go
@@ -205,7 +205,7 @@ func TestReverseProxyStripHeadersPresentInConnection(t *testing.T) {
slices.Sort(cf)
expectedValues := []string{"Upgrade", someConnHeader, fakeConnectionToken}
slices.Sort(expectedValues)
- if !reflect.DeepEqual(cf, expectedValues) {
+ if !slices.Equal(cf, expectedValues) {
t.Errorf("handler modified header %q = %q; want %q", "Connection", cf, expectedValues)
}
}))
@@ -765,7 +765,7 @@ func TestReverseProxyGetPutBuffer(t *testing.T) {
wantLog := []string{"getBuf", "putBuf-" + strconv.Itoa(size)}
mu.Lock()
defer mu.Unlock()
- if !reflect.DeepEqual(log, wantLog) {
+ if !slices.Equal(log, wantLog) {
t.Errorf("Log events = %q; want %q", log, wantLog)
}
}
diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go
index 9b6eb6e1a8..37b888313d 100644
--- a/src/net/http/request_test.go
+++ b/src/net/http/request_test.go
@@ -23,6 +23,7 @@ import (
"os"
"reflect"
"regexp"
+ "slices"
"strings"
"testing"
)
@@ -69,22 +70,22 @@ func TestParseFormQuery(t *testing.T) {
if bz := req.PostFormValue("z"); bz != "post" {
t.Errorf(`req.PostFormValue("z") = %q, want "post"`, bz)
}
- if qs := req.Form["q"]; !reflect.DeepEqual(qs, []string{"foo", "bar"}) {
+ if qs := req.Form["q"]; !slices.Equal(qs, []string{"foo", "bar"}) {
t.Errorf(`req.Form["q"] = %q, want ["foo", "bar"]`, qs)
}
- if both := req.Form["both"]; !reflect.DeepEqual(both, []string{"y", "x"}) {
+ if both := req.Form["both"]; !slices.Equal(both, []string{"y", "x"}) {
t.Errorf(`req.Form["both"] = %q, want ["y", "x"]`, both)
}
if prio := req.FormValue("prio"); prio != "2" {
t.Errorf(`req.FormValue("prio") = %q, want "2" (from body)`, prio)
}
- if orphan := req.Form["orphan"]; !reflect.DeepEqual(orphan, []string{"", "nope"}) {
+ if orphan := req.Form["orphan"]; !slices.Equal(orphan, []string{"", "nope"}) {
t.Errorf(`req.FormValue("orphan") = %q, want "" (from body)`, orphan)
}
- if empty := req.Form["empty"]; !reflect.DeepEqual(empty, []string{"", "not"}) {
+ if empty := req.Form["empty"]; !slices.Equal(empty, []string{"", "not"}) {
t.Errorf(`req.FormValue("empty") = %q, want "" (from body)`, empty)
}
- if nokey := req.Form[""]; !reflect.DeepEqual(nokey, []string{"nokey"}) {
+ if nokey := req.Form[""]; !slices.Equal(nokey, []string{"nokey"}) {
t.Errorf(`req.FormValue("nokey") = %q, want "nokey" (from body)`, nokey)
}
}
@@ -765,7 +766,7 @@ func TestRequestWriteBufferedWriter(t *testing.T) {
"User-Agent: " + DefaultUserAgent + "\r\n",
"\r\n",
}
- if !reflect.DeepEqual(got, want) {
+ if !slices.Equal(got, want) {
t.Errorf("Writes = %q\n Want = %q", got, want)
}
}
@@ -785,7 +786,7 @@ func TestRequestBadHostHeader(t *testing.T) {
"User-Agent: " + DefaultUserAgent + "\r\n",
"\r\n",
}
- if !reflect.DeepEqual(got, want) {
+ if !slices.Equal(got, want) {
t.Errorf("Writes = %q\n Want = %q", got, want)
}
}
@@ -804,7 +805,7 @@ func TestRequestBadUserAgent(t *testing.T) {
"User-Agent: evil X-Evil: evil\r\n",
"\r\n",
}
- if !reflect.DeepEqual(got, want) {
+ if !slices.Equal(got, want) {
t.Errorf("Writes = %q\n Want = %q", got, want)
}
}
diff --git a/src/net/http/serve_test.go b/src/net/http/serve_test.go
index b2858ba8f2..cc485d3b89 100644
--- a/src/net/http/serve_test.go
+++ b/src/net/http/serve_test.go
@@ -34,6 +34,7 @@ import (
"reflect"
"regexp"
"runtime"
+ "slices"
"strconv"
"strings"
"sync"
@@ -4010,7 +4011,7 @@ func testHTTP10ConnectionHeader(t *testing.T, mode testMode) {
resp.Body.Close()
got := resp.Header["Connection"]
- if !reflect.DeepEqual(got, tt.expect) {
+ if !slices.Equal(got, tt.expect) {
t.Errorf("wrong Connection headers for request %q. Got %q expect %q", tt.req, got, tt.expect)
}
}
@@ -4329,7 +4330,7 @@ func testServerConnState(t *testing.T, mode testMode) {
<-complete
sl := <-activeLog
- if !reflect.DeepEqual(sl.got, sl.want) {
+ if !slices.Equal(sl.got, sl.want) {
t.Errorf("Request(s) produced unexpected state sequence.\nGot: %v\nWant: %v", sl.got, sl.want)
}
// Don't return sl to activeLog: we don't expect any further states after
@@ -4355,7 +4356,7 @@ func testServerConnState(t *testing.T, mode testMode) {
return
}
sl.got = append(sl.got, state)
- if sl.complete != nil && (len(sl.got) >= len(sl.want) || !reflect.DeepEqual(sl.got, sl.want[:len(sl.got)])) {
+ if sl.complete != nil && (len(sl.got) >= len(sl.want) || !slices.Equal(sl.got, sl.want[:len(sl.got)])) {
close(sl.complete)
sl.complete = nil
}
diff --git a/src/net/http/sniff_test.go b/src/net/http/sniff_test.go
index d6ef40905e..68c8a6af1e 100644
--- a/src/net/http/sniff_test.go
+++ b/src/net/http/sniff_test.go
@@ -10,7 +10,7 @@ import (
"io"
"log"
. "net/http"
- "reflect"
+ "slices"
"strconv"
"strings"
"testing"
@@ -144,7 +144,7 @@ func testServerIssue5953(t *testing.T, mode testMode) {
got := resp.Header["Content-Type"]
want := []string{""}
- if !reflect.DeepEqual(got, want) {
+ if !slices.Equal(got, want) {
t.Errorf("Content-Type = %q; want %q", got, want)
}
resp.Body.Close()
diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go
index ae7159dab0..2389284249 100644
--- a/src/net/http/transport_test.go
+++ b/src/net/http/transport_test.go
@@ -36,6 +36,7 @@ import (
"os"
"reflect"
"runtime"
+ "slices"
"strconv"
"strings"
"sync"
@@ -4453,7 +4454,7 @@ func TestTransportFlushesBodyChunks(t *testing.T) {
"5\r\nnum2\n\r\n",
"0\r\n\r\n",
}
- if !reflect.DeepEqual(lw.writes, want) {
+ if !slices.Equal(lw.writes, want) {
t.Errorf("Writes differed.\n Got: %q\nWant: %q\n", lw.writes, want)
}
}
@@ -5284,7 +5285,7 @@ func testTransportMaxIdleConns(t *testing.T, mode testMode) {
"|http|host-2.dns-is-faked.golang:" + port,
"|http|host-3.dns-is-faked.golang:" + port,
}
- if got := tr.IdleConnKeysForTesting(); !reflect.DeepEqual(got, want) {
+ if got := tr.IdleConnKeysForTesting(); !slices.Equal(got, want) {
t.Fatalf("idle conn keys mismatch.\n got: %q\nwant: %q\n", got, want)
}
@@ -5296,7 +5297,7 @@ func testTransportMaxIdleConns(t *testing.T, mode testMode) {
"|http|host-3.dns-is-faked.golang:" + port,
"|http|host-4.dns-is-faked.golang:" + port,
}
- if got := tr.IdleConnKeysForTesting(); !reflect.DeepEqual(got, want) {
+ if got := tr.IdleConnKeysForTesting(); !slices.Equal(got, want) {
t.Fatalf("idle conn keys mismatch after 5th host.\n got: %q\nwant: %q\n", got, want)
}
}