fleet/server/logging/kafkarest_test.go
Benjamin Edwards f8cf6ea91c
make context type value header configurable (#4441)
* make context type value header configurable
* populate config
2022-03-09 17:22:29 -05:00

57 lines
1.2 KiB
Go

package logging
import (
"context"
"encoding/json"
"fmt"
"github.com/stretchr/testify/require"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
func TestKafkaRestWrite(t *testing.T) {
ctx := context.Background()
var buf []byte
var err error
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
buf, err = ioutil.ReadAll(r.Body)
require.NoError(t, err)
require.Equal(t, r.URL.Path, "/topics/foo")
require.Equal(t, r.Header.Get("Content-Type"), "foobar")
w.WriteHeader(200)
}))
defer server.Close()
producer := &kafkaRESTProducer{
client: server.Client(),
URL: fmt.Sprintf(krPublishTopicURL, server.URL, "foo"),
CheckURL: fmt.Sprintf(krCheckTopicURL, server.URL, "foo"),
ContentTypeValue: "foobar",
}
err = producer.Write(ctx, logs)
require.NoError(t, err)
expected := makeKafkaRecords(logs)
var actual kafkaRecords
err = json.Unmarshal(buf, &actual)
require.NoError(t, err)
require.Equal(t, expected, actual)
}
func makeKafkaRecords(messages []json.RawMessage) kafkaRecords {
data := kafkaRecords{
Records: make([]kafkaValue, len(messages)),
}
for i, log := range messages {
data.Records[i] = kafkaValue{
Value: log,
}
}
return data
}