/
http_api.go
175 lines (135 loc) · 5.74 KB
/
http_api.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* go4api - an api testing tool written in Go
* Created by: Ping Zhu 2018
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
package api
import (
"fmt"
"strings"
// "encoding/json"
"go4api/lib/testcase"
g4http "go4api/protocal/http"
)
func (tcDataStore *TcDataStore) RunHttp (baseUrl string) (string, []*testcase.TestMessage) {
tcDataStore.CallHttp(baseUrl)
httpResult, httpTestMessages := tcDataStore.Compare()
return httpResult, httpTestMessages
}
func (tcDataStore *TcDataStore) CallHttp (baseUrl string) {
path := "TestCase." + tcDataStore.TcData.TestCase.TcName() + "." + "request"
tcDataStore.PrepEmbeddedFunctions(path)
tcData := tcDataStore.TcData
// urlStr := tcData.TestCase.UrlRaw(baseUrl)
urlStr := tcData.TestCase.UrlEncode(baseUrl)
tcDataStore.HttpUrl = urlStr
//
apiMethodSelector, apiMethod, bodyText, bodyMultipart, boundary := GetPayloadInfo(tcData)
//
reqHeaders := make(map[string]interface{})
reqHeaders = tcData.TestCase.ReqHeaders()
// set the boundary to headers, if multipart
if boundary != "" {
reqHeaders["Content-Type"] = boundary
}
// < !! ----------- !! >
var actualStatusCode int
var actualHeader map[string][]string
var actualBody []byte
//
httpRequest := g4http.HttpRestful{}
if apiMethodSelector == "POSTMultipart" {
actualStatusCode, actualHeader, actualBody = httpRequest.Request(urlStr, apiMethod, reqHeaders, bodyMultipart)
} else {
actualStatusCode, actualHeader, actualBody = httpRequest.Request(urlStr, apiMethod, reqHeaders, bodyText)
}
tcDataStore.HttpActualStatusCode = actualStatusCode
tcDataStore.HttpActualHeader = actualHeader
tcDataStore.HttpActualBody = actualBody
}
func (tcDataStore *TcDataStore) Compare () (string, []*testcase.TestMessage) {
var testResults []bool
var testMessages []*testcase.TestMessage
path := "TestCase." + tcDataStore.TcData.TestCase.TcName() + "." + "response"
tcDataStore.PrepEmbeddedFunctions(path)
httpExpResp := tcDataStore.TcData.TestCase.Response()
testResults, testMessages = tcDataStore.CompareHttpRespGroup(httpExpResp)
// default finalResults
finalResults := "Success"
for key := range testResults {
if testResults[key] == false {
finalResults = "Fail"
break
}
}
// testMessagesJson, _ := json.Marshal(testMessages)
// testMessagesJsonStr := string(testMessagesJson)
return finalResults, testMessages
}
func (tcDataStore *TcDataStore) CompareHttpRespGroup (httpExpResp []map[string]interface{}) ([]bool, []*testcase.TestMessage){
var testResults []bool
var testMessages []*testcase.TestMessage
for _, v := range httpExpResp {
for key, value := range v {
httpExpResp_sub := value.(map[string]interface{})
for assertionKey, expValueOrigin := range httpExpResp_sub {
switch assertionKey {
case "HasMapKey", "NotHasMapKey":
case "IsNull", "IsNotNull":
default:
actualValue := tcDataStore.GetResponseValue(key)
var expValue interface{}
switch expValueOrigin.(type) {
case float64, int64, nil:
expValue = expValueOrigin
default:
expValue = tcDataStore.GetResponseValue(fmt.Sprint(expValueOrigin))
}
// $(status), $(headers), $(body)
var part string
switch {
case strings.HasPrefix(key, "$(status)"):
part = "HTTP.Status"
case strings.HasPrefix(key, "$(headers)"):
part = "HTTP.Headers"
case strings.HasPrefix(key, "$(body)"):
part = "HTTP.Body"
default:
part = "HTTP"
}
testRes, msg := compareCommon(part, key, assertionKey, actualValue, expValue)
testMessages = append(testMessages, msg)
testResults = append(testResults, testRes)
}
}
}
}
return testResults, testMessages
}
//
func (tcDataStore *TcDataStore) HandleHttpResultsForOut () {
// write out session if has
path := "TestCase." + tcDataStore.TcData.TestCase.TcName() + "." + "session"
tcDataStore.PrepEmbeddedFunctions(path)
expTcSession := tcDataStore.TcData.TestCase.Session()
tcDataStore.WriteSession(expTcSession)
// write out global variables if has
path = "TestCase." + tcDataStore.TcData.TestCase.TcName() + "." + "outGlobalVariables"
tcDataStore.PrepEmbeddedFunctions(path)
expOutGlobalVariables := tcDataStore.TcData.TestCase.OutGlobalVariables()
tcDataStore.WriteOutGlobalVariables(expOutGlobalVariables)
// write out tc loca variables if has
path = "TestCase." + tcDataStore.TcData.TestCase.TcName() + "." + "outLocalVariables"
tcDataStore.PrepEmbeddedFunctions(path)
expOutLocalVariables := tcDataStore.TcData.TestCase.OutLocalVariables()
tcDataStore.WriteOutTcLocalVariables(expOutLocalVariables)
// write out files if has
path = "TestCase." + tcDataStore.TcData.TestCase.TcName() + "." + "outFiles"
tcDataStore.PrepEmbeddedFunctions(path)
expOutFiles := tcDataStore.TcData.TestCase.OutFiles()
tcDataStore.HandleOutFiles(expOutFiles)
}