SPB Git forge
15commits 1branches 0releases
29.7 MBsize
maindefault branch
10 days agolast push
TypeScript 36.3% Python 31.8% Go 18% JavaScript 9.8% Shell 1.9% SQL 1.4% CSS 0.5%
2.1 KB · 71 lines go
Raw Blame History
1package identity23import (4	"context"5	"net/http"6	"net/http/httptest"7	"testing"8)910func TestParseASNOrg(t *testing.T) {11	cases := []struct {12		in  string13		asn int14		org string15	}{16		{"AS577 Bell Canada", 577, "Bell Canada"},17		{"AS16276 OVH SAS", 16276, "OVH SAS"},18		{"Bell Canada", 0, "Bell Canada"},19		{"", 0, ""},20		{"ASN Something", 0, "ASN Something"},21	}22	for _, c := range cases {23		asn, org := ParseASNOrg(c.in)24		if asn != c.asn || org != c.org {25			t.Errorf("%q → %d %q, want %d %q", c.in, asn, org, c.asn, c.org)26		}27	}28}2930func TestFallbackAndRounding(t *testing.T) {31	ipinfo := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {32		http.Error(w, "rate limited", 429)33	}))34	defer ipinfo.Close()35	ipapi := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {36		w.Write([]byte(`{"status":"success","country":"Canada","countryCode":"CA","city":"Québec","lat":46.812345,"lon":-71.214567,"as":"AS577 Bell Canada","org":"Bell Canada","query":"76.70.60.50"}`))37	}))38	defer ipapi.Close()39	IPInfoURL, IPAPIURL = ipinfo.URL, ipapi.URL4041	s := New("InternetPressureProbe/test")42	id, err := s.Refresh(context.Background())43	if err != nil {44		t.Fatal(err)45	}46	if id.Source != "ip-api.com" || id.PublicIP != "76.70.60.50" || id.ASN != 577 || id.Country != "CA" || id.City != "Québec" {47		t.Fatalf("%+v", id)48	}49	if *id.Lat != 46.81 || *id.Lon != -71.21 {50		t.Fatalf("coordinates must be rounded to city level: %v %v", *id.Lat, *id.Lon)51	}52	if s.Current() != id {53		t.Fatal("current not stored")54	}55}5657func TestIPInfo(t *testing.T) {58	ipinfo := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {59		w.Write([]byte(`{"ip":"51.161.112.61","city":"Beauharnois","region":"Quebec","country":"CA","loc":"45.3151,-73.8779","org":"AS16276 OVH SAS"}`))60	}))61	defer ipinfo.Close()62	IPInfoURL = ipinfo.URL63	id, err := New("ua").Refresh(context.Background())64	if err != nil {65		t.Fatal(err)66	}67	if id.Source != "ipinfo.io" || id.ASN != 16276 || id.Org != "OVH SAS" || *id.Lat != 45.32 || *id.Lon != -73.88 {68		t.Fatalf("%+v", id)69	}70}71