Skip to content

Commit

Permalink
Merge pull request #7 from satyrius/fix_empty_value
Browse files Browse the repository at this point in the history
empty values parsing, fix #6
  • Loading branch information
satyrius committed Jul 20, 2014
2 parents c06432f + e8cb7a8 commit e846400
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Parser struct {
// strings parsing regexp.
func NewParser(format string) *Parser {
re := regexp.MustCompile(`\\\$([a-z_]+)(\\?(.))`).ReplaceAllString(
regexp.QuoteMeta(format), "(?P<$1>[^$3]+)$2")
regexp.QuoteMeta(format), "(?P<$1>[^$3]*)$2")
return &Parser{format, regexp.MustCompile(fmt.Sprintf("^%v$", re))}
}

Expand Down
15 changes: 13 additions & 2 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (suite *ParserTestSuite) TestFormatSaved() {
func (suite *ParserTestSuite) TestRegexp() {
assert.Equal(suite.T(),
suite.parser.regexp.String(),
`^(?P<remote_addr>[^ ]+) \[(?P<time_local>[^]]+)\] "(?P<request>[^"]+)"$`)
`^(?P<remote_addr>[^ ]*) \[(?P<time_local>[^]]*)\] "(?P<request>[^"]*)"$`)
}

func (suite *ParserTestSuite) TestParseString() {
Expand All @@ -48,7 +48,18 @@ func (suite *ParserTestSuite) TestParseInvalidString() {
line := `GET /api/foo/bar HTTP/1.1`
_, err := suite.parser.ParseString(line)
assert.Error(suite.T(), err)
// TODO test empty value
}

func (suite *ParserTestSuite) TestEmptyValue() {
line := `89.234.89.123 [08/Nov/2013:13:39:18 +0000] ""`
expected := NewEntry(Fields{
"remote_addr": "89.234.89.123",
"time_local": "08/Nov/2013:13:39:18 +0000",
"request": "",
})
entry, err := suite.parser.ParseString(line)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), entry, expected)
}

func TestNginxParser(t *testing.T) {
Expand Down

0 comments on commit e846400

Please sign in to comment.