-
Notifications
You must be signed in to change notification settings - Fork 18
/
test_step.go
120 lines (100 loc) · 3.95 KB
/
test_step.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package runscope
import (
"errors"
"fmt"
)
// TestStep represents each step that makes up part of the test. See https://www.runscope.com/docs/api/steps
type TestStep struct {
URL string `json:"url,omitempty"`
Variables []*Variable `json:"variables,omitempty"`
Args map[string]interface{} `json:"args,omitempty"`
StepType string `json:"step_type,omitempty"`
Auth map[string]string `json:"auth,omitempty"`
ID string `json:"id,omitempty"`
Body string `json:"body,omitempty"`
Note string `json:"note,omitempty"`
Headers map[string][]string `json:"headers,omitempty"`
RequestID string `json:"request_id,omitempty"`
Assertions []*Assertion `json:"assertions,omitempty"`
Scripts []string `json:"scripts,omitempty"`
BeforeScripts []string `json:"before_scripts,omitempty"`
Method string `json:"method,omitempty"`
TestUUID string `json:"test_uuid,omitempty"`
}
// NewTestStep creates a new test step struct
func NewTestStep() *TestStep {
return &TestStep{}
}
// CreateTestStep creates a new runscope test step. See https://www.runscope.com/docs/api/steps#add
func (client *Client) CreateTestStep(testStep *TestStep, bucketKey string, testID string) (*TestStep, error) {
if error := testStep.validate(); error != nil {
return nil, error
}
client.Lock()
defer client.Unlock()
newResource, error := client.createResource(testStep, "test step", testStep.ID,
fmt.Sprintf("/buckets/%s/tests/%s/steps", bucketKey, testID))
if error != nil {
return nil, error
}
steps := newResource.Data.([]interface{})
step := steps[len(steps)-1].(map[string]interface{})
newTestStep, error := getTestStepFromResponse(step)
if error != nil {
return nil, error
}
return newTestStep, nil
}
// ReadTestStep list details about an existing test step. https://www.runscope.com/docs/api/steps#detail
func (client *Client) ReadTestStep(testStep *TestStep, bucketKey string, testID string) (*TestStep, error) {
resource, error := client.readResource("test step", testStep.ID,
fmt.Sprintf("/buckets/%s/tests/%s/steps/%s", bucketKey, testID, testStep.ID))
if error != nil {
return nil, error
}
readTestStep, error := getTestStepFromResponse(resource.Data)
if error != nil {
return nil, error
}
return readTestStep, nil
}
// UpdateTestStep updates an existing test step. https://www.runscope.com/docs/api/steps#modify
func (client *Client) UpdateTestStep(testStep *TestStep, bucketKey string, testID string) (*TestStep, error) {
resource, error := client.updateResource(testStep, "test step", testStep.ID,
fmt.Sprintf("/buckets/%s/tests/%s/steps/%s", bucketKey, testID, testStep.ID))
if error != nil {
return nil, error
}
readTestStep, error := getTestStepFromResponse(resource.Data)
if error != nil {
return nil, error
}
return readTestStep, nil
}
// DeleteTestStep delete an existing test step. https://www.runscope.com/docs/api/steps#delete
func (client *Client) DeleteTestStep(testStep *TestStep, bucketKey string, testID string) error {
return client.deleteResource("test step", testStep.ID,
fmt.Sprintf("/buckets/%s/tests/%s/steps/%s", bucketKey, testID, testStep.ID))
}
func getTestStepFromResponse(response interface{}) (*TestStep, error) {
testStep := new(TestStep)
err := decode(testStep, response)
return testStep, err
}
func (step *TestStep) validate() error {
if step.StepType == "request" {
if err := step.validateRequestType(); err != nil {
return err
}
}
return nil
}
func (step *TestStep) validateRequestType() error {
if step.Method == "" {
return errors.New("A request test step must specify 'Method' property")
}
if step.Method == "GET" && step.Body != "" {
return errors.New("A request test step that specifies a 'GET' method can not include a body property")
}
return nil
}