Files
fleet/server/logging/webhook_test.go
T
Victor Lyuboslavsky c14bea44de Replaced all kitlog.Logger instances with the intermediate *logging.Logger (#40425)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or
remove if NA -->
**Related issue:** Resolves #40054

# Checklist for submitter

- [ ] Changes file added for user-visible changes in `changes/`,
`orbit/changes/` or `ee/fleetd-chrome/changes`.
  - Changes included in previous PR

## Testing

- [x] Added/updated automated tests
- [x] QA'd all new/changed functionality manually

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

* **Refactor**
* Consolidated and standardized internal logging infrastructure across
the application by adopting a unified logging package throughout the
codebase, replacing previous external logging dependencies.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-02-24 18:52:45 -06:00

63 lines
1.7 KiB
Go

package logging
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
platformlogging "github.com/fleetdm/fleet/v4/server/platform/logging"
"github.com/stretchr/testify/require"
)
func TestWebhookSubmission(t *testing.T) {
ctx := context.Background()
logger := platformlogging.NewNopLogger()
var body struct {
Timestamp time.Time `json:"timestamp"`
Details []json.RawMessage `json:"details"`
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "application/json", r.Header.Get("Content-Type"))
err := json.NewDecoder(r.Body).Decode(&body)
require.NoError(t, err)
}))
writer, err := NewWebhookLogWriter(server.URL, logger)
require.NoError(t, err)
logs := []json.RawMessage{
json.RawMessage(`{"pack":"fruit"}`),
json.RawMessage(`{"information":213}`),
json.RawMessage(`{"gordon":"freeman"}`),
}
err = writer.Write(ctx, logs)
require.NoError(t, err)
require.Len(t, body.Details, 3)
require.Equal(t, logs, body.Details)
}
func TestWebhookFailure(t *testing.T) {
ctx := context.Background()
logger := platformlogging.NewNopLogger()
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "bad", http.StatusBadRequest)
}))
writer, err := NewWebhookLogWriter(server.URL, logger)
require.NoError(t, err)
// Should always return no error and not stall, even if bad things happen
// Bad return code
err = writer.Write(ctx, []json.RawMessage{json.RawMessage("{}")})
require.NoError(t, err)
// No server
server.Close()
err = writer.Write(ctx, []json.RawMessage{json.RawMessage("{}")})
require.NoError(t, err)
}