From 4e15703c43c4068e1e05f737e1b36c151c05687d Mon Sep 17 00:00:00 2001 From: Sergei Makarov Date: Sat, 3 Sep 2022 19:14:51 +0400 Subject: [PATCH 1/3] util fixes, tests --- utils/slice.go | 18 ++--- utils/slice_test.go | 169 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+), 11 deletions(-) create mode 100644 utils/slice_test.go diff --git a/utils/slice.go b/utils/slice.go index 412b924..539c0be 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -35,7 +35,7 @@ import ( // of strings. func RemoveDuplicateValues(strSlice []string) []string { keys := make(map[string]bool) - list := []string{} + list := make([]string, 0, len(strSlice)) for _, entry := range strSlice { if _, value := keys[entry]; !value { @@ -55,7 +55,7 @@ func CheckInputArray(input string) []string { delimiter := byte(',') sliceOut := strings.Split(input, string(delimiter)) sliceOut = RemoveDuplicateValues(sliceOut) - result := []string{} + result := make([]string, 0, len(sliceOut)) for _, elem := range sliceOut { if elem != "" { @@ -71,27 +71,23 @@ func CheckInputArray(input string) []string { // format: "name1:value1;name2:value2" // It returns a slice of Cookies. func CheckCookies(input string) []*http.Cookie { - var result []*http.Cookie if input == "" { - return result + return []*http.Cookie{} } // Split and get different pairs of (name,value) pairs := strings.Split(input, ";") if len(pairs) == 0 { - return result + return []*http.Cookie{} } + result := make([]*http.Cookie, 0, len(pairs)) for _, pair := range pairs { couple := strings.Split(pair, ":") - if len(couple) == 0 { + if len(couple) != 2 { continue } - if len(couple) == 2 { - result = append(result, &http.Cookie{Name: couple[0], Value: couple[1]}) - } else { - continue - } + result = append(result, &http.Cookie{Name: couple[0], Value: couple[1]}) } return result diff --git a/utils/slice_test.go b/utils/slice_test.go new file mode 100644 index 0000000..f24668e --- /dev/null +++ b/utils/slice_test.go @@ -0,0 +1,169 @@ +/* +========== +Cariddi +========== + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see http://www.gnu.org/licenses/. + + @Repository: https://github.com/edoardottt/cariddi + + @Author: edoardottt, https://www.edoardoottavianelli.it + + @License: https://github.com/edoardottt/cariddi/blob/main/LICENSE + +*/ + +package utils + +import ( + "net/http" + "reflect" + "testing" +) + +func TestRemoveDuplicateValues(t *testing.T) { + tests := []struct { + name string + slice []string + want []string + }{ + { + name: "empty slice", + slice: []string{}, + want: []string{}, + }, + { + name: "nil slice", + slice: nil, + want: []string{}, + }, + { + name: "withous duplicates", + slice: []string{"a", "b", "c"}, + want: []string{"a", "b", "c"}, + }, + { + name: "has duplicates", + slice: []string{"a", "b", "c", "e", "c", "a"}, + want: []string{"a", "b", "c", "e"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := RemoveDuplicateValues(tt.slice); !reflect.DeepEqual(got, tt.want) { + t.Errorf("RemoveDuplicateValues() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCheckInputArray(t *testing.T) { + tests := []struct { + name string + input string + want []string + }{ + { + name: "empty slice", + input: "", + want: []string{}, + }, + { + name: "empty strings", + input: ",,,,", + want: []string{}, + }, + { + name: "with duplicates", + input: "a,b,a,,c,,d,b, , ,, ", + want: []string{"a", "b", "c", "d", " ", " "}, + }, + { + name: "without duplicates", + input: "a,b,c,d=234, ,", + want: []string{"a", "b", "c", "d=234", " "}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := CheckInputArray(tt.input); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CheckInputArray() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestCheckCookies(t *testing.T) { + tests := []struct { + name string + input string + want []*http.Cookie + }{ + { + name: "empty input", + input: "", + want: []*http.Cookie{}, + }, + { + name: "zero pairs", + input: "asdd311ue2", + want: []*http.Cookie{}, + }, + { + name: "one pair", + input: "name:some_value123", + want: []*http.Cookie{ + { + Name: "name", + Value: "some_value123", + }, + }, + }, + { + name: "several paris", + input: "name1:some_value@1;name_2:some$%_value@", + want: []*http.Cookie{ + { + Name: "name1", + Value: "some_value@1", + }, + { + Name: "name_2", + Value: "some$%_value@", + }, + }, + }, + { + name: "some pairs are not valid", + input: "name1:value:_1;name;2:value2;name_3:value_3", + want: []*http.Cookie{ + { + Name: "2", + Value: "value2", + }, + { + Name: "name_3", + Value: "value_3", + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := CheckCookies(tt.input); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CheckCookies() = %v, want %v", got, tt.want) + } + }) + } +} From 9a83aab6978f2653cf40d771a5421a948fdc8d97 Mon Sep 17 00:00:00 2001 From: Sergei Makarov Date: Mon, 5 Sep 2022 00:16:23 +0400 Subject: [PATCH 2/3] fix lint --- crawler/requests.go | 8 ++++---- utils/files.go | 4 ++-- utils/slice.go | 1 + utils/slice_test.go | 10 ++++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/crawler/requests.go b/crawler/requests.go index 57b3f7f..7f81de7 100644 --- a/crawler/requests.go +++ b/crawler/requests.go @@ -29,7 +29,7 @@ package crawler import ( "bytes" "encoding/json" - "io/ioutil" + "io" "net/http" ) @@ -43,7 +43,7 @@ func GetRequest(target string) (string, error) { defer resp.Body.Close() // We Read the response body on the line below. - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -69,7 +69,7 @@ func PostRequest(target string, data map[string]string) (string, error) { } defer resp.Body.Close() // Read the response body - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -88,7 +88,7 @@ func HeadRequest(target string) (string, error) { } defer resp.Body.Close() // Read the response body - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { return "", err } diff --git a/utils/files.go b/utils/files.go index 6133cd8..d2e0b4d 100644 --- a/utils/files.go +++ b/utils/files.go @@ -29,7 +29,7 @@ package utils import ( "bufio" "fmt" - "io/ioutil" + "io" "log" "net/http" "os" @@ -178,7 +178,7 @@ func ReadEntireFile(inputFile string) []byte { } }() - b, err := ioutil.ReadAll(file) + b, err := io.ReadAll(file) return b } diff --git a/utils/slice.go b/utils/slice.go index 539c0be..518e1f8 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -81,6 +81,7 @@ func CheckCookies(input string) []*http.Cookie { } result := make([]*http.Cookie, 0, len(pairs)) + for _, pair := range pairs { couple := strings.Split(pair, ":") if len(couple) != 2 { diff --git a/utils/slice_test.go b/utils/slice_test.go index f24668e..731ee4e 100644 --- a/utils/slice_test.go +++ b/utils/slice_test.go @@ -24,12 +24,14 @@ along with this program. If not, see http://www.gnu.org/licenses/. */ -package utils +package utils_test import ( "net/http" "reflect" "testing" + + "github.com/edoardottt/cariddi/utils" ) func TestRemoveDuplicateValues(t *testing.T) { @@ -61,7 +63,7 @@ func TestRemoveDuplicateValues(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := RemoveDuplicateValues(tt.slice); !reflect.DeepEqual(got, tt.want) { + if got := utils.RemoveDuplicateValues(tt.slice); !reflect.DeepEqual(got, tt.want) { t.Errorf("RemoveDuplicateValues() = %v, want %v", got, tt.want) } }) @@ -97,7 +99,7 @@ func TestCheckInputArray(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := CheckInputArray(tt.input); !reflect.DeepEqual(got, tt.want) { + if got := utils.CheckInputArray(tt.input); !reflect.DeepEqual(got, tt.want) { t.Errorf("CheckInputArray() = %v, want %v", got, tt.want) } }) @@ -161,7 +163,7 @@ func TestCheckCookies(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := CheckCookies(tt.input); !reflect.DeepEqual(got, tt.want) { + if got := utils.CheckCookies(tt.input); !reflect.DeepEqual(got, tt.want) { t.Errorf("CheckCookies() = %v, want %v", got, tt.want) } }) From 03d34f547738c69ff4d23e5df0f3cff24aafaafa Mon Sep 17 00:00:00 2001 From: Sergei Makarov Date: Mon, 5 Sep 2022 00:32:30 +0400 Subject: [PATCH 3/3] change job go version --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 299e1d3..b1f9599 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -16,7 +16,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.15 + go-version: 1.17 - name: Build run: go build -v ./...