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 ./... 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 412b924..518e1f8 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,24 @@ 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..731ee4e --- /dev/null +++ b/utils/slice_test.go @@ -0,0 +1,171 @@ +/* +========== +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_test + +import ( + "net/http" + "reflect" + "testing" + + "github.com/edoardottt/cariddi/utils" +) + +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 := utils.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 := utils.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 := utils.CheckCookies(tt.input); !reflect.DeepEqual(got, tt.want) { + t.Errorf("CheckCookies() = %v, want %v", got, tt.want) + } + }) + } +}