|
1 | 1 | package v4
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "net/http" |
| 5 | + "net/url" |
4 | 6 | "testing"
|
5 | 7 | )
|
6 | 8 |
|
| 9 | +func lazyURLParse(v string) func() (*url.URL, error) { |
| 10 | + return func() (*url.URL, error) { |
| 11 | + return url.Parse(v) |
| 12 | + } |
| 13 | +} |
| 14 | + |
| 15 | +func TestGetURIPath(t *testing.T) { |
| 16 | + cases := map[string]struct { |
| 17 | + getURL func() (*url.URL, error) |
| 18 | + expect string |
| 19 | + }{ |
| 20 | + // Cases |
| 21 | + "with scheme": { |
| 22 | + getURL: lazyURLParse("https://localhost:9000"), |
| 23 | + expect: "/", |
| 24 | + }, |
| 25 | + "no port, with scheme": { |
| 26 | + getURL: lazyURLParse("https://localhost"), |
| 27 | + expect: "/", |
| 28 | + }, |
| 29 | + "without scheme": { |
| 30 | + getURL: lazyURLParse("localhost:9000"), |
| 31 | + expect: "/", |
| 32 | + }, |
| 33 | + "without scheme, with path": { |
| 34 | + getURL: lazyURLParse("localhost:9000/abc123"), |
| 35 | + expect: "/abc123", |
| 36 | + }, |
| 37 | + "without scheme, with separator": { |
| 38 | + getURL: lazyURLParse("//localhost:9000"), |
| 39 | + expect: "/", |
| 40 | + }, |
| 41 | + "no port, without scheme, with separator": { |
| 42 | + getURL: lazyURLParse("//localhost"), |
| 43 | + expect: "/", |
| 44 | + }, |
| 45 | + "without scheme, with separator, with path": { |
| 46 | + getURL: lazyURLParse("//localhost:9000/abc123"), |
| 47 | + expect: "/abc123", |
| 48 | + }, |
| 49 | + "no port, without scheme, with separator, with path": { |
| 50 | + getURL: lazyURLParse("//localhost/abc123"), |
| 51 | + expect: "/abc123", |
| 52 | + }, |
| 53 | + "opaque with query string": { |
| 54 | + getURL: lazyURLParse("localhost:9000/abc123?efg=456"), |
| 55 | + expect: "/abc123", |
| 56 | + }, |
| 57 | + "failing test": { |
| 58 | + getURL: func() (*url.URL, error) { |
| 59 | + endpoint := "https://service.region.amazonaws.com" |
| 60 | + req, _ := http.NewRequest("POST", endpoint, nil) |
| 61 | + u := req.URL |
| 62 | + |
| 63 | + u.Opaque = "//example.org/bucket/key-._~,!@#$%^&*()" |
| 64 | + |
| 65 | + query := u.Query() |
| 66 | + query.Set("some-query-key", "value") |
| 67 | + u.RawQuery = query.Encode() |
| 68 | + |
| 69 | + return u, nil |
| 70 | + }, |
| 71 | + expect: "/bucket/key-._~,!@#$%^&*()", |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + for name, c := range cases { |
| 76 | + t.Run(name, func(t *testing.T) { |
| 77 | + u, err := c.getURL() |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("failed to get URL, %v", err) |
| 80 | + } |
| 81 | + |
| 82 | + actual := GetURIPath(u) |
| 83 | + if e, a := c.expect, actual; e != a { |
| 84 | + t.Errorf("expect %v path, got %v", e, a) |
| 85 | + } |
| 86 | + }) |
| 87 | + } |
| 88 | +} |
| 89 | + |
7 | 90 | func TestStripExcessHeaders(t *testing.T) {
|
8 | 91 | vals := []string{
|
9 | 92 | "",
|
|
0 commit comments