This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
/
common_steps.go
86 lines (65 loc) · 1.97 KB
/
common_steps.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package common
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/cucumber/godog"
"github.com/tidwall/gjson"
"github.com/trustbloc/edge-core/pkg/log"
"github.com/trustbloc/edv/test/bdd/pkg/context"
)
var logger = log.New("edv-bddtest-common")
// Steps is steps for VC BDD tests.
type Steps struct {
bddContext *context.BDDContext
queryValue string
}
// NewSteps returns new agent from client SDK.
func NewSteps(ctx *context.BDDContext) *Steps {
return &Steps{bddContext: ctx}
}
// RegisterSteps registers agent steps.
func (e *Steps) RegisterSteps(s *godog.Suite) {
s.Step(`^an HTTP GET is sent to "([^"]*)"$`, e.httpGet)
s.Step(`^the JSON path "([^"]*)" of the response equals "([^"]*)"$`, e.jsonPathOfCCResponseEquals)
}
// httpGet sends a GET request to the given URL.
func (e *Steps) httpGet(url string) error {
e.queryValue = ""
client := &http.Client{Transport: &http.Transport{TLSClientConfig: e.bddContext.TLSConfig}}
defer client.CloseIdleConnections()
httpReq, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
resp, err := client.Do(httpReq)
if err != nil {
return err
}
defer func() {
if errClose := resp.Body.Close(); errClose != nil {
logger.Warnf("Error closing HTTP response from [%s]: %s", url, errClose)
}
}()
payload, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("reading response body failed: %w", err)
}
e.queryValue = string(payload)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("received status code %d", resp.StatusCode)
}
return nil
}
func (e *Steps) jsonPathOfCCResponseEquals(path, expected string) error {
r := gjson.Get(e.queryValue, path)
logger.Infof("Path [%s] of JSON %s resolves to %s", path, e.queryValue, r.Str)
if r.Str == expected {
return nil
}
return fmt.Errorf("JSON path resolves to [%s] which is not the expected value [%s]", r.Str, expected)
}