(t *testing.T)
| 22 | ) |
| 23 | |
| 24 | func Test_Utils_GetOffer(t *testing.T) { |
| 25 | t.Parallel() |
| 26 | require.Empty(t, getOffer([]byte("hello"), acceptsOffer)) |
| 27 | require.Equal(t, "1", getOffer([]byte(""), acceptsOffer, "1")) |
| 28 | require.Empty(t, getOffer([]byte("2"), acceptsOffer, "1")) |
| 29 | |
| 30 | require.Empty(t, getOffer([]byte(""), acceptsOfferType)) |
| 31 | require.Empty(t, getOffer([]byte("text/html"), acceptsOfferType)) |
| 32 | require.Empty(t, getOffer([]byte("text/html"), acceptsOfferType, "application/json")) |
| 33 | require.Empty(t, getOffer([]byte("text/html;q=0"), acceptsOfferType, "text/html")) |
| 34 | require.Empty(t, getOffer([]byte("application/json, */*; q=0"), acceptsOfferType, "image/png")) |
| 35 | require.Equal(t, "application/xml", getOffer([]byte("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"), acceptsOfferType, "application/xml", "application/json")) |
| 36 | require.Equal(t, "text/html", getOffer([]byte("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"), acceptsOfferType, "text/html")) |
| 37 | require.Equal(t, "application/pdf", getOffer([]byte("text/plain;q=0,application/pdf;q=0.9,*/*;q=0.000"), acceptsOfferType, "application/pdf", "application/json")) |
| 38 | require.Equal(t, "application/pdf", getOffer([]byte("text/plain;q=0,application/pdf;q=0.9,*/*;q=0.000"), acceptsOfferType, "application/pdf", "application/json")) |
| 39 | require.Equal(t, "text/plain;a=1", getOffer([]byte("text/plain;a=1"), acceptsOfferType, "text/plain;a=1")) |
| 40 | require.Empty(t, getOffer([]byte("text/plain;a=1;b=2"), acceptsOfferType, "text/plain;b=2")) |
| 41 | |
| 42 | // Spaces, quotes, out of order params, and case insensitivity |
| 43 | require.Equal(t, "text/plain", getOffer([]byte("text/plain "), acceptsOfferType, "text/plain")) |
| 44 | require.Equal(t, "text/plain", getOffer([]byte("text/plain;q=0.4 "), acceptsOfferType, "text/plain")) |
| 45 | require.Equal(t, "text/plain", getOffer([]byte("text/plain;q=0.4 ;"), acceptsOfferType, "text/plain")) |
| 46 | require.Equal(t, "text/plain", getOffer([]byte("text/plain;q=0.4 ; p=foo"), acceptsOfferType, "text/plain")) |
| 47 | require.Equal(t, "text/plain;b=2;a=1", getOffer([]byte("text/plain ;a=1;b=2"), acceptsOfferType, "text/plain;b=2;a=1")) |
| 48 | require.Equal(t, "text/plain;a=1", getOffer([]byte("text/plain; a=1 "), acceptsOfferType, "text/plain;a=1")) |
| 49 | require.Equal(t, `text/plain;a="1;b=2\",text/plain"`, getOffer([]byte(`text/plain;a="1;b=2\",text/plain";q=0.9`), acceptsOfferType, `text/plain;a=1;b=2`, `text/plain;a="1;b=2\",text/plain"`)) |
| 50 | require.Equal(t, "text/plain;A=CAPS", getOffer([]byte(`text/plain;a="caPs"`), acceptsOfferType, "text/plain;A=CAPS")) |
| 51 | |
| 52 | // Priority |
| 53 | require.Equal(t, "text/plain", getOffer([]byte("text/plain"), acceptsOfferType, "text/plain", "text/plain;a=1")) |
| 54 | require.Equal(t, "text/plain;a=1", getOffer([]byte("text/plain"), acceptsOfferType, "text/plain;a=1", "", "text/plain")) |
| 55 | require.Equal(t, "text/plain;a=1", getOffer([]byte("text/plain,text/plain;a=1"), acceptsOfferType, "text/plain", "text/plain;a=1")) |
| 56 | require.Equal(t, "text/plain", getOffer([]byte("text/plain;q=0.899,text/plain;a=1;q=0.898"), acceptsOfferType, "text/plain", "text/plain;a=1")) |
| 57 | require.Equal(t, "text/plain;a=1;b=2", getOffer([]byte("text/plain,text/plain;a=1,text/plain;a=1;b=2"), acceptsOfferType, "text/plain", "text/plain;a=1", "text/plain;a=1;b=2")) |
| 58 | |
| 59 | // Takes the last value specified |
| 60 | require.Equal(t, "text/plain;a=1;b=2", getOffer([]byte("text/plain;a=1;b=1;B=2"), acceptsOfferType, "text/plain;a=1;b=1", "text/plain;a=1;b=2")) |
| 61 | |
| 62 | require.Empty(t, getOffer([]byte("utf-8, iso-8859-1;q=0.5"), acceptsOffer)) |
| 63 | require.Empty(t, getOffer([]byte("utf-8, iso-8859-1;q=0.5"), acceptsOffer, "ascii")) |
| 64 | require.Equal(t, "utf-8", getOffer([]byte("utf-8, iso-8859-1;q=0.5"), acceptsOffer, "utf-8")) |
| 65 | require.Equal(t, "iso-8859-1", getOffer([]byte("utf-8;q=0, iso-8859-1;q=0.5"), acceptsOffer, "utf-8", "iso-8859-1")) |
| 66 | |
| 67 | // Accept-Charset wildcard coverage |
| 68 | require.Equal(t, "utf-8", getOffer([]byte("utf-*"), acceptsOffer, "utf-8")) |
| 69 | require.Equal(t, "UTF-16", getOffer([]byte("utf-*"), acceptsOffer, "UTF-16", "iso-8859-1")) |
| 70 | require.Empty(t, getOffer([]byte("utf-*"), acceptsOffer, "iso-8859-1")) |
| 71 | require.Empty(t, getOffer([]byte("utf-*"), acceptsOffer, "utf")) |
| 72 | require.Empty(t, getOffer([]byte("utf-*"), acceptsOffer, "x-utf-8")) |
| 73 | |
| 74 | // Complex wildcard negotiation |
| 75 | require.Equal(t, "utf-16le", getOffer([]byte("utf-8;q=0.4, utf-*;q=0.8, iso-8859-1;q=0.6"), acceptsOffer, "iso-8859-1", "utf-16le")) |
| 76 | require.Equal(t, "iso-8859-1", getOffer([]byte("utf-*;q=0.9, iso-8859-1;q=1"), acceptsOffer, "x-utf-16", "iso-8859-1")) |
| 77 | require.Empty(t, getOffer([]byte("utf-*;q=0.5, iso-8859-1;q=0.4"), acceptsOffer, "ascii", "us-ascii")) |
| 78 | |
| 79 | require.Equal(t, "deflate", getOffer([]byte("gzip, deflate"), acceptsOffer, "deflate")) |
| 80 | require.Empty(t, getOffer([]byte("gzip, deflate;q=0"), acceptsOffer, "deflate")) |
| 81 |
nothing calls this directly
no test coverage detected