-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from nlnwa/fix/parse-resource-request
Fix parsing of query parameters when requesting a resource
- Loading branch information
Showing
2 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package warcserver | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
) | ||
|
||
func TestParseResourceRequest(t *testing.T) { | ||
tests := []struct { | ||
name string // test case name | ||
uri string // expected uri | ||
closest string // expected timestamp | ||
request *http.Request // test request | ||
}{ | ||
{ | ||
name: "Query parameters are not sorted during parsing", | ||
uri: "http://example.com?d=4&a=1&c=3&b=2#hei", | ||
closest: "20210101000000", | ||
request: func() *http.Request { | ||
r, _ := http.NewRequest("GET", "http://example.com", nil) | ||
|
||
ctx := context.WithValue(context.Background(), httprouter.ParamsKey, httprouter.Params{ | ||
httprouter.Param{ | ||
Key: "url", | ||
Value: "/http://example.com?d=4&a=1&c=3&b=2#hei", | ||
}, | ||
httprouter.Param{ | ||
Key: "timestamp", | ||
Value: "20210101000000id_", | ||
}, | ||
}) | ||
|
||
return r.WithContext(ctx) | ||
}(), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
uri, closest := parseResourceRequest(tt.request) | ||
if uri != tt.uri { | ||
t.Errorf("got %s, want %s", uri, tt.uri) | ||
} | ||
if closest != tt.closest { | ||
t.Errorf("got %s, want %s", closest, tt.closest) | ||
} | ||
}) | ||
} | ||
} |