bump go version #2
55
cmd/main.go
55
cmd/main.go
@ -5,13 +5,15 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/tiburon-777/api-crowler/internal/config"
|
||||
"github.com/tiburon-777/api-crowler/internal/scenario"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/tiburon-777/api-crowler/internal/config"
|
||||
"github.com/tiburon-777/api-crowler/internal/scenario"
|
||||
)
|
||||
|
||||
//nolint:gocognit,gocyclo,cyclop
|
||||
func main() {
|
||||
// Парсим флаги приложения.
|
||||
conf, err := config.Get()
|
||||
@ -20,65 +22,72 @@ func main() {
|
||||
}
|
||||
|
||||
// Читаем файл сценария.
|
||||
sc, err := scenario.Get(conf.Scenario)
|
||||
scenarios, err := scenario.Get(conf.Scenario)
|
||||
if err != nil {
|
||||
log.Fatalf("invalid scenario: %v ", err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
cli := http.Client{Timeout: sc.Params.Timeout}
|
||||
cli := http.Client{Timeout: scenarios.Params.Timeout}
|
||||
|
||||
// Запускаем сценарий
|
||||
for key, cs := range sc.Steps {
|
||||
log.Printf("Step#%d - %s", key, cs.Name)
|
||||
req, err := http.NewRequestWithContext(ctx, cs.Query.Method, fmt.Sprintf("%s%s", sc.Params.Address, cs.Query.URL), strings.NewReader(cs.Query.Data))
|
||||
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.Fatalf("request error: %v ", err)
|
||||
log.Printf("request error: %v ", err)
|
||||
}
|
||||
for hKey, hVal := range cs.Query.Headers {
|
||||
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.Fatalf("client error: %v ", err)
|
||||
log.Printf("client error: %v ", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
var body []byte
|
||||
if _, err := resp.Body.Read(body); err != nil {
|
||||
log.Fatalf("client read body: %v ", err)
|
||||
if _, err = resp.Body.Read(body); err != nil {
|
||||
log.Printf("client read body: %v ", err)
|
||||
}
|
||||
if cs.Response.Code != 0 && resp.StatusCode != cs.Response.Code {
|
||||
log.Fatalf("wrong response code: %v ", err)
|
||||
if step.Response.Code != 0 && resp.StatusCode != step.Response.Code {
|
||||
log.Printf("wrong response code: %v ", err)
|
||||
}
|
||||
if len(cs.Response.Body) != 0 {
|
||||
for _, sample := range cs.Response.Body {
|
||||
//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.Fatalf("wrong body")
|
||||
log.Printf("wrong body")
|
||||
}
|
||||
case "not_contains":
|
||||
if strings.Contains(string(body), sample.Text) {
|
||||
log.Fatalf("wrong body")
|
||||
log.Printf("wrong body")
|
||||
}
|
||||
case "begin":
|
||||
if !strings.HasPrefix(string(body), sample.Text) {
|
||||
log.Fatalf("wrong body")
|
||||
log.Printf("wrong body")
|
||||
}
|
||||
case "not_begin":
|
||||
if strings.HasPrefix(string(body), sample.Text) {
|
||||
log.Fatalf("wrong body")
|
||||
log.Printf("wrong body")
|
||||
}
|
||||
case "ends":
|
||||
if !strings.HasSuffix(string(body), sample.Text) {
|
||||
log.Fatalf("wrong body")
|
||||
log.Printf("wrong body")
|
||||
}
|
||||
case "not_ends":
|
||||
if strings.HasSuffix(string(body), sample.Text) {
|
||||
log.Fatalf("wrong body")
|
||||
log.Printf("wrong body")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -83,6 +83,15 @@ linters:
|
||||
- nlreturn
|
||||
- gofmt
|
||||
- gofumpt
|
||||
- varcheck
|
||||
- golint
|
||||
- structcheck
|
||||
- nosnakecase
|
||||
- maligned
|
||||
- interfacer
|
||||
- deadcode
|
||||
- scopelint
|
||||
- ifshort
|
||||
fast: false
|
||||
|
||||
issues:
|
||||
|
@ -2,6 +2,7 @@ package config
|
||||
|
||||
import (
|
||||
"flag"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
package scenario
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"gopkg.in/yaml.v2"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Get(file string) (*Object, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user