Extract getMatchPart method in protocols

dev
Alexey Zhuchkov 2021-10-29 19:10:17 +03:00
parent 3a47413cd4
commit bfb69b2ff2
5 changed files with 73 additions and 69 deletions

View File

@ -16,13 +16,7 @@ import (
// Match matches a generic data response again a given matcher
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
partString := matcher.Part
switch partString {
case "body", "all", "":
partString = "raw"
}
item, ok := data[partString]
item, ok := request.getMatchPart(matcher.Part, data)
if !ok {
return false, []string{}
}
@ -50,25 +44,32 @@ func (request *Request) Match(data map[string]interface{}, matcher *matchers.Mat
// Extract performs extracting operation for an extractor on model and returns true or false.
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
part := extractor.Part
item, ok := request.getMatchPart(extractor.Part, data)
if !ok {
return nil
}
switch extractor.GetType() {
case extractors.RegexExtractor:
return extractor.ExtractRegex(types.ToString(item))
case extractors.KValExtractor:
return extractor.ExtractKval(data)
}
return nil
}
func (request *Request) getMatchPart(part string, data output.InternalEvent) (interface{}, bool) {
switch part {
case "body", "all":
case "body", "all", "":
part = "raw"
}
item, ok := data[part]
if !ok {
return nil
return "", false
}
itemStr := types.ToString(item)
switch extractor.GetType() {
case extractors.RegexExtractor:
return extractor.ExtractRegex(itemStr)
case extractors.KValExtractor:
return extractor.ExtractKval(data)
}
return nil
return item, true
}
// responseToDSLMap converts a DNS response to a map for use in DSL matching

View File

@ -16,17 +16,10 @@ import (
// Match matches a generic data response again a given matcher
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
partString := matcher.Part
switch partString {
case "body", "all", "data", "":
partString = "raw"
}
item, ok := data[partString]
itemStr, ok := request.getMatchPart(matcher.Part, data)
if !ok {
return false, []string{}
}
itemStr := types.ToString(item)
switch matcher.GetType() {
case matchers.SizeMatcher:
@ -45,17 +38,10 @@ func (request *Request) Match(data map[string]interface{}, matcher *matchers.Mat
// Extract performs extracting operation for an extractor on model and returns true or false.
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
partString := extractor.Part
switch partString {
case "body", "all", "data", "":
partString = "raw"
}
item, ok := data[partString]
itemStr, ok := request.getMatchPart(extractor.Part, data)
if !ok {
return nil
}
itemStr := types.ToString(item)
switch extractor.GetType() {
case extractors.RegexExtractor:
@ -66,6 +52,21 @@ func (request *Request) Extract(data map[string]interface{}, extractor *extracto
return nil
}
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
switch part {
case "body", "all", "data", "":
part = "raw"
}
item, ok := data[part]
if !ok {
return "", false
}
itemStr := types.ToString(item)
return itemStr, true
}
// responseToDSLMap converts a file response to a map for use in DSL matching
func (request *Request) responseToDSLMap(raw, inputFilePath, matchedFileName string) output.InternalEvent {
return output.InternalEvent{

View File

@ -14,17 +14,10 @@ import (
// Match matches a generic data response again a given matcher
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
partString := matcher.Part
switch partString {
case "body", "resp", "":
partString = "data"
}
item, ok := data[partString]
itemStr, ok := request.getMatchPart(matcher.Part, data)
if !ok {
return false, []string{}
}
itemStr := types.ToString(item)
switch matcher.GetType() {
case matchers.SizeMatcher:
@ -43,17 +36,10 @@ func (request *Request) Match(data map[string]interface{}, matcher *matchers.Mat
// Extract performs extracting operation for an extractor on model and returns true or false.
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
partString := extractor.Part
switch partString {
case "body", "resp", "":
partString = "data"
}
item, ok := data[partString]
itemStr, ok := request.getMatchPart(extractor.Part, data)
if !ok {
return nil
}
itemStr := types.ToString(item)
switch extractor.GetType() {
case extractors.RegexExtractor:
@ -64,6 +50,21 @@ func (request *Request) Extract(data map[string]interface{}, extractor *extracto
return nil
}
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
switch part {
case "body", "resp", "":
part = "data"
}
item, ok := data[part]
if !ok {
return "", false
}
itemStr := types.ToString(item)
return itemStr, true
}
// responseToDSLMap converts a headless response to a map for use in DSL matching
func (request *Request) responseToDSLMap(resp, req, host, matched string) output.InternalEvent {
return output.InternalEvent{

View File

@ -17,7 +17,7 @@ import (
// Match matches a generic data response again a given matcher
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
item, ok := getMatchPart(matcher.Part, data)
item, ok := request.getMatchPart(matcher.Part, data)
if !ok {
return false, []string{}
}
@ -57,7 +57,7 @@ func getStatusCode(data map[string]interface{}) (int, bool) {
// Extract performs extracting operation for an extractor on model and returns true or false.
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
item, ok := getMatchPart(extractor.Part, data)
item, ok := request.getMatchPart(extractor.Part, data)
if !ok {
return nil
}
@ -75,7 +75,7 @@ func (request *Request) Extract(data map[string]interface{}, extractor *extracto
}
// getMatchPart returns the match part honoring "all" matchers + others.
func getMatchPart(part string, data output.InternalEvent) (string, bool) {
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
if part == "header" {
part = "all_headers"
}

View File

@ -14,17 +14,10 @@ import (
// Match matches a generic data response again a given matcher
func (request *Request) Match(data map[string]interface{}, matcher *matchers.Matcher) (bool, []string) {
partString := matcher.Part
switch partString {
case "body", "all", "":
partString = "data"
}
item, ok := data[partString]
itemStr, ok := request.getMatchPart(matcher.Part, data)
if !ok {
return false, []string{}
}
itemStr := types.ToString(item)
switch matcher.GetType() {
case matchers.SizeMatcher:
@ -43,17 +36,10 @@ func (request *Request) Match(data map[string]interface{}, matcher *matchers.Mat
// Extract performs extracting operation for an extractor on model and returns true or false.
func (request *Request) Extract(data map[string]interface{}, extractor *extractors.Extractor) map[string]struct{} {
partString := extractor.Part
switch partString {
case "body", "all", "":
partString = "data"
}
item, ok := data[partString]
itemStr, ok := request.getMatchPart(extractor.Part, data)
if !ok {
return nil
}
itemStr := types.ToString(item)
switch extractor.GetType() {
case extractors.RegexExtractor:
@ -64,6 +50,21 @@ func (request *Request) Extract(data map[string]interface{}, extractor *extracto
return nil
}
func (request *Request) getMatchPart(part string, data output.InternalEvent) (string, bool) {
switch part {
case "body", "all", "":
part = "data"
}
item, ok := data[part]
if !ok {
return "", false
}
itemStr := types.ToString(item)
return itemStr, true
}
// responseToDSLMap converts a network response to a map for use in DSL matching
func (request *Request) responseToDSLMap(req, resp, raw, host, matched string) output.InternalEvent {
return output.InternalEvent{