1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package gremlin
import (
"context"
"errors"
"net/http"
"net/url"
"testing"
"github.com/jessevdk/go-flags"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
func TestConfigParsing(t *testing.T) {
var cfg Config
_, err := flags.ParseArgs(&cfg, []string{
"--disable-expansion",
"--endpoint", "http://localhost:8182/gremlin",
})
assert.NoError(t, err)
assert.True(t, cfg.DisableExpansion)
assert.Equal(t, "http", cfg.Endpoint.Scheme)
assert.Equal(t, "http://localhost:8182/gremlin", cfg.Endpoint.String())
cfg = Config{}
_, err = flags.ParseArgs(&cfg, nil)
assert.NoError(t, err)
assert.NotNil(t, cfg.Endpoint.URL)
}
func TestConfigBuild(t *testing.T) {
tests := []struct {
name string
cfg Config
opts []Option
wantErr bool
}{
{
name: "HTTP",
cfg: Config{
Endpoint: Endpoint{
URL: func() *url.URL {
u, _ := url.Parse("http://gremlin-server/gremlin")
return u
}(),
},
},
},
{
name: "NoScheme",
cfg: Config{
Endpoint: Endpoint{
URL: &url.URL{},
},
},
wantErr: true,
},
{
name: "BadScheme",
cfg: Config{
Endpoint: Endpoint{
URL: &url.URL{
Scheme: "bad",
},
},
},
wantErr: true,
},
{
name: "WithOptions",
cfg: Config{
Endpoint: Endpoint{
URL: func() *url.URL {
u, _ := url.Parse("http://gremlin-server/gremlin")
return u
}(),
},
DisableExpansion: true,
},
opts: []Option{WithHTTPClient(&http.Client{})},
},
{
name: "NoExpansion",
cfg: Config{
Endpoint: Endpoint{
URL: &url.URL{
Scheme: "bad",
},
},
DisableExpansion: true,
},
wantErr: true,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
client, err := tc.cfg.Build(tc.opts...)
if !tc.wantErr {
assert.NotNil(t, client)
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}
type testRoundTripper struct{ mock.Mock }
func (rt *testRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
args := rt.Called(req)
rsp, _ := args.Get(0).(*http.Response)
return rsp, args.Error(1)
}
func TestBuildWithHTTPClient(t *testing.T) {
var transport testRoundTripper
transport.On("RoundTrip", mock.Anything).
Return(nil, errors.New("noop")).
Once()
defer transport.AssertExpectations(t)
u, err := url.Parse("http://gremlin-server:8182/gremlin")
require.NoError(t, err)
client, err := Config{Endpoint: Endpoint{u}}.
Build(WithHTTPClient(&http.Client{Transport: &transport}))
require.NoError(t, err)
_, _ = client.Do(context.Background(), NewEvalRequest("g.V()"))
}
func TestExpandOrdering(t *testing.T) {
var cfg Config
cfg.Endpoint.URL, _ = url.Parse("http://gremlin-server/gremlin")
interceptor := func(RoundTripper) RoundTripper {
return RoundTripperFunc(func(ctx context.Context, req *Request) (*Response, error) {
assert.Equal(t, `g.V().hasLabel("user")`, req.Arguments[ArgsGremlin])
assert.Nil(t, req.Arguments[ArgsBindings])
return nil, errors.New("noop")
})
}
c, err := cfg.Build(WithInterceptor(interceptor))
require.NoError(t, err)
req := NewEvalRequest("g.V().hasLabel($1)", WithBindings(map[string]interface{}{"$1": "user"}))
_, _ = c.Do(context.Background(), req)
}
|