@marcel91200 / caddy Mirror · read-only
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 11, 01:00 UTC

master
go 61 lines 1.53 KB
Raw
1 package caddyhttp
2
3 import (
4 "net/http"
5 "testing"
6 )
7
8 func TestIsIncremental(t *testing.T) {
9 for _, tc := range []struct {
10 name string
11 values []string
12 want bool
13 }{
14 {name: "absent"},
15 {name: "true", values: []string{"?1"}, want: true},
16 {name: "false", values: []string{"?0"}},
17 {name: "surrounding space", values: []string{" ?1 "}, want: true},
18 {name: "unknown parameter", values: []string{"?1;q=0.5"}, want: true},
19 {name: "empty parameter", values: []string{"?1;"}},
20 {name: "trailing garbage", values: []string{"?1 junk"}},
21 {name: "integer", values: []string{"1"}},
22 {name: "token", values: []string{"true"}},
23 {name: "string", values: []string{`"?1"`}},
24 {name: "invalid boolean", values: []string{"?2"}},
25 {name: "empty value", values: []string{""}},
26 {name: "repeated field", values: []string{"?1", "?0"}},
27 } {
28 t.Run(tc.name, func(t *testing.T) {
29 hdr := http.Header{}
30 for _, v := range tc.values {
31 hdr.Add(incrementalHeader, v)
32 }
33 if got := IsIncremental(hdr); got != tc.want {
34 t.Errorf("IsIncremental(%q) = %v, want %v", tc.values, got, tc.want)
35 }
36 })
37 }
38 }
39
40 func BenchmarkIsIncremental(b *testing.B) {
41 for _, bc := range []struct {
42 name string
43 value string
44 }{
45 {name: "absent"},
46 {name: "true", value: "?1"},
47 {name: "false", value: "?0"},
48 {name: "true with param", value: "?1;q=0.5"},
49 } {
50 hdr := http.Header{}
51 if bc.value != "" {
52 hdr.Set(incrementalHeader, bc.value)
53 }
54 b.Run(bc.name, func(b *testing.B) {
55 b.ReportAllocs()
56 for b.Loop() {
57 IsIncremental(hdr)
58 }
59 })
60 }
61 }