From 20071db575c75530f6611308248f42900d58ad6d Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Thu, 28 May 2020 10:56:12 -0500 Subject: [PATCH 01/10] Adds Truncate function --- stringutil/stringutil.go | 13 +++++++++++++ stringutil/stringutil_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/stringutil/stringutil.go b/stringutil/stringutil.go index 37c109b..fa3d3aa 100644 --- a/stringutil/stringutil.go +++ b/stringutil/stringutil.go @@ -7,6 +7,19 @@ import ( "unicode" ) +// Left returns the "n" left characters of the string. +func Truncate(s string, n int) string { + if n <= 0 { + return "" + } + + if len([]rune(s)) <= n { + return s + } + + return string([]rune(s)[:n]) +} + // Left returns the "n" left characters of the string. // // If the string is shorter than "n" it will return the first "n" characters of diff --git a/stringutil/stringutil_test.go b/stringutil/stringutil_test.go index f2948d3..d297193 100644 --- a/stringutil/stringutil_test.go +++ b/stringutil/stringutil_test.go @@ -6,6 +6,33 @@ import ( "testing" ) +func TestTruncate(t *testing.T) { + cases := []struct { + in string + n int + want string + }{ + {"Hello", 100, "Hello"}, + {"Hello", 1, "H"}, + {"Hello", 5, "Hello"}, + {"Hello", 4, "Hell"}, + {"Hello", 0, ""}, + {"Hello", -2, ""}, + {"汉语漢語", 1, "汉"}, + {"汉语漢語", 3, "汉语漢"}, + {"汉语漢語", 4, "汉语漢語"}, + } + + for i, tc := range cases { + t.Run(fmt.Sprintf("%v", i), func(t *testing.T) { + out := Truncate(tc.in, tc.n) + if out != tc.want { + t.Errorf("\nout: %#v\nwant: %#v\n", out, tc.want) + } + }) + } +} + func TestLeft(t *testing.T) { cases := []struct { in string From 885679d58fcec74769f754bde84dcfee4c4393a5 Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Thu, 28 May 2020 12:07:48 -0500 Subject: [PATCH 02/10] Update stringutil/stringutil.go Co-authored-by: Rafael Dantas Justo --- stringutil/stringutil.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stringutil/stringutil.go b/stringutil/stringutil.go index fa3d3aa..242d5b8 100644 --- a/stringutil/stringutil.go +++ b/stringutil/stringutil.go @@ -7,7 +7,7 @@ import ( "unicode" ) -// Left returns the "n" left characters of the string. +// Truncate returns the "n" left characters of the string. func Truncate(s string, n int) string { if n <= 0 { return "" From 4b767086beb72fa6b4f7ebd7bbc6a57bc98d598e Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Mon, 1 Jun 2020 13:17:24 -0500 Subject: [PATCH 03/10] go version --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index c845370..a13962c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/teamwork/utils -go 1.13 +go 1.14 require ( github.com/Strum355/go-difflib v1.1.0 // indirect From 4420e0681b29fc6cfc4c0c8cda0b61049c05a9cf Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Mon, 1 Jun 2020 13:53:02 -0500 Subject: [PATCH 04/10] SetSSP deterministic output --- httputilx/header/example_test.go | 10 +++++----- httputilx/header/set.go | 16 ++++++++++------ httputilx/header/set_test.go | 8 ++++---- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/httputilx/header/example_test.go b/httputilx/header/example_test.go index 2a0605f..e38e751 100644 --- a/httputilx/header/example_test.go +++ b/httputilx/header/example_test.go @@ -10,11 +10,11 @@ func ExampleSetCSP() { static := "static.example.com" headers := make(http.Header) header.SetCSP(headers, header.CSPArgs{ // nolint - header.CSPDefaultSrc: {header.CSPSourceNone}, - header.CSPScriptSrc: {static}, - header.CSPStyleSrc: {static, header.CSPSourceUnsafeInline}, - header.CSPFormAction: {header.CSPSourceSelf}, - header.CSPReportURI: {"/csp"}, + {header.CSPDefaultSrc, header.CSPSourceNone}, + {header.CSPScriptSrc, static}, + {header.CSPStyleSrc, static, header.CSPSourceUnsafeInline}, + {header.CSPFormAction, header.CSPSourceSelf}, + {header.CSPReportURI, "/csp"}, }) // Output: diff --git a/httputilx/header/set.go b/httputilx/header/set.go index 874b301..59652ad 100644 --- a/httputilx/header/set.go +++ b/httputilx/header/set.go @@ -139,7 +139,7 @@ const ( ) // CSPArgs are arguments for SetCSP(). -type CSPArgs map[string][]string +type CSPArgs [][]string // SetCSP sets a Content-Security-Policy header. // @@ -166,13 +166,17 @@ func SetCSP(header http.Header, args CSPArgs) error { var b strings.Builder i := 1 - for k, v := range args { - b.WriteString(k) + for _, v := range args { + if len(v) < 2 { + return errors.New("expected pair of values") + } + + b.WriteString(v[0]) b.WriteString(" ") - for j := range v { - b.WriteString(v[j]) - if j != len(v)-1 { + for j := range v[1:] { + b.WriteString(v[j+1]) + if j != len(v)-2 { b.WriteString(" ") } } diff --git a/httputilx/header/set_test.go b/httputilx/header/set_test.go index f88e543..07414a3 100644 --- a/httputilx/header/set_test.go +++ b/httputilx/header/set_test.go @@ -57,17 +57,17 @@ func TestCSP(t *testing.T) { }{ {CSPArgs{}, ""}, { - CSPArgs{CSPDefaultSrc: {CSPSourceSelf}}, + CSPArgs{{CSPDefaultSrc, CSPSourceSelf}}, "default-src 'self'", }, { - CSPArgs{CSPDefaultSrc: {CSPSourceSelf, "https://example.com"}}, + CSPArgs{{CSPDefaultSrc, CSPSourceSelf, "https://example.com"}}, "default-src 'self' https://example.com", }, { CSPArgs{ - CSPDefaultSrc: {CSPSourceSelf, "https://example.com"}, - CSPConnectSrc: {"https://a.com", "https://b.com"}, + {CSPDefaultSrc, CSPSourceSelf, "https://example.com"}, + {CSPConnectSrc, "https://a.com", "https://b.com"}, }, "default-src 'self' https://example.com; connect-src https://a.com https://b.com", }, From 2b4e2259c1683b315382cc1e468105ef3cf2a37c Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Mon, 1 Jun 2020 14:09:32 -0500 Subject: [PATCH 05/10] travis go version upgrade --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 333c129..944a6c6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - 1.13.x + - 1.14.x go_import_path: github.com/teamwork/utils notifications: email: false From d96ace17a08d860ac296b152a3525627498e6d04 Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Fri, 12 Nov 2021 07:41:09 -0600 Subject: [PATCH 06/10] upgrade to go1.17 --- .travis.yml | 2 +- goutil/goutil_test.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 944a6c6..b87fcc2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - 1.14.x + - 1.17.x go_import_path: github.com/teamwork/utils notifications: email: false diff --git a/goutil/goutil_test.go b/goutil/goutil_test.go index 6032c79..444fca8 100644 --- a/goutil/goutil_test.go +++ b/goutil/goutil_test.go @@ -39,7 +39,8 @@ func TestExpand(t *testing.T) { []string{"net/..."}, []string{"net", "net/http", "net/http/cgi", "net/http/cookiejar", "net/http/fcgi", "net/http/httptest", "net/http/httptrace", - "net/http/httputil", "net/http/internal", "net/http/pprof", + "net/http/httputil", "net/http/internal", "net/http/internal/ascii", + "net/http/internal/testcert", "net/http/pprof", "net/internal/socktest", "net/mail", "net/rpc", "net/rpc/jsonrpc", "net/smtp", "net/textproto", "net/url", }, @@ -95,12 +96,12 @@ func TestExpand(t *testing.T) { { []string{"thi.s/will/never/exist"}, nil, - `cannot find module providing package thi.s/will/never/exist`, + "no required module provides package thi.s/will/never/exist;", }, { []string{"thi.s/will/never/exist/..."}, nil, - `cannot find module providing package thi.s/will/never/exist`, + `no required module provides package thi.s/will/never/exist;`, }, { []string{"./doesnt/exist"}, From 810ff08fe1e601a85b423d4ada2db522adb78ffa Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Fri, 12 Nov 2021 08:00:58 -0600 Subject: [PATCH 07/10] fix tests --- .travis.yml | 2 +- go.mod | 2 +- goutil/goutil_test.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index b87fcc2..e233cdb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -16,7 +16,7 @@ install: | script: | set -e cd $HOME/gopath/src/github.com/teamwork/utils - ./bin/coverage -race ./... + env GO111MODULE=off ./bin/coverage -race ./... env GO111MODULE=off ./bin/lint ./... before_cache: | diff --git a/go.mod b/go.mod index a13962c..ecd4484 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/teamwork/utils -go 1.14 +go 1.17 require ( github.com/Strum355/go-difflib v1.1.0 // indirect diff --git a/goutil/goutil_test.go b/goutil/goutil_test.go index 444fca8..29a3f1a 100644 --- a/goutil/goutil_test.go +++ b/goutil/goutil_test.go @@ -96,12 +96,12 @@ func TestExpand(t *testing.T) { { []string{"thi.s/will/never/exist"}, nil, - "no required module provides package thi.s/will/never/exist;", + `cannot find package "thi.s/will/never/exist"`, }, { []string{"thi.s/will/never/exist/..."}, nil, - `no required module provides package thi.s/will/never/exist;`, + `cannot find package "thi.s/will/never/exist"`, }, { []string{"./doesnt/exist"}, From 1a72cad1a75da31ebff9aa2f1edc70d11928d3d7 Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Fri, 12 Nov 2021 08:20:39 -0600 Subject: [PATCH 08/10] revert 1.13 --- .travis.yml | 4 ++-- go.mod | 2 +- goutil/goutil_test.go | 7 +++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index e233cdb..333c129 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: go go: - - 1.17.x + - 1.13.x go_import_path: github.com/teamwork/utils notifications: email: false @@ -16,7 +16,7 @@ install: | script: | set -e cd $HOME/gopath/src/github.com/teamwork/utils - env GO111MODULE=off ./bin/coverage -race ./... + ./bin/coverage -race ./... env GO111MODULE=off ./bin/lint ./... before_cache: | diff --git a/go.mod b/go.mod index ecd4484..c845370 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/teamwork/utils -go 1.17 +go 1.13 require ( github.com/Strum355/go-difflib v1.1.0 // indirect diff --git a/goutil/goutil_test.go b/goutil/goutil_test.go index 29a3f1a..6032c79 100644 --- a/goutil/goutil_test.go +++ b/goutil/goutil_test.go @@ -39,8 +39,7 @@ func TestExpand(t *testing.T) { []string{"net/..."}, []string{"net", "net/http", "net/http/cgi", "net/http/cookiejar", "net/http/fcgi", "net/http/httptest", "net/http/httptrace", - "net/http/httputil", "net/http/internal", "net/http/internal/ascii", - "net/http/internal/testcert", "net/http/pprof", + "net/http/httputil", "net/http/internal", "net/http/pprof", "net/internal/socktest", "net/mail", "net/rpc", "net/rpc/jsonrpc", "net/smtp", "net/textproto", "net/url", }, @@ -96,12 +95,12 @@ func TestExpand(t *testing.T) { { []string{"thi.s/will/never/exist"}, nil, - `cannot find package "thi.s/will/never/exist"`, + `cannot find module providing package thi.s/will/never/exist`, }, { []string{"thi.s/will/never/exist/..."}, nil, - `cannot find package "thi.s/will/never/exist"`, + `cannot find module providing package thi.s/will/never/exist`, }, { []string{"./doesnt/exist"}, From a38107e0b596d5f9fefafd4d86db85d4ff8b4af5 Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Fri, 12 Nov 2021 08:46:11 -0600 Subject: [PATCH 09/10] Update stringutil/stringutil.go Co-authored-by: Rafael Dantas Justo --- stringutil/stringutil.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stringutil/stringutil.go b/stringutil/stringutil.go index 242d5b8..17fc888 100644 --- a/stringutil/stringutil.go +++ b/stringutil/stringutil.go @@ -13,7 +13,7 @@ func Truncate(s string, n int) string { return "" } - if len([]rune(s)) <= n { + if utf8.RuneCountInString(s) <= n { return s } From 326bb52cf5d2c5484473581ed8c6caecb8f69814 Mon Sep 17 00:00:00 2001 From: Cesar Cejudo Date: Fri, 12 Nov 2021 08:48:03 -0600 Subject: [PATCH 10/10] add utf8 import --- stringutil/stringutil.go | 1 + 1 file changed, 1 insertion(+) diff --git a/stringutil/stringutil.go b/stringutil/stringutil.go index 17fc888..d7b3fad 100644 --- a/stringutil/stringutil.go +++ b/stringutil/stringutil.go @@ -5,6 +5,7 @@ import ( "regexp" "strings" "unicode" + "unicode/utf8" ) // Truncate returns the "n" left characters of the string.