-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.go
74 lines (64 loc) · 2.11 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package cmd
import (
"net/http"
"time"
"github.com/fabmob/playground-standard-covoiturage/cmd/test"
"github.com/spf13/cobra"
)
// testCmd represents the test command
var testCmd = &cobra.Command{
Use: "test",
Short: "Test an API complying with the standard covoiturage",
Long: "Test an API complying with the standard covoiturage",
Run: func(cmd *cobra.Command, args []string) {
var timeout = 100 * time.Millisecond
body, err := readBodyFromStdin(cmd, timeout)
if err != nil {
body = nil
}
err = test.RunTest(method, URL, query, body, verbose, apiKey, flagsWithDefault(http.StatusOK))
exitWithError(err)
},
}
var (
apiKey string
URL string
verbose bool
query test.Query
expectNonEmpty bool
expectResponseCode int
method string
)
func init() {
rootCmd.AddCommand(testCmd)
testCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Make the operation more talkative")
testCmd.PersistentFlags().BoolVar(
&expectNonEmpty,
"expectNonEmpty",
test.DefaultFlagExpectNonEmpty,
"Should an empty request return an error",
)
testCmd.PersistentFlags().StringVar(&apiKey, "auth", "", "API key sent in the \"X-API-Key\" header of the request")
testCmd.PersistentFlags().IntVar(
&expectResponseCode,
"expectResponseCode",
0,
"Expected status code. Defaults to success, 2xx, status code - exact default depends on endpoint",
)
testCmd.Flags().StringVar(
&expectedBookingStatus, "expectBookingStatus", "", "Expected booking status, checked on response (only for GET /bookings)",
)
testCmd.Flags().StringVar(&method, "method", http.MethodGet, "HTTP method, either GET (default), POST or PATCH")
testCmd.Flags().StringVarP(&URL, "url", "u", "", "API call URL")
testCmd.Flags().VarP(&query, "query", "q", "Query parameters in the form name=value")
}
func flagsWithDefault(defaultStatus int) test.Flags {
flags := test.NewFlags()
flags.ExpectNonEmpty = expectNonEmpty
if expectResponseCode == 0 { //not set
flags.ExpectedResponseCode = defaultStatus
} else {
flags.ExpectedResponseCode = expectResponseCode
}
return flags
}