package hunter import ( "context" "errors" "net/http" "net/http/httptest" "testing" ) func TestClient(t *testing.T) { verifyCalls := 0 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Header.Get("X-API-KEY") != "k" { w.WriteHeader(http.StatusUnauthorized) w.Write([]byte(`{"errors":[{"id":"authentication_failed","code":401,"details":"No user found"}]}`)) return } q := r.URL.Query() switch r.URL.Path { case "/account": w.Write([]byte(`{"data":{"email":"pat@example.com","plan_name":"Free","reset_date":"2026-12-01", "requests":{"searches":{"used":3,"available":25,"remaining":22},"verifications":{"used":1,"available":50,"remaining":49}}}}`)) case "/domain-search": if q.Get("domain") != "acme.example" || q.Get("type") != "personal" || q.Get("limit") != "5" || q.Get("seniority") != "executive,senior" || q.Get("department") != "it" { t.Errorf("domain search query: %v", q) } w.Header().Set("X-Credits-Used", "1") w.Write([]byte(`{"data":{"organization":"Acme","pattern":"{first}","emails":[{"value":"pat@acme.example","type":"personal", "confidence":94,"first_name":"Pat","last_name":"Lee","position":"CTO","department":"it", "verification":{"date":"2026-11-01","status":"valid"}}]},"meta":{"results":1}}`)) case "/email-finder": if q.Get("first_name") != "Ann" || q.Get("last_name") != "Ray" { t.Errorf("finder query: %v", q) } w.Write([]byte(`{"data":{"email":null,"score":null}}`)) case "/email-verifier": verifyCalls++ if verifyCalls == 1 { w.WriteHeader(http.StatusAccepted) return } w.Header().Set("X-Credits-Used", "0.5") w.Write([]byte(`{"data":{"status":"accept_all","score":70,"email":"pat@acme.example"}}`)) case "/quota": w.WriteHeader(http.StatusTooManyRequests) default: w.WriteHeader(http.StatusNotFound) w.Write([]byte(`{"errors":[{"id":"not_found","code":404,"details":"Nothing here"}]}`)) } })) defer srv.Close() ctx := context.Background() c := New("k").WithBase(srv.URL) a, err := c.Account(ctx) if err != nil || a.PlanName != "Free" || a.Left() != "22 of 25 searches and 49 of 50 verifications left" { t.Fatalf("account: %+v %v", a, err) } res, err := c.DomainSearch(ctx, DomainQuery{Domain: "acme.example", Limit: 5, Seniority: []string{"executive", "senior"}, Departments: []string{"it"}}) if err != nil || len(res.Emails) != 1 || res.Credits != 1 || res.Emails[0].Verification.Status != "valid" || res.Emails[0].Position != "CTO" { t.Fatalf("domain search: %+v %v", res, err) } f, err := c.FindEmail(ctx, "acme.example", "Ann", "Ray") if err != nil || f.Email != "" { t.Fatalf("finder: %+v %v", f, err) } v, err := c.Verify(ctx, "pat@acme.example") if err != nil || v.Status != "accept_all" || v.Credits != 0.5 || verifyCalls != 2 { t.Fatalf("verify: %+v %v (%d calls)", v, err, verifyCalls) } if _, _, err := c.get(ctx, "/quota", nil, &struct{}{}); !errors.Is(err, ErrNoCredits) { t.Errorf("quota: %v", err) } if _, _, err := c.get(ctx, "/nope", nil, &struct{}{}); err == nil || err.Error() != "Hunter.io /nope: HTTP 404: Nothing here" { t.Errorf("404: %v", err) } if _, err := New("bad").WithBase(srv.URL).Account(ctx); !errors.Is(err, ErrUnauthorized) { t.Errorf("bad key: %v", err) } }