Refactor the test environment lookup

This commit is contained in:
M. Mert Yildiran
2022-02-07 23:32:11 +03:00
parent 85254e7b00
commit d91d73a485

View File

@@ -16,6 +16,8 @@ import (
"github.com/google/martian/har" "github.com/google/martian/har"
) )
const mizuTestEnvVar = "MIZU_TEST"
type Protocol struct { type Protocol struct {
Name string `json:"name"` Name string `json:"name"`
LongName string `json:"longName"` LongName string `json:"longName"`
@@ -262,6 +264,7 @@ type HTTPWrapper struct {
} }
func (h HTTPPayload) MarshalJSON() ([]byte, error) { func (h HTTPPayload) MarshalJSON() ([]byte, error) {
_, testEnvEnabled := os.LookupEnv("MIZU_TEST")
switch h.Type { switch h.Type {
case TypeHttpRequest: case TypeHttpRequest:
harRequest, err := har.NewRequest(h.Data.(*http.Request), true) harRequest, err := har.NewRequest(h.Data.(*http.Request), true)
@@ -286,12 +289,11 @@ func (h HTTPPayload) MarshalJSON() ([]byte, error) {
} }
return harRequest.QueryString[i].Value < harRequest.QueryString[j].Value return harRequest.QueryString[i].Value < harRequest.QueryString[j].Value
}) })
_, present := os.LookupEnv("MIZU_TEST") if testEnvEnabled {
if present {
harRequest.URL = "" harRequest.URL = ""
} }
var reqWrapper *HTTPRequestWrapper var reqWrapper *HTTPRequestWrapper
if !present { if !testEnvEnabled {
reqWrapper = &HTTPRequestWrapper{Request: h.Data.(*http.Request)} reqWrapper = &HTTPRequestWrapper{Request: h.Data.(*http.Request)}
} }
return json.Marshal(&HTTPWrapper{ return json.Marshal(&HTTPWrapper{
@@ -322,9 +324,8 @@ func (h HTTPPayload) MarshalJSON() ([]byte, error) {
} }
return harResponse.Cookies[i].Value < harResponse.Cookies[j].Value return harResponse.Cookies[i].Value < harResponse.Cookies[j].Value
}) })
_, present := os.LookupEnv("MIZU_TEST")
var resWrapper *HTTPResponseWrapper var resWrapper *HTTPResponseWrapper
if !present { if !testEnvEnabled {
resWrapper = &HTTPResponseWrapper{Response: h.Data.(*http.Response)} resWrapper = &HTTPResponseWrapper{Response: h.Data.(*http.Response)}
} }
return json.Marshal(&HTTPWrapper{ return json.Marshal(&HTTPWrapper{