repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

pico / pgs
Eric Bower · 20 Aug 24

header_test.go

  1package pgs
  2
  3import (
  4	"fmt"
  5	"testing"
  6
  7	"github.com/google/go-cmp/cmp"
  8)
  9
 10type HeaderFixture struct {
 11	name   string
 12	input  string
 13	expect []*HeaderRule
 14}
 15
 16func TestParseHeaderText(t *testing.T) {
 17	success := HeaderFixture{
 18		name:  "success",
 19		input: "/path\n\ttest: one",
 20		expect: []*HeaderRule{
 21			{
 22				Path: "/path",
 23				Headers: []*HeaderLine{
 24					{Name: "test", Value: "one"},
 25				},
 26			},
 27		},
 28	}
 29
 30	successIndex := HeaderFixture{
 31		name:  "successIndex",
 32		input: "/index.html\n\tX-Frame-Options: DENY",
 33		expect: []*HeaderRule{
 34			{
 35				Path: "/index.html",
 36				Headers: []*HeaderLine{
 37					{Name: "x-frame-options", Value: "DENY"},
 38				},
 39			},
 40		},
 41	}
 42
 43	compileList := ""
 44	for _, deny := range headerDenyList {
 45		compileList += fmt.Sprintf("\n\t%s: value", deny)
 46	}
 47
 48	denyList := HeaderFixture{
 49		name:  "denyList",
 50		input: fmt.Sprintf("/\n\tX-Frame-Options: DENY%s", compileList),
 51		expect: []*HeaderRule{
 52			{
 53				Path: "/",
 54				Headers: []*HeaderLine{
 55					{Name: "x-frame-options", Value: "DENY"},
 56				},
 57			},
 58		},
 59	}
 60
 61	multiValue := HeaderFixture{
 62		name:  "multiValue",
 63		input: "/*\n\tcache-control: max-age=0\n\tcache-control: no-cache\n\tcache-control: no-store\n\tcache-control: must-revalidate",
 64		expect: []*HeaderRule{
 65			{
 66				Path: "/*",
 67				Headers: []*HeaderLine{
 68					{Name: "cache-control", Value: "max-age=0"},
 69					{Name: "cache-control", Value: "no-cache"},
 70					{Name: "cache-control", Value: "no-store"},
 71					{Name: "cache-control", Value: "must-revalidate"},
 72				},
 73			},
 74		},
 75	}
 76
 77	comment := HeaderFixture{
 78		name:  "comment",
 79		input: "/path\n\t# comment\n\ttest: one",
 80		expect: []*HeaderRule{
 81			{
 82				Path: "/path",
 83				Headers: []*HeaderLine{
 84					{Name: "test", Value: "one"},
 85				},
 86			},
 87		},
 88	}
 89
 90	invalidName := HeaderFixture{
 91		name:   "invalidName",
 92		input:  "/path\n\t: value",
 93		expect: []*HeaderRule{},
 94	}
 95
 96	invalidValue := HeaderFixture{
 97		name:   "invalidValue",
 98		input:  "/path\n\ttest:",
 99		expect: []*HeaderRule{},
100	}
101
102	invalidForOrder := HeaderFixture{
103		name:   "invalidForOrder",
104		input:  "\ttest: one\n/path",
105		expect: []*HeaderRule{},
106	}
107
108	empty := HeaderFixture{
109		name:   "empty",
110		input:  "",
111		expect: []*HeaderRule{},
112	}
113
114	emptyLine := HeaderFixture{
115		name:  "emptyLine",
116		input: "/path\n\n\ttest: one",
117		expect: []*HeaderRule{
118			{
119				Path: "/path",
120				Headers: []*HeaderLine{
121					{Name: "test", Value: "one"},
122				},
123			},
124		},
125	}
126
127	duplicate := HeaderFixture{
128		name:  "duplicate",
129		input: "/path\n\ttest: one\n/path\n\ttest: two",
130		expect: []*HeaderRule{
131			{
132				Path: "/path",
133				Headers: []*HeaderLine{
134					{Name: "test", Value: "one"},
135				},
136			},
137			{
138				Path: "/path",
139				Headers: []*HeaderLine{
140					{Name: "test", Value: "two"},
141				},
142			},
143		},
144	}
145
146	noColon := HeaderFixture{
147		name:   "noColon",
148		input:  "/path\n\ttest = one",
149		expect: []*HeaderRule{},
150	}
151
152	fixtures := []HeaderFixture{
153		success,
154		successIndex,
155		denyList,
156		multiValue,
157		comment,
158		invalidName,
159		invalidValue,
160		invalidForOrder,
161		empty,
162		emptyLine,
163		duplicate,
164		noColon,
165	}
166
167	for _, fixture := range fixtures {
168		t.Run(fixture.name, func(t *testing.T) {
169			results, err := parseHeaderText(fixture.input)
170			if err != nil {
171				t.Error(err)
172			}
173			fmt.Println(results)
174			if cmp.Equal(results, fixture.expect) == false {
175				//nolint
176				t.Fatalf(cmp.Diff(fixture.expect, results))
177			}
178		})
179	}
180}