-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient_test.go
150 lines (139 loc) · 3.69 KB
/
client_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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"encoding/json"
"encoding/xml"
"log/slog"
"net/http"
"os"
"reflect"
"regexp"
"testing"
"github.com/go-resty/resty/v2"
"gopkg.in/yaml.v3"
)
func TestParseJsonResponse(t *testing.T) {
client := &Client{
client: resty.New(),
symtab: map[string]any{},
logger: &slog.Logger{},
}
file_content, err := os.ReadFile("fixtures/response.json")
if err != nil {
t.Errorf(`ParseJsonResponse() load test results error: %s`, err.Error())
return
}
var data any
if err := json.Unmarshal(file_content, &data); err != nil {
t.Errorf(`ParseJsonResponse() parsing test results error: %s`, err.Error())
return
}
raw_http := &http.Response{
Header: make(map[string][]string),
}
raw_http.Header.Add(contentTypeHeader, "application/json")
resp := &resty.Response{
RawResponse: raw_http,
}
resp.SetBody(file_content)
res_data := client.getResponse(resp, "json")
if !reflect.DeepEqual(data, res_data) {
t.Errorf(`ParseJsonResponse() parsed json results differ`)
}
}
func TestParseXMLResponse(t *testing.T) {
client := &Client{
client: resty.New(),
symtab: map[string]any{},
logger: &slog.Logger{},
}
file_content, err := os.ReadFile("fixtures/response.xml")
if err != nil {
t.Errorf(`ParseXMLResponse() load test result error: %s`, err.Error())
return
}
var (
data_internal *Content
data any
)
if err := xml.Unmarshal(file_content, &data_internal); err != nil {
t.Errorf(`ParseXMLResponse() parsing test results error: %s`, err.Error())
return
}
data_tmp := make(map[string]any)
data_tmp[data_internal.Name] = data_internal.Attrs
data = data_tmp
raw_http := &http.Response{
Header: make(map[string][]string),
}
raw_http.Header.Add(contentTypeHeader, "text/xml")
resp := &resty.Response{
RawResponse: raw_http,
}
resp.SetBody(file_content)
res_data := client.getResponse(resp, "xml")
if !reflect.DeepEqual(data, res_data) {
t.Errorf(`ParseXMLResponse() parsed xml results differ`)
}
}
func TestParseYAMLResponse(t *testing.T) {
client := &Client{
client: resty.New(),
symtab: map[string]any{},
logger: &slog.Logger{},
}
file_content, err := os.ReadFile("fixtures/response_simple.yml")
if err != nil {
t.Errorf(`ParseYAMLResponse() load test results error: %s`, err.Error())
return
}
var (
data_internal any
)
if err := yaml.Unmarshal(file_content, &data_internal); err != nil {
t.Errorf(`ParseYAMLResponse() parsing test results error: %s`, err.Error())
return
}
raw_http := &http.Response{
Header: make(map[string][]string),
}
raw_http.Header.Add(contentTypeHeader, "application/yaml")
resp := &resty.Response{
RawResponse: raw_http,
}
resp.SetBody(file_content)
res_data := client.getResponse(resp, "yaml")
if !reflect.DeepEqual(data_internal, res_data) {
t.Errorf(`ParseYAMLResponse() parsed json results differ`)
}
}
func TestParseTextLineResponse(t *testing.T) {
client := &Client{
client: resty.New(),
symtab: map[string]any{},
logger: &slog.Logger{},
}
file_content, err := os.ReadFile("fixtures/response_line_simple.txt")
if err != nil {
t.Errorf(`ParseTextLineResponse() load test results error: %s`, err.Error())
return
}
var data []string
if re, err := regexp.Compile("\r?\n"); err != nil {
t.Errorf(`ParseTextLineResponse() parsing test results error: %s`, err.Error())
return
} else {
data = re.Split(string(file_content), -1)
}
raw_http := &http.Response{
Header: make(map[string][]string),
}
raw_http.Header.Add(contentTypeHeader, "text/plain")
resp := &resty.Response{
RawResponse: raw_http,
}
resp.SetBody(file_content)
res_data := client.getResponse(resp, "text-lines")
if !reflect.DeepEqual(data, res_data) {
t.Errorf(`ParseTextLineResponse() parsed text results differ`)
}
}