Disable handlers for disabled features

This commit is contained in:
Martin Polden 2018-02-10 17:52:55 +01:00
parent 8112536125
commit 215cce290c
3 changed files with 68 additions and 17 deletions

View File

@ -52,8 +52,8 @@ type PortResponse struct {
Reachable bool `json:"reachable"`
}
func New(db db.Database, lookupAddr LookupAddr, lookupPort LookupPort, logger *logrus.Logger) *Server {
return &Server{lookupAddr: lookupAddr, lookupPort: lookupPort, db: db, log: logger}
func New(db db.Database, lookupAddr LookupAddr, lookupPort LookupPort, log *logrus.Logger) *Server {
return &Server{lookupAddr: lookupAddr, lookupPort: lookupPort, db: db, log: log}
}
func ipFromRequest(header string, r *http.Request) (net.IP, error) {
@ -86,9 +86,13 @@ func (s *Server) newResponse(r *http.Request) (Response, error) {
if err != nil {
s.log.Debug(err)
}
hostnames, err := s.lookupAddr(ip)
if err != nil {
s.log.Debug(err)
var hostnames []string
if s.lookupAddr != nil {
h, err := s.lookupAddr(ip)
if err != nil {
s.log.Debug(err)
}
hostnames = h
}
return Response{
IP: ip,
@ -269,15 +273,19 @@ func (s *Server) Handler() http.Handler {
r.Handle("/", appHandler(s.CLIHandler)).Methods("GET").MatcherFunc(cliMatcher)
r.Handle("/", appHandler(s.CLIHandler)).Methods("GET").Headers("Accept", textMediaType)
r.Handle("/ip", appHandler(s.CLIHandler)).Methods("GET")
r.Handle("/country", appHandler(s.CLICountryHandler)).Methods("GET")
r.Handle("/country-iso", appHandler(s.CLICountryISOHandler)).Methods("GET")
r.Handle("/city", appHandler(s.CLICityHandler)).Methods("GET")
if !s.db.IsEmpty() {
r.Handle("/country", appHandler(s.CLICountryHandler)).Methods("GET")
r.Handle("/country-iso", appHandler(s.CLICountryISOHandler)).Methods("GET")
r.Handle("/city", appHandler(s.CLICityHandler)).Methods("GET")
}
// Browser
r.Handle("/", appHandler(s.DefaultHandler)).Methods("GET")
// Port testing
r.Handle("/port/{port:[0-9]+}", appHandler(s.PortHandler)).Methods("GET")
if s.lookupPort != nil {
r.Handle("/port/{port:[0-9]+}", appHandler(s.PortHandler)).Methods("GET")
}
// Not found handler which returns JSON when appropriate
r.NotFoundHandler = appHandler(s.NotFoundHandler)

View File

@ -11,17 +11,20 @@ import (
"github.com/mpolden/ipd/iputil/db"
)
type database struct{}
func lookupAddr(net.IP) ([]string, error) { return []string{"localhost"}, nil }
func lookupPort(net.IP, uint64) error { return nil }
func (d *database) Country(net.IP) (db.Country, error) {
type testDb struct{}
func (t *testDb) Country(net.IP) (db.Country, error) {
return db.Country{Name: "Elbonia", ISO: "EB"}, nil
}
func (d *database) City(net.IP) (string, error) { return "Bornyasherk", nil }
func newTestAPI() *Server {
return &Server{db: &database{}, lookupAddr: lookupAddr, lookupPort: lookupPort}
func (t *testDb) City(net.IP) (string, error) { return "Bornyasherk", nil }
func (t *testDb) IsEmpty() bool { return false }
func testServer() *Server {
return &Server{db: &testDb{}, lookupAddr: lookupAddr, lookupPort: lookupPort}
}
func httpGet(url string, acceptMediaType string, userAgent string) (string, int, error) {
@ -47,7 +50,7 @@ func httpGet(url string, acceptMediaType string, userAgent string) (string, int,
func TestCLIHandlers(t *testing.T) {
log.SetOutput(ioutil.Discard)
s := httptest.NewServer(newTestAPI().Handler())
s := httptest.NewServer(testServer().Handler())
var tests = []struct {
url string
@ -79,9 +82,43 @@ func TestCLIHandlers(t *testing.T) {
}
}
func TestDisabledHandlers(t *testing.T) {
log.SetOutput(ioutil.Discard)
server := testServer()
server.lookupPort = nil
server.lookupAddr = nil
server.db = db.Empty()
s := httptest.NewServer(server.Handler())
var tests = []struct {
url string
out string
status int
}{
{s.URL + "/port/1337", "404 page not found", 404},
{s.URL + "/country", "404 page not found", 404},
{s.URL + "/country-iso", "404 page not found", 404},
{s.URL + "/city", "404 page not found", 404},
{s.URL + "/json", `{"ip":"127.0.0.1","ip_decimal":2130706433}`, 200},
}
for _, tt := range tests {
out, status, err := httpGet(tt.url, "", "")
if err != nil {
t.Fatal(err)
}
if status != tt.status {
t.Errorf("Expected %d, got %d", tt.status, status)
}
if out != tt.out {
t.Errorf("Expected %q, got %q", tt.out, out)
}
}
}
func TestJSONHandlers(t *testing.T) {
log.SetOutput(ioutil.Discard)
s := httptest.NewServer(newTestAPI().Handler())
s := httptest.NewServer(testServer().Handler())
var tests = []struct {
url string

View File

@ -9,6 +9,7 @@ import (
type Database interface {
Country(net.IP) (Country, error)
City(net.IP) (string, error)
IsEmpty() bool
}
type Country struct {
@ -25,6 +26,7 @@ type empty struct{}
func (d *empty) Country(ip net.IP) (Country, error) { return Country{}, nil }
func (d *empty) City(ip net.IP) (string, error) { return "", nil }
func (d *empty) IsEmpty() bool { return true }
func Empty() Database { return &empty{} }
@ -87,3 +89,7 @@ func (g *geoip) City(ip net.IP) (string, error) {
}
return "", nil
}
func (g *geoip) IsEmpty() bool {
return g.country != nil || g.city != nil
}