mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-09-02 03:00:41 +00:00
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>
This commit is contained in:
@@ -8,12 +8,16 @@ import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/martian/har"
|
||||
)
|
||||
|
||||
const mizuTestEnvVar = "MIZU_TEST"
|
||||
|
||||
type Protocol struct {
|
||||
Name string `json:"name"`
|
||||
LongName string `json:"longName"`
|
||||
@@ -262,27 +266,86 @@ type HTTPWrapper struct {
|
||||
}
|
||||
|
||||
func (h HTTPPayload) MarshalJSON() ([]byte, error) {
|
||||
_, testEnvEnabled := os.LookupEnv(mizuTestEnvVar)
|
||||
switch h.Type {
|
||||
case TypeHttpRequest:
|
||||
harRequest, err := har.NewRequest(h.Data.(*http.Request), true)
|
||||
if err != nil {
|
||||
return nil, errors.New("Failed converting request to HAR")
|
||||
}
|
||||
sort.Slice(harRequest.Headers, func(i, j int) bool {
|
||||
if harRequest.Headers[i].Name < harRequest.Headers[j].Name {
|
||||
return true
|
||||
}
|
||||
if harRequest.Headers[i].Name > harRequest.Headers[j].Name {
|
||||
return false
|
||||
}
|
||||
return harRequest.Headers[i].Value < harRequest.Headers[j].Value
|
||||
})
|
||||
sort.Slice(harRequest.QueryString, func(i, j int) bool {
|
||||
if harRequest.QueryString[i].Name < harRequest.QueryString[j].Name {
|
||||
return true
|
||||
}
|
||||
if harRequest.QueryString[i].Name > harRequest.QueryString[j].Name {
|
||||
return false
|
||||
}
|
||||
return harRequest.QueryString[i].Value < harRequest.QueryString[j].Value
|
||||
})
|
||||
if harRequest.PostData != nil {
|
||||
sort.Slice(harRequest.PostData.Params, func(i, j int) bool {
|
||||
if harRequest.PostData.Params[i].Name < harRequest.PostData.Params[j].Name {
|
||||
return true
|
||||
}
|
||||
if harRequest.PostData.Params[i].Name > harRequest.PostData.Params[j].Name {
|
||||
return false
|
||||
}
|
||||
return harRequest.PostData.Params[i].Value < harRequest.PostData.Params[j].Value
|
||||
})
|
||||
}
|
||||
if testEnvEnabled {
|
||||
harRequest.URL = ""
|
||||
}
|
||||
var reqWrapper *HTTPRequestWrapper
|
||||
if !testEnvEnabled {
|
||||
reqWrapper = &HTTPRequestWrapper{Request: h.Data.(*http.Request)}
|
||||
}
|
||||
return json.Marshal(&HTTPWrapper{
|
||||
Method: harRequest.Method,
|
||||
Details: harRequest,
|
||||
RawRequest: &HTTPRequestWrapper{Request: h.Data.(*http.Request)},
|
||||
RawRequest: reqWrapper,
|
||||
})
|
||||
case TypeHttpResponse:
|
||||
harResponse, err := har.NewResponse(h.Data.(*http.Response), true)
|
||||
if err != nil {
|
||||
return nil, errors.New("Failed converting response to HAR")
|
||||
}
|
||||
sort.Slice(harResponse.Headers, func(i, j int) bool {
|
||||
if harResponse.Headers[i].Name < harResponse.Headers[j].Name {
|
||||
return true
|
||||
}
|
||||
if harResponse.Headers[i].Name > harResponse.Headers[j].Name {
|
||||
return false
|
||||
}
|
||||
return harResponse.Headers[i].Value < harResponse.Headers[j].Value
|
||||
})
|
||||
sort.Slice(harResponse.Cookies, func(i, j int) bool {
|
||||
if harResponse.Cookies[i].Name < harResponse.Cookies[j].Name {
|
||||
return true
|
||||
}
|
||||
if harResponse.Cookies[i].Name > harResponse.Cookies[j].Name {
|
||||
return false
|
||||
}
|
||||
return harResponse.Cookies[i].Value < harResponse.Cookies[j].Value
|
||||
})
|
||||
var resWrapper *HTTPResponseWrapper
|
||||
if !testEnvEnabled {
|
||||
resWrapper = &HTTPResponseWrapper{Response: h.Data.(*http.Response)}
|
||||
}
|
||||
return json.Marshal(&HTTPWrapper{
|
||||
Method: "",
|
||||
Url: "",
|
||||
Details: harResponse,
|
||||
RawResponse: &HTTPResponseWrapper{Response: h.Data.(*http.Response)},
|
||||
RawResponse: resWrapper,
|
||||
})
|
||||
default:
|
||||
panic(fmt.Sprintf("HTTP payload cannot be marshaled: %v", h.Type))
|
||||
|
Reference in New Issue
Block a user