test(http): add tests for write client
parent
dd8c2691ed
commit
d62e2e009a
|
@ -193,7 +193,7 @@ type WriteService struct {
|
|||
|
||||
var _ platform.WriteService = (*WriteService)(nil)
|
||||
|
||||
func (s *WriteService) Write(ctx context.Context, org, bucket platform.ID, r io.Reader) error {
|
||||
func (s *WriteService) Write(ctx context.Context, orgID, bucketID platform.ID, r io.Reader) error {
|
||||
u, err := newURL(s.Addr, writePath)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -213,9 +213,19 @@ func (s *WriteService) Write(ctx context.Context, org, bucket platform.ID, r io.
|
|||
req.Header.Set("Content-Encoding", "gzip")
|
||||
SetToken(s.Token, req)
|
||||
|
||||
org, err := orgID.Encode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bucket, err := bucketID.Encode()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params := req.URL.Query()
|
||||
params.Set("org", string(org.Encode()))
|
||||
params.Set("bucket", string(bucket.Encode()))
|
||||
params.Set("org", string(org))
|
||||
params.Set("bucket", string(bucket))
|
||||
req.URL.RawQuery = params.Encode()
|
||||
|
||||
hc := newClient(u.Scheme, s.InsecureSkipVerify)
|
||||
|
|
|
@ -1,51 +1,71 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/influxdata/platform"
|
||||
"github.com/influxdata/platform/models"
|
||||
)
|
||||
|
||||
func TestWriteService_Write(t *testing.T) {
|
||||
type fields struct {
|
||||
Token string
|
||||
InsecureSkipVerify bool
|
||||
}
|
||||
type args struct {
|
||||
ctx context.Context
|
||||
org platform.ID
|
||||
bucket platform.ID
|
||||
r io.Reader
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
status int
|
||||
want []models.Point
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
// TODO: Add test cases.
|
||||
{
|
||||
args: args{
|
||||
org: 1,
|
||||
bucket: 2,
|
||||
r: strings.NewReader("m,t1=v1 f1=2"),
|
||||
},
|
||||
status: http.StatusNoContent,
|
||||
want: "m,t1=v1 f1=2",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var org, bucket *platform.ID
|
||||
var lp []byte
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
r.URL.Query().Get(OrgID)
|
||||
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)
|
||||
w.WriteHeader(tt.status)
|
||||
}))
|
||||
s := &WriteService{
|
||||
Addr: ts.URL,
|
||||
Token: tt.fields.Token,
|
||||
InsecureSkipVerify: tt.fields.InsecureSkipVerify,
|
||||
Addr: ts.URL,
|
||||
}
|
||||
if err := s.Write(tt.args.ctx, tt.args.org, tt.args.bucket, tt.args.r); (err != nil) != tt.wantErr {
|
||||
if err := s.Write(context.Background(), tt.args.org, tt.args.bucket, tt.args.r); (err != nil) != tt.wantErr {
|
||||
t.Errorf("WriteService.Write() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue