Skip to content

Commit a762273

Browse files
committed
retain query string from original request
When resolving a go link, combine the query string parameters from the request and long URL. Updates #77 Signed-off-by: Will Norris <[email protected]>
1 parent ae5d8c9 commit a762273

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

golink.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ func serveGo(w http.ResponseWriter, r *http.Request) {
397397
stats.mu.Unlock()
398398

399399
login, _ := currentUser(r)
400-
target, err := expandLink(link.Long, expandEnv{Now: time.Now().UTC(), Path: remainder, user: login})
400+
env := expandEnv{Now: time.Now().UTC(), Path: remainder, user: login, query: r.URL.Query()}
401+
target, err := expandLink(link.Long, env)
401402
if err != nil {
402403
log.Printf("expanding %q: %v", link.Long, err)
403404
if errors.Is(err, errNoUser) {
@@ -475,6 +476,9 @@ type expandEnv struct {
475476
// user is the current user, if any.
476477
// For example, "[email protected]" or "foo@github".
477478
user string
479+
480+
// query is the query parameters from the original request.
481+
query url.Values
478482
}
479483

480484
var errNoUser = errors.New("no user")
@@ -521,6 +525,17 @@ func expandLink(long string, env expandEnv) (*url.URL, error) {
521525
return nil, err
522526
}
523527

528+
// add query parameters from original request
529+
if len(env.query) > 0 {
530+
query := u.Query()
531+
for key, values := range env.query {
532+
for _, v := range values {
533+
query.Add(key, v)
534+
}
535+
}
536+
u.RawQuery = query.Encode()
537+
}
538+
524539
return u, nil
525540
}
526541

golink_test.go

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ func TestServeGo(t *testing.T) {
5959
},
6060
{
6161
name: "simple link with query",
62-
link: "/who?q",
62+
link: "/who?q=1",
6363
wantStatus: http.StatusFound,
64-
wantLink: "http://who/", // TODO: eventually http://who/?q
64+
wantLink: "http://who/?q=1",
6565
},
6666
{
6767
name: "simple link with path and query",
68-
link: "/who/p?q",
68+
link: "/who/p?q=1",
6969
wantStatus: http.StatusFound,
70-
wantLink: "http://who/p", // TODO: eventually http://who/p?q
70+
wantLink: "http://who/p?q=1",
7171
},
7272
{
7373
name: "user link",
@@ -282,6 +282,7 @@ func TestExpandLink(t *testing.T) {
282282
long string // long URL for golink
283283
now time.Time // current time
284284
user string // current user resolving link
285+
query string // query string
285286
remainder string // remainder of URL path after golink name
286287
wantErr bool // whether we expect an error
287288
want string // expected redirect URL
@@ -372,11 +373,38 @@ func TestExpandLink(t *testing.T) {
372373
remainder: "a",
373374
want: "/rel/a",
374375
},
375-
376+
{
377+
name: "query-string",
378+
long: `/rel`,
379+
query: "a=b",
380+
want: "/rel?a=b",
381+
},
382+
{
383+
name: "path-and-query-string",
384+
long: `/rel`,
385+
remainder: "path",
386+
query: "a=b",
387+
want: "/rel/path?a=b",
388+
},
389+
{
390+
name: "combine-query-string",
391+
long: `/rel?a=1`,
392+
query: "a=2&b=2",
393+
want: "/rel?a=1&a=2&b=2",
394+
},
395+
{
396+
name: "template-and-combined-query-string",
397+
long: `/rel{{with .Path}}/{{.}}{{end}}?a=1`,
398+
remainder: "path",
399+
query: "b=2",
400+
want: "/rel/path?a=1&b=2",
401+
},
376402
}
377403
for _, tt := range tests {
378404
t.Run(tt.name, func(t *testing.T) {
379-
link, err := expandLink(tt.long, expandEnv{Now: tt.now, Path: tt.remainder, user: tt.user})
405+
query, _ := url.ParseQuery(tt.query)
406+
env := expandEnv{Now: tt.now, Path: tt.remainder, user: tt.user, query: query}
407+
link, err := expandLink(tt.long, env)
380408
if (err != nil) != tt.wantErr {
381409
t.Fatalf("expandLink(%q) returned error %v; want %v", tt.long, err, tt.wantErr)
382410
}

0 commit comments

Comments
 (0)