|
| 1 | +// Copyright 2013 The Go Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file or at |
| 5 | +// https://developers.google.com/open-source/licenses/bsd. |
| 6 | + |
| 7 | +package header |
| 8 | + |
| 9 | +import ( |
| 10 | + "net/http" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/google/go-cmp/cmp" |
| 14 | +) |
| 15 | + |
| 16 | +var parseAcceptTests = []struct { |
| 17 | + s string |
| 18 | + expected []AcceptSpec |
| 19 | +}{ |
| 20 | + {"text/html", []AcceptSpec{{"text/html", 1}}}, |
| 21 | + {"text/html; q=0", []AcceptSpec{{"text/html", 0}}}, |
| 22 | + {"text/html; q=0.0", []AcceptSpec{{"text/html", 0}}}, |
| 23 | + {"text/html; q=1", []AcceptSpec{{"text/html", 1}}}, |
| 24 | + {"text/html; q=1.0", []AcceptSpec{{"text/html", 1}}}, |
| 25 | + {"text/html; q=0.1", []AcceptSpec{{"text/html", 0.1}}}, |
| 26 | + {"text/html;q=0.1", []AcceptSpec{{"text/html", 0.1}}}, |
| 27 | + {"text/html, text/plain", []AcceptSpec{{"text/html", 1}, {"text/plain", 1}}}, |
| 28 | + {"text/html; q=0.1, text/plain", []AcceptSpec{{"text/html", 0.1}, {"text/plain", 1}}}, |
| 29 | + {"iso-8859-5, unicode-1-1;q=0.8,iso-8859-1", []AcceptSpec{{"iso-8859-5", 1}, {"unicode-1-1", 0.8}, {"iso-8859-1", 1}}}, |
| 30 | + {"iso-8859-1", []AcceptSpec{{"iso-8859-1", 1}}}, |
| 31 | + {"*", []AcceptSpec{{"*", 1}}}, |
| 32 | + {"da, en-gb;q=0.8, en;q=0.7", []AcceptSpec{{"da", 1}, {"en-gb", 0.8}, {"en", 0.7}}}, |
| 33 | + {"da, q, en-gb;q=0.8", []AcceptSpec{{"da", 1}, {"q", 1}, {"en-gb", 0.8}}}, |
| 34 | + {"image/png, image/*;q=0.5", []AcceptSpec{{"image/png", 1}, {"image/*", 0.5}}}, |
| 35 | + |
| 36 | + // bad cases |
| 37 | + {"value1; q=0.1.2", []AcceptSpec{{"value1", 0.1}}}, |
| 38 | + {"da, en-gb;q=foo", []AcceptSpec{{"da", 1}}}, |
| 39 | +} |
| 40 | + |
| 41 | +func TestParseAccept(t *testing.T) { |
| 42 | + for _, tt := range parseAcceptTests { |
| 43 | + header := http.Header{"Accept": {tt.s}} |
| 44 | + actual := ParseAccept(header, "Accept") |
| 45 | + if !cmp.Equal(actual, tt.expected) { |
| 46 | + t.Errorf("ParseAccept(h, %q)=%v, want %v", tt.s, actual, tt.expected) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments