test: add integration tests for httpcache and gitshallow

This commit is contained in:
AJ ONeal 2026-04-20 10:01:57 -06:00
parent 4e8321af97
commit 344246362f
No known key found for this signature in database
2 changed files with 151 additions and 0 deletions

View File

@ -0,0 +1,76 @@
//go:build integration
package gitshallow_test
import (
"os"
"path/filepath"
"testing"
"github.com/therootcompany/golib/net/gitshallow"
)
const testRepoURL = "https://github.com/bitwire-it/ipblocklist"
func testdataDir(t *testing.T) string {
t.Helper()
dir, _ := filepath.Abs(".")
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return filepath.Join(dir, "testdata")
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("could not find module root (go.mod)")
}
dir = parent
}
}
func TestRepo_Clone(t *testing.T) {
repoDir := filepath.Join(testdataDir(t), "gitshallow_ipblocklist")
os.RemoveAll(repoDir) // start fresh
repo := gitshallow.New(testRepoURL, repoDir, 1, "")
updated, err := repo.Fetch()
if err != nil {
t.Fatalf("Fetch (clone): %v", err)
}
if !updated {
t.Error("first Fetch: expected updated=true after fresh clone")
}
// Verify expected files are present.
for _, rel := range []string{
"tables/inbound/single_ips.txt",
"tables/inbound/networks.txt",
"tables/outbound/single_ips.txt",
"tables/outbound/networks.txt",
} {
p := filepath.Join(repoDir, rel)
if info, err := os.Stat(p); err != nil {
t.Errorf("expected file missing: %s", rel)
} else {
t.Logf("%s: %d bytes", rel, info.Size())
}
}
}
func TestRepo_Pull(t *testing.T) {
repoDir := filepath.Join(testdataDir(t), "gitshallow_ipblocklist")
repo := gitshallow.New(testRepoURL, repoDir, 1, "")
// Ensure cloned first.
if _, err := repo.Fetch(); err != nil {
t.Fatalf("initial Fetch: %v", err)
}
// Pull again — already up to date, updated may be true or false.
_, err := repo.Fetch()
if err != nil {
t.Fatalf("second Fetch (pull): %v", err)
}
t.Log("pull completed without error")
}

View File

@ -0,0 +1,75 @@
//go:build integration
package httpcache_test
import (
"os"
"path/filepath"
"testing"
"github.com/therootcompany/golib/net/httpcache"
)
const (
testURL = "https://github.com/bitwire-it/ipblocklist/raw/refs/heads/main/tables/inbound/single_ips.txt"
testFile = "httpcache_inbound.txt"
)
func testdataDir(t *testing.T) string {
t.Helper()
dir, _ := filepath.Abs(".")
for {
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
return filepath.Join(dir, "testdata")
}
parent := filepath.Dir(dir)
if parent == dir {
t.Fatal("could not find module root (go.mod)")
}
dir = parent
}
}
func TestCacher_Download(t *testing.T) {
path := filepath.Join(testdataDir(t), testFile)
os.Remove(path) // start fresh
c := httpcache.New(testURL, path)
updated, err := c.Fetch()
if err != nil {
t.Fatalf("first Fetch: %v", err)
}
if !updated {
t.Error("first Fetch: expected updated=true")
}
info, err := os.Stat(path)
if err != nil {
t.Fatalf("file not created: %v", err)
}
if info.Size() == 0 {
t.Error("downloaded file is empty")
}
t.Logf("downloaded %d bytes to %s", info.Size(), path)
}
func TestCacher_ConditionalGet(t *testing.T) {
path := filepath.Join(testdataDir(t), testFile)
// Ensure file exists from a prior download (or download it now).
c := httpcache.New(testURL, path)
if _, err := c.Fetch(); err != nil {
t.Fatalf("initial Fetch: %v", err)
}
// Second fetch on the same Cacher should use ETag/Last-Modified.
updated, err := c.Fetch()
if err != nil {
t.Fatalf("second Fetch: %v", err)
}
if updated {
t.Error("second Fetch: expected updated=false (content unchanged)")
}
t.Log("conditional GET correctly returned 304 / not-modified")
}