Skip to content

Commit

Permalink
add referer override test
Browse files Browse the repository at this point in the history
  • Loading branch information
facundoolano committed Jul 29, 2024
1 parent 81a4bed commit 13c6c7d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,30 @@ func TestFormatRegex(t *testing.T) {
assertEqual(t, result["ua_type"], "desktop")
assertEqual(t, result["os"], "Windows")
}

func TestRefererOverride(t *testing.T) {
// when -, leave empty
line := `xx.xx.xx.xx - - [24/Jul/2024:00:00:28 +0000] "GET /feed HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"`
pattern := formatToRegex(DEFAULT_LOG_FORMAT)
result, err := parseLogLine(pattern, line)
assertEqual(t, err, nil)
assertEqual(t, result["referer"], "")

// get from field and strip protocol
line = `xx.xx.xx.xx - - [24/Jul/2024:00:00:28 +0000] "GET /feed HTTP/1.1" 301 169 "https://olano.dev/feed.xml" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"`
result, err = parseLogLine(pattern, line)
assertEqual(t, err, nil)
assertEqual(t, result["referer"], "olano.dev/feed.xml")

// pick from utm_source when present
line = `xx.xx.xx.xx - - [24/Jul/2024:00:00:28 +0000] "GET /feed?utm_source=example.com HTTP/1.1" 301 169 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"`
result, err = parseLogLine(pattern, line)
assertEqual(t, err, nil)
assertEqual(t, result["referer"], "example.com")

// prefer field to utm_source
line = `xx.xx.xx.xx - - [24/Jul/2024:00:00:28 +0000] "GET /feed?utm_source=example.com HTTP/1.1" 301 169 "https://olano.dev/feed.xml" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"`
result, err = parseLogLine(pattern, line)
assertEqual(t, err, nil)
assertEqual(t, result["referer"], "olano.dev/feed.xml")
}

0 comments on commit 13c6c7d

Please sign in to comment.