Skip to content

Commit 69a79f8

Browse files
committed
Encoding fixes
1 parent 95b3c3a commit 69a79f8

11 files changed

+98
-27
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ SCRIPTDIR := $(shell pwd)
33
ROOTDIR := $(shell cd $(SCRIPTDIR) && pwd)
44

55
GOBUILDDIR := $(SCRIPTDIR)/.gobuild
6-
GOVERSION := 1.7.5-alpine
6+
GOVERSION := 1.8-alpine
77

88
ARANGODB := arangodb:3.1.12
99
#ARANGODB := neunhoef/arangodb:3.2.devel-1
@@ -32,7 +32,7 @@ $(GOBUILDDIR):
3232

3333
DBCONTAINER := $(PROJECT)-test-db
3434

35-
run-tests: build run-tests-single-with-auth run-tests-single-no-auth
35+
run-tests: run-tests-single-with-auth run-tests-single-no-auth
3636

3737
run-tests-single-no-auth:
3838
@echo "Single server, no authentication"

client_databases_impl.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ package driver
2424

2525
import (
2626
"context"
27-
"net/url"
2827
"path"
2928
)
3029

3130
// Database opens a connection to an existing database.
3231
// If no database with given name exists, an NotFoundError is returned.
3332
func (c *client) Database(ctx context.Context, name string) (Database, error) {
34-
escapedName := url.QueryEscape(name)
33+
escapedName := pathEscape(name)
3534
req, err := c.conn.NewRequest("GET", path.Join("_db", escapedName, "_api/database/current"))
3635
if err != nil {
3736
return nil, WithStack(err)
@@ -52,7 +51,7 @@ func (c *client) Database(ctx context.Context, name string) (Database, error) {
5251

5352
// DatabaseExists returns true if a database with given name exists.
5453
func (c *client) DatabaseExists(ctx context.Context, name string) (bool, error) {
55-
escapedName := url.QueryEscape(name)
54+
escapedName := pathEscape(name)
5655
req, err := c.conn.NewRequest("GET", path.Join("_db", escapedName, "_api/database/current"))
5756
if err != nil {
5857
return false, WithStack(err)

client_users_impl.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ package driver
2424

2525
import (
2626
"context"
27-
"net/url"
2827
"path"
2928
)
3029

3130
// User opens a connection to an existing user.
3231
// If no user with given name exists, an NotFoundError is returned.
3332
func (c *client) User(ctx context.Context, name string) (User, error) {
34-
escapedName := url.QueryEscape(name)
33+
escapedName := pathEscape(name)
3534
req, err := c.conn.NewRequest("GET", path.Join("_api/user", escapedName))
3635
if err != nil {
3736
return nil, WithStack(err)
@@ -56,8 +55,8 @@ func (c *client) User(ctx context.Context, name string) (User, error) {
5655

5756
// UserExists returns true if a database with given name exists.
5857
func (c *client) UserExists(ctx context.Context, name string) (bool, error) {
59-
escapedName := url.QueryEscape(name)
60-
req, err := c.conn.NewRequest("GET", path.Join("_api/user", escapedName))
58+
escapedName := pathEscape(name)
59+
req, err := c.conn.NewRequest("GET", path.Join("_api", "user", escapedName))
6160
if err != nil {
6261
return false, WithStack(err)
6362
}

collection_document_impl.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ package driver
2525
import (
2626
"context"
2727
"fmt"
28-
"net/url"
2928
"path"
3029
"reflect"
3130
)
@@ -37,7 +36,7 @@ func (c *collection) ReadDocument(ctx context.Context, key string, result interf
3736
if err := validateKey(key); err != nil {
3837
return DocumentMeta{}, WithStack(err)
3938
}
40-
escapedKey := url.QueryEscape(key)
39+
escapedKey := pathEscape(key)
4140
req, err := c.conn.NewRequest("GET", path.Join(c.relPath("document"), escapedKey))
4241
if err != nil {
4342
return DocumentMeta{}, WithStack(err)
@@ -166,7 +165,7 @@ func (c *collection) UpdateDocument(ctx context.Context, key string, update inte
166165
if update == nil {
167166
return DocumentMeta{}, WithStack(InvalidArgumentError{Message: "update nil"})
168167
}
169-
escapedKey := url.QueryEscape(key)
168+
escapedKey := pathEscape(key)
170169
req, err := c.conn.NewRequest("PATCH", path.Join(c.relPath("document"), escapedKey))
171170
if err != nil {
172171
return DocumentMeta{}, WithStack(err)
@@ -275,7 +274,7 @@ func (c *collection) ReplaceDocument(ctx context.Context, key string, document i
275274
if document == nil {
276275
return DocumentMeta{}, WithStack(InvalidArgumentError{Message: "document nil"})
277276
}
278-
escapedKey := url.QueryEscape(key)
277+
escapedKey := pathEscape(key)
279278
req, err := c.conn.NewRequest("PUT", path.Join(c.relPath("document"), escapedKey))
280279
if err != nil {
281280
return DocumentMeta{}, WithStack(err)
@@ -380,7 +379,7 @@ func (c *collection) RemoveDocument(ctx context.Context, key string) (DocumentMe
380379
if err := validateKey(key); err != nil {
381380
return DocumentMeta{}, WithStack(err)
382381
}
383-
escapedKey := url.QueryEscape(key)
382+
escapedKey := pathEscape(key)
384383
req, err := c.conn.NewRequest("DELETE", path.Join(c.relPath("document"), escapedKey))
385384
if err != nil {
386385
return DocumentMeta{}, WithStack(err)

collection_impl.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ package driver
2424

2525
import (
2626
"context"
27-
"net/url"
2827
"path"
2928
)
3029

@@ -51,7 +50,7 @@ type collection struct {
5150

5251
// relPath creates the relative path to this collection (`_db/<db-name>/_api/<api-name>/<col-name>`)
5352
func (c *collection) relPath(apiName string) string {
54-
escapedName := url.QueryEscape(c.name)
53+
escapedName := pathEscape(c.name)
5554
return path.Join(c.db.relPath(), "_api", apiName, escapedName)
5655
}
5756

database_impl.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ package driver
2424

2525
import (
2626
"context"
27-
"net/url"
2827
"path"
2928
)
3029

@@ -50,7 +49,7 @@ type database struct {
5049

5150
// relPath creates the relative path to this database (`_db/<name>`)
5251
func (d *database) relPath() string {
53-
escapedName := url.QueryEscape(d.name)
52+
escapedName := pathEscape(d.name)
5453
return path.Join("_db", escapedName)
5554
}
5655

@@ -62,7 +61,7 @@ func (d *database) Name() string {
6261
// Collection opens a connection to an existing collection within the database.
6362
// If no collection with given name exists, an NotFoundError is returned.
6463
func (d *database) Collection(ctx context.Context, name string) (Collection, error) {
65-
escapedName := url.QueryEscape(name)
64+
escapedName := pathEscape(name)
6665
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection", escapedName))
6766
if err != nil {
6867
return nil, WithStack(err)
@@ -83,7 +82,7 @@ func (d *database) Collection(ctx context.Context, name string) (Collection, err
8382

8483
// CollectionExists returns true if a collection with given name exists within the database.
8584
func (d *database) CollectionExists(ctx context.Context, name string) (bool, error) {
86-
escapedName := url.QueryEscape(name)
85+
escapedName := pathEscape(name)
8786
req, err := d.conn.NewRequest("GET", path.Join(d.relPath(), "_api/collection", escapedName))
8887
if err != nil {
8988
return false, WithStack(err)

encode-go_1_8.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
// +build "go1.8"
24+
25+
package driver
26+
27+
import "net/url"
28+
29+
// Escape the given value for use in a URL path.
30+
func pathEscape(s string) string {
31+
return url.QueryEscape(s)
32+
}

encode.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
// Author Ewout Prangsma
21+
//
22+
23+
// +build !"go1.8"
24+
25+
package driver
26+
27+
import "net/url"
28+
29+
// Escape the given value for use in a URL path.
30+
func pathEscape(s string) string {
31+
return url.PathEscape(s)
32+
}

http/request.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"net/url"
3232
"reflect"
3333
"strconv"
34+
"strings"
3435

3536
driver "github.com/arangodb/go-driver"
3637
)
@@ -117,15 +118,27 @@ func (r *httpRequest) SetHeader(key, value string) driver.Request {
117118
// createHTTPRequest creates a golang http.Request based on the configured arguments.
118119
func (r *httpRequest) createHTTPRequest(endpoint url.URL) (*http.Request, error) {
119120
u := endpoint
120-
u.Path = r.path
121+
u.Path = ""
122+
url := u.String()
123+
if !strings.HasSuffix(url, "/") {
124+
url = url + "/"
125+
}
126+
p := r.path
127+
if strings.HasPrefix(p, "/") {
128+
p = p[1:]
129+
}
130+
url = url + p
121131
if r.q != nil {
122-
u.RawQuery = r.q.Encode()
132+
q := r.q.Encode()
133+
if len(q) > 0 {
134+
url = url + "?" + q
135+
}
123136
}
124137
var body io.Reader
125138
if r.body != nil {
126139
body = bytes.NewReader(r.body)
127140
}
128-
req, err := http.NewRequest(r.method, u.String(), body)
141+
req, err := http.NewRequest(r.method, url, body)
129142
if err != nil {
130143
return nil, driver.WithStack(err)
131144
}

test/cursor_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func TestCreateCursor(t *testing.T) {
140140
queryTestContext{driver.WithQueryCache(nil), false},
141141
queryTestContext{driver.WithQueryCache(nil, true), false},
142142
queryTestContext{driver.WithQueryCache(nil, false), false},
143-
queryTestContext{driver.WithQueryMemoryLimit(nil, 10000), false},
143+
queryTestContext{driver.WithQueryMemoryLimit(nil, 60000), false},
144144
queryTestContext{driver.WithQueryTTL(nil, time.Minute), false},
145145
queryTestContext{driver.WithQueryBatchSize(driver.WithQueryCount(nil), 1), true},
146146
queryTestContext{driver.WithQueryCache(driver.WithQueryCount(driver.WithQueryBatchSize(nil, 2))), true},

user_impl.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ package driver
2424

2525
import (
2626
"context"
27-
"net/url"
2827
"path"
2928
)
3029

@@ -56,7 +55,7 @@ type userData struct {
5655

5756
// relPath creates the relative path to this index (`_api/user/<name>`)
5857
func (u *user) relPath() string {
59-
escapedName := url.QueryEscape(u.data.Name)
58+
escapedName := pathEscape(u.data.Name)
6059
return path.Join("_api", "user", escapedName)
6160
}
6261

@@ -203,7 +202,7 @@ func (u *user) RevokeAccess(ctx context.Context, db Database) error {
203202

204203
// grant grants or revokes access to a database for this user.
205204
func (u *user) grant(ctx context.Context, db Database, access string) error {
206-
escapedDbName := url.QueryEscape(db.Name())
205+
escapedDbName := pathEscape(db.Name())
207206
req, err := u.conn.NewRequest("PUT", path.Join(u.relPath(), "database", escapedDbName))
208207
if err != nil {
209208
return WithStack(err)

0 commit comments

Comments
 (0)