LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package url | 5 package url |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | |
9 "encoding/gob" | |
10 "fmt" | 8 "fmt" |
11 "reflect" | 9 "reflect" |
12 "strings" | 10 "strings" |
13 "testing" | 11 "testing" |
14 ) | 12 ) |
15 | 13 |
16 type URLTest struct { | 14 type URLTest struct { |
17 in string | 15 in string |
18 out *URL | 16 out *URL |
19 roundtrip string // expected result of reserializing the URL; empty mean
s same as "in". | 17 roundtrip string // expected result of reserializing the URL; empty mean
s same as "in". |
(...skipping 844 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 func TestParseFailure(t *testing.T) { | 862 func TestParseFailure(t *testing.T) { |
865 // Test that the first parse error is returned. | 863 // Test that the first parse error is returned. |
866 const url = "%gh&%ij" | 864 const url = "%gh&%ij" |
867 _, err := ParseQuery(url) | 865 _, err := ParseQuery(url) |
868 errStr := fmt.Sprint(err) | 866 errStr := fmt.Sprint(err) |
869 if !strings.Contains(errStr, "%gh") { | 867 if !strings.Contains(errStr, "%gh") { |
870 t.Errorf(`ParseQuery(%q) returned error %q, want something conta
ining %q"`, url, errStr, "%gh") | 868 t.Errorf(`ParseQuery(%q) returned error %q, want something conta
ining %q"`, url, errStr, "%gh") |
871 } | 869 } |
872 } | 870 } |
873 | 871 |
874 func TestGobEncode(t *testing.T) { | 872 func TestParseUserinfo(t *testing.T) { |
875 » gob.Register(&URL{}) | |
876 tests := []string{ | 873 tests := []string{ |
877 » » "http://alberto:zudo@example.com/foo?bar=baz#foobar", | 874 » » "", |
878 » » "http://alberto@example.com/foo?bar=baz#foobar", | 875 » » "foo", |
879 » » "http://alberto:@example.com/foo?bar=baz#foobar", | 876 » » "foo:", |
880 » } | 877 » » "foo:bar", |
881 » for _, s := range tests { | 878 » » "fo%40o:bar", // @ |
882 » » url1, err := Parse(s) | 879 » » "foo%40:%2Fbar", // @ and / |
883 » » if err != nil { | 880 » } |
884 » » » t.Fatal(err) | 881 » for _, v := range tests { |
885 » » } | 882 » » if u, err := ParseUserinfo(v); err != nil { |
886 » » var buf bytes.Buffer | 883 » » » t.Errorf("error parsing valid Userinfo %q: %s", v, err) |
887 » » if err := gob.NewEncoder(&buf).Encode(url1); err != nil { | 884 » » } else if s := u.String(); s != v { |
888 » » » t.Fatal(err) | 885 » » » t.Errorf("incorrectly parsed Userinfo. Want %q, got %q."
, v, s) |
889 » » } | 886 » » } |
890 » » var url2 *URL | 887 » } |
891 » » if err := gob.NewDecoder(bytes.NewBuffer(buf.Bytes())).Decode(&u
rl2); err != nil { | 888 } |
892 » » » t.Fatal(err) | 889 |
893 » » } | 890 func TestParseInvalidUserinfo(t *testing.T) { |
894 » » if url1.String() != url2.String() { | 891 » tests := []string{ |
895 » » » t.Error("URL was not decoded correctly. Want %q, got %q"
, url1.String(), url2.String()) | 892 » » "::", |
896 » » } | 893 » » "foo:bar:baz", |
897 » } | 894 » } |
898 } | 895 » for _, v := range tests { |
| 896 » » if u, err := ParseUserinfo(v); err == nil { |
| 897 » » » t.Errorf("invalid Userinfo %q parsed as %v", v, u) |
| 898 » » } |
| 899 » } |
| 900 } |
LEFT | RIGHT |