package main // validate doc - https://pkg.go.dev/github.com/go-playground/validator/v10 import ( "context" "fmt" "log" "net/http" "strings" "git.tiburon.su/OCTOPUS/api-crowler/internal/config" "git.tiburon.su/OCTOPUS/api-crowler/internal/scenario" ) //nolint:gocognit,gocyclo,cyclop func main() { // Парсим флаги приложения. conf, err := config.Get() if err != nil { log.Fatalf("invalid config: %v ", err) } // Читаем файл сценария. scenarios, err := scenario.Get(conf.Scenario) if err != nil { log.Fatalf("invalid scenario: %v ", err) } ctx := context.Background() cli := http.Client{Timeout: scenarios.Params.Timeout} // Запускаем сценарий for key, step := range scenarios.Steps { log.Printf("Step#%d - %s", key, step.Name) req, err := http.NewRequestWithContext( ctx, step.Query.Method, fmt.Sprintf("%s%s", scenarios.Params.Address, step.Query.URL), strings.NewReader(step.Query.Data), ) if err != nil { log.Printf("request error: %v ", err) } defer req.Body.Close() for hKey, hVal := range step.Query.Headers { req.Header.Add(hKey, hVal) } resp, err := cli.Do(req) if err != nil { log.Printf("client error: %v ", err) } defer resp.Body.Close() var body []byte if _, err = resp.Body.Read(body); err != nil { log.Printf("client read body: %v ", err) } if step.Response.Code != 0 && resp.StatusCode != step.Response.Code { log.Printf("wrong response code: %v ", err) } //nolint:nestif if len(step.Response.Body) != 0 { for _, sample := range step.Response.Body { switch sample.Function { case "contains": if !strings.Contains(string(body), sample.Text) { log.Printf("wrong body") } case "not_contains": if strings.Contains(string(body), sample.Text) { log.Printf("wrong body") } case "begin": if !strings.HasPrefix(string(body), sample.Text) { log.Printf("wrong body") } case "not_begin": if strings.HasPrefix(string(body), sample.Text) { log.Printf("wrong body") } case "ends": if !strings.HasSuffix(string(body), sample.Text) { log.Printf("wrong body") } case "not_ends": if strings.HasSuffix(string(body), sample.Text) { log.Printf("wrong body") } } } } } }