2020-06-05 13:15:07 +02:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
// returns the "%p" representation of the thing passed in
|
build: modernize Go usage
This commit modernizes Go usage. This was done with:
go run golang.org/x/tools/gopls/internal/analysis/modernize/cmd/modernize@latest -fix -test ./...
Then files needed to be `go fmt`ed and a few comments needed to be
restored.
The modernizations include replacing
- if/else conditional assignment by a call to the built-in min or max functions added in go1.21
- sort.Slice(x, func(i, j int) bool) { return s[i] < s[j] } by a call to slices.Sort(s), added in go1.21
- interface{} by the 'any' type added in go1.18
- append([]T(nil), s...) by slices.Clone(s) or slices.Concat(s), added in go1.21
- loop around an m[k]=v map update by a call to one of the Collect, Copy, Clone, or Insert functions from the maps package, added in go1.21
- []byte(fmt.Sprintf...) by fmt.Appendf(nil, ...), added in go1.19
- append(s[:i], s[i+1]...) by slices.Delete(s, i, i+1), added in go1.21
- a 3-clause for i := 0; i < n; i++ {} loop by for i := range n {}, added in go1.22
2025-02-26 22:08:12 +01:00
|
|
|
func ptr(p any) string {
|
2020-06-05 13:15:07 +02:00
|
|
|
return fmt.Sprintf("%p", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetDefaults(t *testing.T) {
|
|
|
|
old := http.DefaultTransport.(*http.Transport)
|
|
|
|
newT := new(http.Transport)
|
|
|
|
SetDefaults(newT, old)
|
|
|
|
// Can't use assert.Equal or reflect.DeepEqual for this as it has functions in
|
|
|
|
// Check functions by comparing the "%p" representations of them
|
|
|
|
assert.Equal(t, ptr(old.Proxy), ptr(newT.Proxy), "when checking .Proxy")
|
|
|
|
assert.Equal(t, ptr(old.DialContext), ptr(newT.DialContext), "when checking .DialContext")
|
2022-08-21 16:47:55 +02:00
|
|
|
assert.Equal(t, ptr(old.DialTLSContext), ptr(newT.DialTLSContext), "when checking .DialTLSContext")
|
2020-06-05 13:15:07 +02:00
|
|
|
assert.Equal(t, old.TLSClientConfig, newT.TLSClientConfig, "when checking .TLSClientConfig")
|
|
|
|
assert.Equal(t, old.TLSHandshakeTimeout, newT.TLSHandshakeTimeout, "when checking .TLSHandshakeTimeout")
|
|
|
|
assert.Equal(t, old.DisableKeepAlives, newT.DisableKeepAlives, "when checking .DisableKeepAlives")
|
|
|
|
assert.Equal(t, old.DisableCompression, newT.DisableCompression, "when checking .DisableCompression")
|
|
|
|
assert.Equal(t, old.MaxIdleConns, newT.MaxIdleConns, "when checking .MaxIdleConns")
|
|
|
|
assert.Equal(t, old.MaxIdleConnsPerHost, newT.MaxIdleConnsPerHost, "when checking .MaxIdleConnsPerHost")
|
|
|
|
assert.Equal(t, old.IdleConnTimeout, newT.IdleConnTimeout, "when checking .IdleConnTimeout")
|
|
|
|
assert.Equal(t, old.ResponseHeaderTimeout, newT.ResponseHeaderTimeout, "when checking .ResponseHeaderTimeout")
|
|
|
|
assert.Equal(t, old.ExpectContinueTimeout, newT.ExpectContinueTimeout, "when checking .ExpectContinueTimeout")
|
|
|
|
assert.Equal(t, old.TLSNextProto, newT.TLSNextProto, "when checking .TLSNextProto")
|
|
|
|
assert.Equal(t, old.MaxResponseHeaderBytes, newT.MaxResponseHeaderBytes, "when checking .MaxResponseHeaderBytes")
|
|
|
|
}
|
|
|
|
|
|
|
|
type aType struct {
|
|
|
|
Matching string
|
|
|
|
OnlyA string
|
|
|
|
MatchingInt int
|
|
|
|
DifferentType string
|
|
|
|
}
|
|
|
|
|
|
|
|
type bType struct {
|
|
|
|
Matching string
|
|
|
|
OnlyB string
|
|
|
|
MatchingInt int
|
|
|
|
DifferentType int
|
|
|
|
Unused string
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetFrom(t *testing.T) {
|
|
|
|
a := aType{
|
|
|
|
Matching: "a",
|
|
|
|
OnlyA: "onlyA",
|
|
|
|
MatchingInt: 1,
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 02:17:24 +02:00
|
|
|
DifferentType: "surprise",
|
2020-06-05 13:15:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
b := bType{
|
|
|
|
Matching: "b",
|
|
|
|
OnlyB: "onlyB",
|
|
|
|
MatchingInt: 2,
|
|
|
|
DifferentType: 7,
|
|
|
|
Unused: "Ha",
|
|
|
|
}
|
|
|
|
bBefore := b
|
|
|
|
|
|
|
|
SetFrom(&a, &b)
|
|
|
|
|
|
|
|
assert.Equal(t, aType{
|
|
|
|
Matching: "b",
|
|
|
|
OnlyA: "onlyA",
|
|
|
|
MatchingInt: 2,
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 02:17:24 +02:00
|
|
|
DifferentType: "surprise",
|
2020-06-05 13:15:07 +02:00
|
|
|
}, a)
|
|
|
|
|
|
|
|
assert.Equal(t, bBefore, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSetFromReversed(t *testing.T) {
|
|
|
|
a := aType{
|
|
|
|
Matching: "a",
|
|
|
|
OnlyA: "onlyA",
|
|
|
|
MatchingInt: 1,
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 02:17:24 +02:00
|
|
|
DifferentType: "surprise",
|
2020-06-05 13:15:07 +02:00
|
|
|
}
|
|
|
|
aBefore := a
|
|
|
|
|
|
|
|
b := bType{
|
|
|
|
Matching: "b",
|
|
|
|
OnlyB: "onlyB",
|
|
|
|
MatchingInt: 2,
|
|
|
|
DifferentType: 7,
|
|
|
|
Unused: "Ha",
|
|
|
|
}
|
|
|
|
|
|
|
|
SetFrom(&b, &a)
|
|
|
|
|
|
|
|
assert.Equal(t, bType{
|
|
|
|
Matching: "a",
|
|
|
|
OnlyB: "onlyB",
|
|
|
|
MatchingInt: 1,
|
|
|
|
DifferentType: 7,
|
|
|
|
Unused: "Ha",
|
|
|
|
}, b)
|
|
|
|
|
|
|
|
assert.Equal(t, aBefore, a)
|
|
|
|
}
|