Skip to content

Commit

Permalink
Merge pull request #60 from Teamwork/just-truncate
Browse files Browse the repository at this point in the history
Adds Truncate function
  • Loading branch information
cesartw authored Nov 12, 2021
2 parents 1f9ea59 + 326bb52 commit 61c8125
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 15 deletions.
10 changes: 5 additions & 5 deletions httputilx/header/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 10 additions & 6 deletions httputilx/header/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand All @@ -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(" ")
}
}
Expand Down
8 changes: 4 additions & 4 deletions httputilx/header/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
14 changes: 14 additions & 0 deletions stringutil/stringutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@ import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
)

// Truncate returns the "n" left characters of the string.
func Truncate(s string, n int) string {
if n <= 0 {
return ""
}

if utf8.RuneCountInString(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
Expand Down
27 changes: 27 additions & 0 deletions stringutil/stringutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 61c8125

Please sign in to comment.