move should-collect logic into func so it can be tested

This commit is contained in:
Danny 2018-05-14 10:14:43 +02:00
parent 05a159da7f
commit 2c09050d74
2 changed files with 32 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/base64"
"net/http"
"net/url"
"strings"
"time"
"github.com/mssola/user_agent"
@ -12,14 +13,26 @@ import (
"github.com/usefathom/fathom/pkg/models"
)
func ShouldCollect(r *http.Request) bool {
// abort if this is a bot.
ua := user_agent.New(r.UserAgent())
if ua.Bot() {
return false
}
if r.Referer() == "" {
return false
}
return true
}
/* middleware */
func NewCollectHandler() http.Handler {
go aggregate()
return HandlerFunc(func(w http.ResponseWriter, r *http.Request) error {
// abort if this is a bot.
ua := user_agent.New(r.UserAgent())
if ua.Bot() {
if !ShouldCollect(r) {
return nil
}
@ -28,11 +41,6 @@ func NewCollectHandler() http.Handler {
return err
}
// abandon if HTTP referer was empty
if u.Scheme == "" || u.Host == "" {
return nil
}
q := r.URL.Query()
now := time.Now()
@ -40,7 +48,7 @@ func NewCollectHandler() http.Handler {
pageview := &models.Pageview{
SessionID: q.Get("sid"),
Hostname: u.Scheme + "://" + u.Host,
Pathname: q.Get("p"),
Pathname: "/" + strings.TrimLeft(q.Get("p"), "/"),
IsNewVisitor: q.Get("nv") == "1",
IsNewSession: q.Get("ns") == "1",
IsUnique: q.Get("u") == "1",

15
pkg/api/collect_test.go Normal file
View File

@ -0,0 +1,15 @@
package api
import (
"net/http"
"testing"
)
func TestShouldCollect(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("User-Agent", "Mozilla/1.0")
r.Header.Add("Referer", "http://usefathom.com/")
if v := ShouldCollect(r); v != true {
t.Errorf("Expected %#v, got %#v", true, false)
}
}