influxdb/http/write_handler_test.go

72 lines
1.6 KiB
Go
Raw Normal View History

2018-10-10 17:42:31 +00:00
package http
import (
2018-10-12 02:42:42 +00:00
"compress/gzip"
2018-10-10 17:42:31 +00:00
"context"
"io"
2018-10-12 02:42:42 +00:00
"io/ioutil"
2018-10-10 17:42:31 +00:00
"net/http"
"net/http/httptest"
2018-10-12 02:42:42 +00:00
"strings"
2018-10-10 17:42:31 +00:00
"testing"
"github.com/influxdata/platform"
)
func TestWriteService_Write(t *testing.T) {
type args struct {
org platform.ID
bucket platform.ID
r io.Reader
}
tests := []struct {
name string
args args
status int
2018-10-12 02:42:42 +00:00
want string
2018-10-10 17:42:31 +00:00
wantErr bool
}{
2018-10-12 02:42:42 +00:00
{
args: args{
org: 1,
bucket: 2,
r: strings.NewReader("m,t1=v1 f1=2"),
},
status: http.StatusNoContent,
want: "m,t1=v1 f1=2",
},
2018-10-10 17:42:31 +00:00
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2018-10-12 02:42:42 +00:00
var org, bucket *platform.ID
var lp []byte
2018-10-10 17:42:31 +00:00
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2018-10-12 02:42:42 +00:00
org, _ = platform.IDFromString(r.URL.Query().Get("org"))
bucket, _ = platform.IDFromString(r.URL.Query().Get("bucket"))
defer r.Body.Close()
in, _ := gzip.NewReader(r.Body)
defer in.Close()
lp, _ = ioutil.ReadAll(in)
2018-10-10 17:42:31 +00:00
w.WriteHeader(tt.status)
}))
s := &WriteService{
2018-10-12 02:42:42 +00:00
Addr: ts.URL,
2018-10-10 17:42:31 +00:00
}
2018-10-12 02:42:42 +00:00
if err := s.Write(context.Background(), tt.args.org, tt.args.bucket, tt.args.r); (err != nil) != tt.wantErr {
2018-10-10 17:42:31 +00:00
t.Errorf("WriteService.Write() error = %v, wantErr %v", err, tt.wantErr)
}
2018-10-12 02:42:42 +00:00
if got, want := *org, tt.args.org; got != want {
t.Errorf("WriteService.Write() org = %v, want %v", got, want)
}
if got, want := *bucket, tt.args.bucket; got != want {
t.Errorf("WriteService.Write() bucket = %v, want %v", got, want)
}
if got, want := string(lp), tt.want; got != want {
t.Errorf("WriteService.Write() = %v, want %v", got, want)
}
2018-10-10 17:42:31 +00:00
})
}
}