-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocessor.go
More file actions
101 lines (81 loc) · 2.55 KB
/
Copy pathprocessor.go
File metadata and controls
101 lines (81 loc) · 2.55 KB
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
package main
import (
"encoding/json"
"fmt"
"reflect"
)
type httpExecuter interface {
execute(httpMethod, pathName string, xTestSuiteRequest XTestSuiteRequest) (statusCode int, body []byte, err error)
}
type processTestSuites struct {
httpExecuter httpExecuter
}
func newTestProcessor(httpExecuter httpExecuter) processTestSuites {
return processTestSuites{
httpExecuter: httpExecuter,
}
}
func (testProcessor processTestSuites) processTestSuites(httpMethod, pathName string, xTestSuites []XTestSuite) []testSuiteReport {
testSuites := []testSuiteReport{}
for _, test := range xTestSuites {
testSuite := testSuiteReport{
PathName: pathName,
Description: test.Description,
Operation: httpMethod,
ResultDetails: resultDetails{},
}
statusCode, body, err := testProcessor.httpExecuter.execute(httpMethod, pathName, test.Request)
if err != nil {
testSuite.FailWithError(err)
testSuites = append(testSuites, testSuite)
continue
}
// Set the expected and actual results for the report
testSuite.ResultDetails.SetActualExpectHTTPStatus(test.Response.HTTPStatus, statusCode)
testSuite.ResultDetails.SetActualExpectBody(test.Response.Body, string(body))
// `test.Response.ShouldSkipBodyValidation` should be run first before anything
// so that the value is set before we do assertions.
//
// If we do assertions first there might be a chance that this
// `test.Response.ShouldSkipBodyValidation` would be skipped
if test.Response.ShouldSkipBodyValidation() {
// Modify the test suite report so it does not show
// body assertions
testSuite.ShouldSkipBodyValidation = true
if test.Description == "[NEG] Invalid otp" {
fmt.Println(testSuite.ShouldSkipBodyValidation)
}
} else {
bodyIsEqual, err := compareResponse([]byte(test.Response.Body), body)
if err != nil {
testSuite.FailWithError(err)
testSuites = append(testSuites, testSuite)
continue
}
if !bodyIsEqual {
testSuite.Fail()
testSuites = append(testSuites, testSuite)
continue
}
}
if test.Response.HTTPStatus != statusCode {
testSuite.Fail()
testSuites = append(testSuites, testSuite)
continue
}
testSuite.Pass()
testSuites = append(testSuites, testSuite)
}
return testSuites
}
//TODO: Refactor this code placement
func compareResponse(a, b []byte) (bool, error) {
var json1, json2 interface{}
if err := json.Unmarshal(a, &json1); err != nil {
return false, err
}
if err := json.Unmarshal(b, &json2); err != nil {
return false, err
}
return reflect.DeepEqual(json1, json2), nil
}