-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test for YAML unmarshalling (#396)
The new tests specifically test unmarshalling of line feed and carriage return escape sequences in double qouted strings, as that was an issue with goccy/go-yaml. Refs #359 Co-authored-by: Felipe Zipitría <[email protected]>
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2024 OWASP CRS Project | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
type yamlTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestYamlTestSuite(t *testing.T) { | ||
suite.Run(t, new(yamlTestSuite)) | ||
} | ||
|
||
func (s *yamlTestSuite) TestUnmarshalling_LineFeed() { | ||
s.testUnmarshalling(`\n`, "\n") | ||
} | ||
|
||
func (s *yamlTestSuite) TestUnmarshalling_CarriageReturn() { | ||
s.testUnmarshalling(`\r`, "\r") | ||
|
||
} | ||
|
||
func (s *yamlTestSuite) testUnmarshalling(escapeSequence string, literal string) { | ||
yamlTemplate := `--- | ||
tests: | ||
- test_id: 1234 | ||
stages: | ||
- input: | ||
headers: | ||
"%s": "%s" | ||
` | ||
keyTemplate := "Some%sHeader" | ||
valueTemplate := "some%svalue" | ||
|
||
key := fmt.Sprintf(keyTemplate, escapeSequence) | ||
value := fmt.Sprintf(valueTemplate, escapeSequence) | ||
expectedKey := fmt.Sprintf(keyTemplate, literal) | ||
expectedValue := fmt.Sprintf(valueTemplate, literal) | ||
|
||
yamlString := fmt.Sprintf(yamlTemplate, key, value) | ||
test := &FTWTest{} | ||
|
||
err := yaml.Unmarshal([]byte(yamlString), test) | ||
s.Require().NoError(err) | ||
|
||
headers := test.FTWTest.Tests[0].Stages[0].Input.Headers | ||
s.Contains(headers, expectedKey) | ||
s.Equal(expectedValue, headers[expectedKey]) | ||
} |