Files
kubeshark/tap/extensions/http/helpers.go
M. Mert Yıldıran db427d91cc Add unit tests for HTTP dissector (#767)
* Add unit tests for HTTP dissector

* Ignore some fields on test environment

* Remove Git patches

* Don't have indent in the expected JSONs

* Fix more issues and update expected JSONs

* Refactor the test environment lookup

* Add a Makefile

* Include HTTP tests into the CI

* Fix the linting errors

* Fix another linting error

* Update the expected JSONs

* Sort `PostData.Params` params as well

* Move expected JSONs into `expect/dissect`

* Add `TestAnalyze` method

* Add `TestRepresent` method

* Add `TestRegister`, `TestMacros` and `TestPing` methods

* Test extensions first

* Remove expected JSONs

* Remove `bin` directory and update `Makefile` rules

* Update `.gitignore`

* Fix skipping download functionality in the Makefile

* Run `go mod tidy`

* Fix the race condition in the tests

* Revert "Test extensions first"

This reverts commit b8350cf139.

* Make `TEST_UPDATE` env lookup one-liner

* Update .github/workflows/test.yml

Co-authored-by: gadotroee <55343099+gadotroee@users.noreply.github.com>

* Add a newline

* Replace `ls` with `find`

Co-authored-by: gadotroee <55343099+gadotroee@users.noreply.github.com>
2022-02-09 13:34:52 +03:00

98 lines
2.3 KiB
Go

package http
import (
"encoding/json"
"fmt"
"reflect"
"sort"
"strconv"
"github.com/up9inc/mizu/tap/api"
)
func mapSliceRebuildAsMap(mapSlice []interface{}) (newMap map[string]interface{}) {
newMap = make(map[string]interface{})
for _, item := range mapSlice {
h := item.(map[string]interface{})
newMap[h["name"].(string)] = h["value"]
}
return
}
func mapSliceMergeRepeatedKeys(mapSlice []interface{}) (newMapSlice []interface{}) {
newMapSlice = make([]interface{}, 0)
valuesMap := make(map[string][]interface{})
for _, item := range mapSlice {
h := item.(map[string]interface{})
key := h["name"].(string)
valuesMap[key] = append(valuesMap[key], h["value"])
}
for key, values := range valuesMap {
h := make(map[string]interface{})
h["name"] = key
if len(values) == 1 {
h["value"] = values[0]
} else {
h["value"] = values
}
newMapSlice = append(newMapSlice, h)
}
sort.Slice(newMapSlice, func(i, j int) bool {
return newMapSlice[i].(map[string]interface{})["name"].(string) < newMapSlice[j].(map[string]interface{})["name"].(string)
})
return
}
func representMapSliceAsTable(mapSlice []interface{}, selectorPrefix string) (representation string) {
var table []api.TableData
for _, item := range mapSlice {
h := item.(map[string]interface{})
key := h["name"].(string)
value := h["value"]
switch reflect.TypeOf(value).Kind() {
case reflect.Slice:
fallthrough
case reflect.Array:
for i, el := range value.([]interface{}) {
selector := fmt.Sprintf("%s.%s[%d]", selectorPrefix, key, i)
table = append(table, api.TableData{
Name: fmt.Sprintf("%s [%d]", key, i),
Value: el,
Selector: selector,
})
}
default:
selector := fmt.Sprintf("%s[\"%s\"]", selectorPrefix, key)
table = append(table, api.TableData{
Name: key,
Value: value,
Selector: selector,
})
}
}
obj, _ := json.Marshal(table)
representation = string(obj)
return
}
func representSliceAsTable(slice []interface{}, selectorPrefix string) (representation string) {
var table []api.TableData
for i, item := range slice {
selector := fmt.Sprintf("%s[%d]", selectorPrefix, i)
table = append(table, api.TableData{
Name: strconv.Itoa(i),
Value: item,
Selector: selector,
})
}
obj, _ := json.Marshal(table)
representation = string(obj)
return
}