Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
61809cd1
Commit
61809cd1
authored
Jun 18, 2012
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net/url: sort keys in Encode; don't enumerate map randomly
R=golang-dev, adg CC=golang-dev
https://golang.org/cl/6303098
parent
f3474890
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
11 deletions
+27
-11
url.go
src/pkg/net/url/url.go
+16
-4
url_test.go
src/pkg/net/url/url_test.go
+11
-7
No files found.
src/pkg/net/url/url.go
View file @
61809cd1
...
...
@@ -7,7 +7,9 @@
package
url
import
(
"bytes"
"errors"
"sort"
"strconv"
"strings"
)
...
...
@@ -538,14 +540,24 @@ func (v Values) Encode() string {
if
v
==
nil
{
return
""
}
parts
:=
make
([]
string
,
0
,
len
(
v
))
// will be large enough for most uses
for
k
,
vs
:=
range
v
{
var
buf
bytes
.
Buffer
keys
:=
make
([]
string
,
0
,
len
(
v
))
for
k
:=
range
v
{
keys
=
append
(
keys
,
k
)
}
sort
.
Strings
(
keys
)
for
_
,
k
:=
range
keys
{
vs
:=
v
[
k
]
prefix
:=
QueryEscape
(
k
)
+
"="
for
_
,
v
:=
range
vs
{
parts
=
append
(
parts
,
prefix
+
QueryEscape
(
v
))
if
buf
.
Len
()
>
0
{
buf
.
WriteByte
(
'&'
)
}
buf
.
WriteString
(
prefix
)
buf
.
WriteString
(
QueryEscape
(
v
))
}
}
return
strings
.
Join
(
parts
,
"&"
)
return
buf
.
String
(
)
}
// resolvePath applies special path segments from refs and applies
...
...
src/pkg/net/url/url_test.go
View file @
61809cd1
...
...
@@ -453,20 +453,24 @@ func TestEscape(t *testing.T) {
//}
type
EncodeQueryTest
struct
{
m
Values
expected
string
expected1
string
m
Values
expected
string
}
var
encodeQueryTests
=
[]
EncodeQueryTest
{
{
nil
,
""
,
""
},
{
Values
{
"q"
:
{
"puppies"
},
"oe"
:
{
"utf8"
}},
"q=puppies&oe=utf8"
,
"oe=utf8&q=puppies"
},
{
Values
{
"q"
:
{
"dogs"
,
"&"
,
"7"
}},
"q=dogs&q=%26&q=7"
,
"q=dogs&q=%26&q=7"
},
{
nil
,
""
},
{
Values
{
"q"
:
{
"puppies"
},
"oe"
:
{
"utf8"
}},
"oe=utf8&q=puppies"
},
{
Values
{
"q"
:
{
"dogs"
,
"&"
,
"7"
}},
"q=dogs&q=%26&q=7"
},
{
Values
{
"a"
:
{
"a1"
,
"a2"
,
"a3"
},
"b"
:
{
"b1"
,
"b2"
,
"b3"
},
"c"
:
{
"c1"
,
"c2"
,
"c3"
},
},
"a=a1&a=a2&a=a3&b=b1&b=b2&b=b3&c=c1&c=c2&c=c3"
},
}
func
TestEncodeQuery
(
t
*
testing
.
T
)
{
for
_
,
tt
:=
range
encodeQueryTests
{
if
q
:=
tt
.
m
.
Encode
();
q
!=
tt
.
expected
&&
q
!=
tt
.
expected1
{
if
q
:=
tt
.
m
.
Encode
();
q
!=
tt
.
expected
{
t
.
Errorf
(
`EncodeQuery(%+v) = %q, want %q`
,
tt
.
m
,
q
,
tt
.
expected
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment