File: headers_test.go

package info (click to toggle)
golang-codeberg-git-pages-go-headers 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52 kB
  • sloc: makefile: 2
file content (139 lines) | stat: -rw-r--r-- 2,781 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package headers_test

import (
	"fmt"
	"net/http"
	"reflect"
	"strings"
	"testing"

	"codeberg.org/git-pages/go-headers"
)

func TestExample(t *testing.T) {
	rules := headers.Must(headers.ParseString(`
# enable COOP/COEP on index:
/index.html
  cross-origin-opener-policy: same-origin
  # breaks some use cases
  cross-origin-embedder-policy: credentialless
# on the worker too:
/worker.js
  cross-origin-embedder-policy: credentialless
/*
  x-content-type-options: nosniff
`))
	expected := []headers.Rule{
		{
			Path: "/index.html",
			Headers: http.Header{
				"Cross-Origin-Opener-Policy":   []string{"same-origin"},
				"Cross-Origin-Embedder-Policy": []string{"credentialless"},
			},
		},
		{
			Path: "/worker.js",
			Headers: http.Header{
				"Cross-Origin-Embedder-Policy": []string{"credentialless"},
			},
		},
		{
			Path: "/*",
			Headers: http.Header{
				"X-Content-Type-Options": []string{"nosniff"},
			},
		},
	}
	if !reflect.DeepEqual(rules, expected) {
		t.Fail()
	}
}

func TestErrorLoneHeaderLine(t *testing.T) {
	_, err := headers.ParseString(`
  vary: accept
`)
	if !strings.Contains(fmt.Sprintf("%s", err), "header line without preceding path line") {
		t.Fail()
	}
}

func TestErrorMalformedHeader(t *testing.T) {
	_, err := headers.ParseString(`
/index.html
  malformed
`)
	if !strings.Contains(fmt.Sprintf("%s", err), "header line without separator") {
		t.Fail()
	}
}

func TestMultipleHeaders(t *testing.T) {
	rules := headers.Must(headers.ParseString(`
/index.html
  vary: accept
  vary: x-something
`))
	expected := []headers.Rule{
		{
			Path: "/index.html",
			Headers: http.Header{
				"Vary": []string{"accept", "x-something"},
			},
		},
	}
	if !reflect.DeepEqual(rules, expected) {
		t.Fail()
	}
}

func TestNameNormalization(t *testing.T) {
	rules := headers.Must(headers.ParseString(`
/index.html
  ALLOW: OPTIONS
  allow: GET
`))
	expected := []headers.Rule{
		{
			Path: "/index.html",
			Headers: http.Header{
				"Allow": []string{"OPTIONS", "GET"},
			},
		},
	}
	if !reflect.DeepEqual(rules, expected) {
		t.Fail()
	}
}

func TestSpaceTrimming(t *testing.T) {
	rules := headers.Must(headers.ParseString("/* \n  Access-Control-Allow-Methods: GET"))
	expected := []headers.Rule{
		{
			Path: "/*",
			Headers: http.Header{
				"Access-Control-Allow-Methods": []string{"GET"},
			},
		},
	}
	if !reflect.DeepEqual(rules, expected) {
		t.Fail()
	}
}

func TestUnparse(t *testing.T) {
	rules := headers.Must(headers.ParseString(`
/index.html
  cross-origin-opener-policy: same-origin
/*
  X-CLACKS-OVERHEAD: GNU Terry Pratchett
`))
	unparsed := headers.Must(headers.UnparseString(rules))
	if unparsed != `/index.html
	Cross-Origin-Opener-Policy: same-origin
/*
	X-Clacks-Overhead: GNU Terry Pratchett
` {
		t.Errorf("%v\n", unparsed)
	}
}