|
| 1 | +package httpsign |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +func TestScRequestTarget(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + |
| 15 | + cases := []struct { |
| 16 | + name string |
| 17 | + u *url.URL |
| 18 | + want string |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "plain path", |
| 22 | + u: mustParseURL(t, "https://example.com/foo"), |
| 23 | + want: "/foo", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "path with query", |
| 27 | + u: mustParseURL(t, "https://example.com/foo?a=b"), |
| 28 | + want: "/foo?a=b", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "escaped slash in path preserved", |
| 32 | + u: mustParseURL(t, "https://example.com/a%2Fb"), |
| 33 | + want: "/a%2Fb", |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "escaped space in path preserved", |
| 37 | + u: mustParseURL(t, "https://example.com/a%20b"), |
| 38 | + want: "/a%20b", |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "escaped path with query", |
| 42 | + u: mustParseURL(t, "https://example.com/a%2Fb?x=1"), |
| 43 | + want: "/a%2Fb?x=1", |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "empty query delimiter preserved", |
| 47 | + u: mustParseURL(t, "https://example.com/path?"), |
| 48 | + want: "/path?", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "escaped path with empty query delimiter", |
| 52 | + u: mustParseURL(t, "https://example.com/a%2Fb?"), |
| 53 | + want: "/a%2Fb?", |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "empty path normalized to slash", |
| 57 | + u: mustParseURL(t, "https://example.com"), |
| 58 | + want: "/", |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "empty path with empty query delimiter", |
| 62 | + u: mustParseURL(t, "https://example.com?"), |
| 63 | + want: "/?", |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "raw path set without re-encoding via Path alone", |
| 67 | + u: &url.URL{Path: "/a/b", RawPath: "/a%2Fb"}, |
| 68 | + want: "/a%2Fb", |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + for _, tc := range cases { |
| 73 | + t.Run(tc.name, func(t *testing.T) { |
| 74 | + assert.Equal(t, tc.want, scRequestTarget(tc.u)) |
| 75 | + }) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestScPath(t *testing.T) { |
| 80 | + t.Parallel() |
| 81 | + |
| 82 | + cases := []struct { |
| 83 | + name string |
| 84 | + u *url.URL |
| 85 | + want string |
| 86 | + }{ |
| 87 | + {name: "plain path", u: mustParseURL(t, "https://example.com/foo"), want: "/foo"}, |
| 88 | + {name: "escaped slash preserved", u: mustParseURL(t, "https://example.com/a%2Fb"), want: "/a%2Fb"}, |
| 89 | + {name: "query ignored", u: mustParseURL(t, "https://example.com/foo?a=b"), want: "/foo"}, |
| 90 | + {name: "empty query delimiter ignored", u: mustParseURL(t, "https://example.com/path?"), want: "/path"}, |
| 91 | + {name: "empty path normalized to slash", u: mustParseURL(t, "https://example.com"), want: "/"}, |
| 92 | + {name: "raw path preserved", u: &url.URL{Path: "/a/b", RawPath: "/a%2Fb"}, want: "/a%2Fb"}, |
| 93 | + } |
| 94 | + |
| 95 | + for _, tc := range cases { |
| 96 | + t.Run(tc.name, func(t *testing.T) { |
| 97 | + assert.Equal(t, tc.want, scPath(tc.u)) |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestDerivedRequestTarget(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + |
| 105 | + cases := []struct { |
| 106 | + name string |
| 107 | + raw string |
| 108 | + wantRT string |
| 109 | + wantPath string |
| 110 | + }{ |
| 111 | + {name: "escaped slash", raw: "https://example.com/a%2Fb", wantRT: "/a%2Fb", wantPath: "/a%2Fb"}, |
| 112 | + {name: "empty query", raw: "https://example.com/path?", wantRT: "/path?", wantPath: "/path"}, |
| 113 | + {name: "escaped slash and empty query", raw: "https://example.com/a%2Fb?", wantRT: "/a%2Fb?", wantPath: "/a%2Fb"}, |
| 114 | + {name: "normal query", raw: "https://example.com/foo?a=b", wantRT: "/foo?a=b", wantPath: "/foo"}, |
| 115 | + {name: "empty path", raw: "https://example.com", wantRT: "/", wantPath: "/"}, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tc := range cases { |
| 119 | + t.Run(tc.name, func(t *testing.T) { |
| 120 | + req, err := http.NewRequest("GET", tc.raw, nil) |
| 121 | + require.NoError(t, err) |
| 122 | + msg, err := parseRequest(req, false, 0, "https") |
| 123 | + require.NoError(t, err) |
| 124 | + assert.Equal(t, tc.wantRT, msg.derived["@request-target"]) |
| 125 | + assert.Equal(t, tc.wantPath, msg.derived["@path"]) |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +func mustParseURL(t *testing.T, raw string) *url.URL { |
| 131 | + t.Helper() |
| 132 | + u, err := url.Parse(raw) |
| 133 | + require.NoError(t, err) |
| 134 | + return u |
| 135 | +} |
0 commit comments