Skip to content

Commit

Permalink
check content type header (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
anvari1313 authored Feb 19, 2024
1 parent a692003 commit 664492b
Showing 1 changed file with 60 additions and 17 deletions.
77 changes: 60 additions & 17 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,6 @@ func (s *server) handle(writer http.ResponseWriter, req *http.Request) {
}
}

// JSONBytesEqual compares the JSON in two byte slices.
func JSONBytesEqual(a, b []byte) (bool, error) {
var json1, json2 interface{}
if err := json.Unmarshal(a, &json1); err != nil {
return false, err
}

if err := json.Unmarshal(b, &json2); err != nil {
return false, err
}

return reflect.DeepEqual(json2, json1), nil
}

type Job interface {
Do()
}
Expand Down Expand Up @@ -311,12 +297,43 @@ func (j *upstreamTestJob) Do() {
return
}

equalBody, err := JSONBytesEqual(mainResBody, testResBody)
mainResContentType := j.mainRes.Header.Get("content-type")
testResContentType := testRes.Header.Get("content-type")
if mainResContentType != testResContentType {
logging.L.Warn("NOT equal content-type to compare",
j.loggingFields(j.mainRes.StatusCode, testRes.StatusCode)...)
err = strg.Store(storage.Log{
URL: j.req.URL.String(),
Headers: j.req.Header,
MainUpstreamStatusCode: j.mainRes.StatusCode,
TestUpstreamStatusCode: testRes.StatusCode,
})
return
}

var comparator bodyEqualizerFunc
var responseSkipPath bool

switch strings.ToLower(mainResContentType) {
case "application/json", "application/ld+json":
responseSkipPath = true
comparator = JSONBytesEqual
// TODO: We didn't have time to implement it.
//case "application/xml", "application/xhtml+xml", "text/xml":
// responseSkipPath = false
// comparator = xmlBytesEqual
default:
responseSkipPath = false
comparator = dummyBytesEqual
}

equalBody, err := comparator(mainResBody, testResBody)
if err != nil {
logging.L.Error("error in JSON equality check of body request", j.loggingFieldsWithError(err)...)
logging.L.Error("error in response equality check", j.loggingFieldsWithError(err)...)
return
}
if !equalBody {

if !equalBody && responseSkipPath {
if testRes.StatusCode == j.mainRes.StatusCode {
srcBodyStr := string(mainResBody)
testBodyStr := string(testResBody)
Expand Down Expand Up @@ -359,3 +376,29 @@ func (j *upstreamTestJob) Do() {
}
}
}

type bodyEqualizerFunc func(a, b []byte) (bool, error)

// JSONBytesEqual compares the JSON in two byte slices.
func JSONBytesEqual(a, b []byte) (bool, error) {
var json1, json2 interface{}
if err := json.Unmarshal(a, &json1); err != nil {
return false, err
}

if err := json.Unmarshal(b, &json2); err != nil {
return false, err
}

return reflect.DeepEqual(json2, json1), nil
}

// xmlBytesEqual compares the JSON in two byte slices.
func xmlBytesEqual(a, b []byte) (bool, error) {
// TODO: Implement it in the future
return false, nil
}

func dummyBytesEqual(a, b []byte) (bool, error) {
return bytes.Equal(a, b), nil
}

0 comments on commit 664492b

Please sign in to comment.