-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gen-bundle: Add support for Variants (#539)
The index section of bundle maps URLs to a Variants value + a list of the responses for each possible Variant-Key (#450). But before this patch gen-bundle could not generate a bundle that have multiple variants for single URL. This patch teaches indexSection.Finalize() to generate entries with non-empty variants-value, based on the responses' Variant and Variant-Key headers [1]. [1] https://tools.ietf.org/html/draft-ietf-httpbis-variants-05
- Loading branch information
Showing
4 changed files
with
303 additions
and
24 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,100 @@ | ||
package bundle | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/WICG/webpackage/go/bundle/version" | ||
"github.com/WICG/webpackage/go/internal/testhelper" | ||
) | ||
|
||
func urlMustParse(rawurl string) *url.URL { | ||
u, err := url.Parse(rawurl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return u | ||
} | ||
|
||
func TestVariants(t *testing.T) { | ||
variants, err := parseVariants("Accept-Encoding;gzip;br, Accept-Language;en;fr;ja") | ||
if err != nil { | ||
t.Errorf("parseListOfStringLists unexpectedly failed: %v", err) | ||
} | ||
if nk, err := variants.numberOfPossibleKeys(); nk != 6 || err != nil { | ||
t.Errorf("numberOfPossibleKeys: got: (%v, %v) want: (%v, %v)", nk, err, 6, nil) | ||
} | ||
|
||
cases := []struct { | ||
index int | ||
variantKey []string | ||
}{ | ||
{0, []string{"gzip", "en"}}, | ||
{1, []string{"gzip", "fr"}}, | ||
{2, []string{"gzip", "ja"}}, | ||
{3, []string{"br", "en"}}, | ||
{4, []string{"br", "fr"}}, | ||
{5, []string{"br", "ja"}}, | ||
{-1, []string{"gzip", "es"}}, | ||
{-1, []string{}}, | ||
{-1, []string{"gzip"}}, | ||
{-1, []string{"gzip", "en", "foo"}}, | ||
} | ||
for _, c := range cases { | ||
if i := variants.indexInPossibleKeys(c.variantKey); i != c.index { | ||
t.Errorf("indexInPossibleKeys: got: %v want: %v", i, c.index) | ||
} | ||
|
||
if c.index != -1 { | ||
key := variants.possibleKeyAt(c.index) | ||
if !reflect.DeepEqual(key, c.variantKey) { | ||
t.Errorf("possibleKeyAt(%d): got: %v\nwant: %v", c.index, key, c.variantKey) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func TestIndexSectionWithVariants(t *testing.T) { | ||
url := urlMustParse("https://example.com/") | ||
variants := []string{"Accept-Encoding;gzip;br, Accept-Language;en;fr"} | ||
is := &indexSection{} | ||
is.addExchange( | ||
&Exchange{ | ||
Request{URL: url}, | ||
Response{Header: http.Header{ | ||
"Variants": variants, | ||
"Variant-Key": []string{"gzip;fr, br;en"}, | ||
}}, | ||
}, 20, 2) | ||
is.addExchange( | ||
&Exchange{ | ||
Request{URL: url}, | ||
Response{Header: http.Header{ | ||
"Variants": variants, | ||
"Variant-Key": []string{"gzip;en"}, | ||
}}, | ||
}, 10, 1) | ||
is.addExchange( | ||
&Exchange{ | ||
Request{URL: url}, | ||
Response{Header: http.Header{ | ||
"Variants": variants, | ||
"Variant-Key": []string{"br;fr"}, | ||
}}, | ||
}, 30, 3) | ||
if err := is.Finalize(version.VersionB1); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
want := `map["https://example.com/":["Accept-Encoding;gzip;br, Accept-Language;en;fr" 10 1 20 2 20 2 30 3]]` | ||
|
||
got, err := testhelper.CborBinaryToReadableString(is.bytes) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got != want { | ||
t.Errorf("got: %s\nwant: %s", got, want) | ||
} | ||
} |
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
Oops, something went wrong.