Skip to content

Commit 816ebbf

Browse files
authored
Merge pull request #43 from nginxinc/types-dir
fix: validate body of types directive
2 parents e552cc0 + 9398dbb commit 816ebbf

File tree

4 files changed

+67
-2
lines changed

4 files changed

+67
-2
lines changed

analyze_map.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ var mapBodies = map[string]mapParameterMasks{
3232
"match": {
3333
defaultMasks: ngxConf1More,
3434
},
35+
"types": {
36+
defaultMasks: ngxConf1More,
37+
},
3538
}
3639

3740
// analyzeMapBody validates the body of a map-like directive. Map-like directives are block directives

analyze_map_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/stretchr/testify/require"
1414
)
1515

16-
// nolint:funlen
16+
//nolint:funlen,exhaustruct
1717
func TestAnalyzeMapBody(t *testing.T) {
1818
t.Parallel()
1919

@@ -82,6 +82,26 @@ func TestAnalyzeMapBody(t *testing.T) {
8282
},
8383
term: ";",
8484
},
85+
"valid types": {
86+
mapDirective: "types",
87+
parameter: &Directive{
88+
Directive: "text/html",
89+
Args: []string{"html htm shtml"},
90+
Line: 5,
91+
Block: Directives{},
92+
},
93+
term: ";",
94+
},
95+
"invalid types with special parameter": {
96+
mapDirective: "types",
97+
parameter: &Directive{
98+
Directive: "hostnames",
99+
Line: 5,
100+
Block: Directives{},
101+
},
102+
term: ";",
103+
wantErr: &ParseError{What: "invalid number of parameters"},
104+
},
85105
"invalid geo proxy_recursive parameter": {
86106
mapDirective: "geo",
87107
parameter: &Directive{

parse_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func getTestConfigPath(parts ...string) string {
4343
return filepath.Join("testdata", "configs", filepath.Join(parts...))
4444
}
4545

46-
// nolint:gochecknoglobals
46+
//nolint:gochecknoglobals,exhaustruct
4747
var parseFixtures = []parseFixture{
4848
{"includes-regular", "", ParseOptions{}, Payload{
4949
Status: "failed",
@@ -1186,6 +1186,42 @@ var parseFixtures = []parseFixture{
11861186
},
11871187
},
11881188
}},
1189+
{"types", "", ParseOptions{ErrorOnUnknownDirectives: true}, Payload{
1190+
Status: "ok",
1191+
Config: []Config{
1192+
{
1193+
File: getTestConfigPath("types", "nginx.conf"),
1194+
Status: "ok",
1195+
Parsed: Directives{
1196+
{
1197+
Directive: "http",
1198+
Args: []string{},
1199+
Line: 1,
1200+
Block: Directives{
1201+
{
1202+
Directive: "types",
1203+
Line: 2,
1204+
Block: Directives{
1205+
{
1206+
Directive: "text/html",
1207+
Args: []string{"html", "htm", "shtml"},
1208+
Line: 3,
1209+
Block: Directives{},
1210+
},
1211+
{
1212+
Directive: "text/css",
1213+
Args: []string{"css"},
1214+
Line: 4,
1215+
Block: Directives{},
1216+
},
1217+
},
1218+
},
1219+
},
1220+
},
1221+
},
1222+
},
1223+
},
1224+
}},
11891225
}
11901226

11911227
func TestParse(t *testing.T) {

testdata/configs/types/nginx.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
http {
2+
types {
3+
text/html html htm shtml;
4+
text/css css;
5+
}
6+
}

0 commit comments

Comments
 (0)