89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package main
|
|
|
|
// validate doc - https://pkg.go.dev/github.com/go-playground/validator/v10
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/tiburon-777/w3back/internal/scenario"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
// Парсим флаги приложения.
|
|
file := flag.String("scenario", "./test/test.yaml", "path to file with test scenario")
|
|
flag.Parse()
|
|
ctx := context.Background()
|
|
|
|
// Читаем файл сценария.
|
|
sc, err := scenario.New(ctx, *file)
|
|
if err != nil {
|
|
log.Fatalf("can't understand test scenario: %v ", err)
|
|
}
|
|
|
|
// Валидируем сценарий
|
|
if err := validator.New().Struct(sc); err != nil {
|
|
log.Fatalf("invalid scenario: %v ", err)
|
|
}
|
|
|
|
cli := http.Client{Timeout: sc.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))
|
|
if err != nil {
|
|
log.Fatalf("request error: %v ", err)
|
|
}
|
|
for hKey, hVal := range cs.Query.Headers {
|
|
req.Header.Add(hKey, hVal)
|
|
}
|
|
resp, err := cli.Do(req)
|
|
if err != nil {
|
|
log.Fatalf("client error: %v ", err)
|
|
}
|
|
var body []byte
|
|
if _, err := resp.Body.Read(body); err != nil {
|
|
log.Fatalf("client read body: %v ", err)
|
|
}
|
|
if cs.Response.Code != 0 && resp.StatusCode != cs.Response.Code {
|
|
log.Fatalf("wrong response code: %v ", err)
|
|
}
|
|
if len(cs.Response.Body) != 0 {
|
|
for _, sample := range cs.Response.Body {
|
|
switch sample.Function {
|
|
case "contains":
|
|
if !strings.Contains(string(body), sample.Text) {
|
|
log.Fatalf("wrong body")
|
|
}
|
|
case "not_contains":
|
|
if strings.Contains(string(body), sample.Text) {
|
|
log.Fatalf("wrong body")
|
|
}
|
|
case "begin":
|
|
if !strings.HasPrefix(string(body), sample.Text) {
|
|
log.Fatalf("wrong body")
|
|
}
|
|
case "not_begin":
|
|
if strings.HasPrefix(string(body), sample.Text) {
|
|
log.Fatalf("wrong body")
|
|
}
|
|
case "ends":
|
|
if !strings.HasSuffix(string(body), sample.Text) {
|
|
log.Fatalf("wrong body")
|
|
}
|
|
case "not_ends":
|
|
if strings.HasSuffix(string(body), sample.Text) {
|
|
log.Fatalf("wrong body")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|