fix(webconnectivity): ignore any status code <= 0 (#579)

This diff changes the algorithm used by webconnectivity's
httpanalysis.go to ignore any status code <= 0 rather
than just ignoring the == 0 case.

Make sure we add test cases for when the control's status
code is negative rather than being zero.

While there, simplify code where boolean checks could be
more compact according to staticcheck.

Closes https://github.com/ooni/probe/issues/1825
This commit is contained in:
Simone Basso 2021-11-05 13:51:22 +01:00 committed by GitHub
commit 3b27780836
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 8 deletions

View file

@ -80,14 +80,14 @@ func HTTPStatusCodeMatch(tk urlgetter.TestKeys, ctrl ControlResponse) (out *bool
return // no real status code
}
measurement := tk.Requests[0].Response.Code
if control == 0 {
if control <= 0 {
return // no real status code
}
if measurement == 0 {
if measurement <= 0 {
return // no real status code
}
value := control == measurement
if value == true {
if value {
// if the status codes are equal, they clearly match
out = &value
return
@ -110,10 +110,10 @@ func HTTPHeadersMatch(tk urlgetter.TestKeys, ctrl ControlResponse) *bool {
if len(tk.Requests) <= 0 {
return nil
}
if tk.Requests[0].Response.Code == 0 {
if tk.Requests[0].Response.Code <= 0 {
return nil
}
if ctrl.HTTPRequest.StatusCode == 0 {
if ctrl.HTTPRequest.StatusCode <= 0 {
return nil
}
control := ctrl.HTTPRequest.Headers
@ -203,13 +203,13 @@ func HTTPTitleMatch(tk urlgetter.TestKeys, ctrl ControlResponse) (out *bool) {
return
}
response := tk.Requests[0].Response
if response.Code == 0 {
if response.Code <= 0 {
return
}
if response.BodyIsTruncated {
return
}
if ctrl.HTTPRequest.StatusCode == 0 {
if ctrl.HTTPRequest.StatusCode <= 0 {
return
}
control := ctrl.HTTPRequest.Title