2018-10-14 21:38:14 +02:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-06-25 18:51:51 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2019-02-20 01:10:18 +02:00
|
|
|
|
2018-10-14 21:38:14 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/core"
|
|
|
|
"github.com/MontFerret/ferret/pkg/runtime/values"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Assertions() map[string]core.Function {
|
|
|
|
return map[string]core.Function{
|
2019-06-25 18:51:51 +02:00
|
|
|
"EXPECT": expect,
|
|
|
|
"T::EXPECT": expect,
|
|
|
|
"T::HTTP::GET": httpGet,
|
2018-10-14 21:38:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expect(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 2, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if args[0].Compare(args[1]) == 0 {
|
|
|
|
return values.EmptyString, nil
|
|
|
|
}
|
|
|
|
|
2018-11-22 05:45:00 +02:00
|
|
|
return values.NewString(fmt.Sprintf(`expected "%s", but got "%s"`, args[0], args[1])), nil
|
2018-10-14 21:38:14 +02:00
|
|
|
}
|
2019-06-25 18:51:51 +02:00
|
|
|
|
|
|
|
func httpGet(_ context.Context, args ...core.Value) (core.Value, error) {
|
|
|
|
err := core.ValidateArgs(args, 1, 2)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
url := args[0].String()
|
|
|
|
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return values.None, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return values.String(b), nil
|
|
|
|
}
|