-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
icinga2: Check errors in HTTP status and JSON
The Icinga 2 Event Stream connection did not validate the HTTP status code returned by the server. Consequently, HTTP errors were not identified and their error messages were incorrectly interpreted as valid messages. This occurred in two places: firstly, with regard to the HTTP status code, and secondly, in relation to the error field, which may be present within a JSON struct. This modification introduces a brief HTTP status validation method that adheres to the specifications outlined in the Icinga 2 API documentation and advanced error checks in UnmarshalEventStreamResponse.
- Loading branch information
Showing
4 changed files
with
89 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package icinga2 | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func TestCheckHTTPResponseStatusCode(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
status int | ||
wantErr assert.ErrorAssertionFunc | ||
}{ | ||
{"http-200", http.StatusOK, assert.NoError}, | ||
{"http-299", 299, assert.NoError}, | ||
{"http-204", http.StatusNoContent, assert.NoError}, | ||
{"http-401", http.StatusUnauthorized, assert.Error}, | ||
{"http-500", http.StatusInternalServerError, assert.Error}, | ||
{"http-900", 900, assert.Error}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.wantErr(t, checkHTTPResponseStatusCode(tt.status), "checkHTTPResponseStatusCode(%v)", tt.status) | ||
}) | ||
} | ||
} |