bump go version #2

Closed
tiburon wants to merge 8 commits from initial_code into master
4 changed files with 44 additions and 24 deletions
Showing only changes of commit 41ec43f0f4 - Show all commits

View File

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

View File

@ -83,6 +83,15 @@ linters:
- nlreturn - nlreturn
- gofmt - gofmt
- gofumpt - gofumpt
- varcheck
- golint
- structcheck
- nosnakecase
- maligned
- interfacer
- deadcode
- scopelint
- ifshort
fast: false fast: false
issues: issues:

View File

@ -2,6 +2,7 @@ package config
import ( import (
"flag" "flag"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
) )

View File

@ -1,9 +1,10 @@
package scenario package scenario
import ( import (
"os"
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"os"
) )
func Get(file string) (*Object, error) { func Get(file string) (*Object, error) {