test: cover lookup handler
This commit is contained in:
46
main_test.go
Normal file
46
main_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLookupMissingDomain(t *testing.T) {
|
||||
s := &server{
|
||||
nrenASNs: make(map[uint]struct{}),
|
||||
}
|
||||
s.ready.Store(true)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/lookup", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
s.lookupHandler(rr, req)
|
||||
|
||||
if rr.Code != http.StatusBadRequest {
|
||||
t.Fatalf("expected 400, got %d", rr.Code)
|
||||
}
|
||||
|
||||
if !strings.Contains(rr.Body.String(), "missing domain") {
|
||||
t.Fatalf("expected error message in response")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLookupServiceNotReady(t *testing.T) {
|
||||
s := &server{
|
||||
nrenASNs: make(map[uint]struct{}),
|
||||
}
|
||||
s.ready = atomic.Bool{}
|
||||
s.ready.Store(false)
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/lookup?domain=example.com", nil)
|
||||
rr := httptest.NewRecorder()
|
||||
|
||||
s.lookupHandler(rr, req)
|
||||
|
||||
if rr.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("expected 503, got %d", rr.Code)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user