-
Notifications
You must be signed in to change notification settings - Fork 17
/
validate.go
81 lines (67 loc) · 2.34 KB
/
validate.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
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
// SPDX-License-Identifier: AGPL
package daemon
import (
"github.com/pb33f/libopenapi-validator/errors"
"github.com/pb33f/ranch/model"
"net/http"
)
func (ws *WiretapService) ValidateResponse(
request *model.Request,
returnedResponse *http.Response) []*errors.ValidationError {
var validationErrors []*errors.ValidationError
if ws.document != nil && ws.docModel != nil {
_, validationErrors = ws.validator.ValidateHttpResponse(request.HttpRequest, returnedResponse)
}
// wipe out any path not found errors, they are not relevant to the response.
var cleanedErrors []*errors.ValidationError
for x := range validationErrors {
if !validationErrors[x].IsPathMissingError() {
cleanedErrors = append(cleanedErrors, validationErrors[x])
}
}
transaction := BuildResponse(request, returnedResponse)
if len(cleanedErrors) > 0 {
transaction.ResponseValidation = cleanedErrors
}
ws.transactionStore.Put(request.Id.String(), transaction, nil)
if len(cleanedErrors) > 0 {
ws.streamChan <- cleanedErrors
ws.broadcastResponseValidationErrors(request, returnedResponse, cleanedErrors)
} else {
ws.broadcastResponse(request, returnedResponse)
}
return validationErrors
}
func (ws *WiretapService) ValidateRequest(
modelRequest *model.Request,
httpRequest *http.Request) []*errors.ValidationError {
var validationErrors, cleanedErrors []*errors.ValidationError
if ws.document != nil && ws.docModel != nil {
validator := ws.validator
_, validationErrors = validator.ValidateHttpRequest(httpRequest)
}
for _, validationError := range validationErrors {
cleanedErrors = append(cleanedErrors, validationError)
}
// record results
buildTransConfig := HttpTransactionConfig{
OriginalRequest: modelRequest.HttpRequest,
NewRequest: httpRequest,
ID: modelRequest.Id,
TransactionConfig: ws.config,
}
transaction := BuildHttpTransaction(buildTransConfig)
if len(cleanedErrors) > 0 {
transaction.RequestValidation = cleanedErrors
}
ws.transactionStore.Put(modelRequest.Id.String(), modelRequest, nil)
// broadcast what we found.
if len(cleanedErrors) > 0 {
ws.streamChan <- cleanedErrors
ws.broadcastRequestValidationErrors(modelRequest, cleanedErrors, transaction)
} else {
ws.broadcastRequest(modelRequest, transaction)
}
return cleanedErrors
}