mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
f8cf6ea91c
* make context type value header configurable * populate config
57 lines
1.2 KiB
Go
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
|
|
}
|