Files
supermarket/internal/api/bing/client_test.go
davidandClaude Opus 5.5 2302fb0436 Phase 3 part 7: Bing, IndexNow and search engine setup
SEO → Search engines: a pull request that adds the IndexNow key file, a
robots.txt naming the sitemap, and turns the sitemap on when the Hugo
config disables it (checked with a build); IndexNow submissions of pages
that go live through Changes or Briefs once the key file is served; the
sitemaps Search Console has; and, with an API key, Bing's traffic,
searches and crawl issues. NewlyLive no longer queries while a result
set holds the database's only connection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 08:45:13 -07:00

47 lines
1.5 KiB
Go

package bing
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestClient(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("apikey") != "k" {
w.WriteHeader(http.StatusUnauthorized)
return
}
if r.URL.Query().Get("siteUrl") != "https://us.example/" {
t.Errorf("site: %s", r.URL)
}
switch r.URL.Path {
case "/GetRankAndTrafficStats":
w.Write([]byte(`{"d":[{"__type":"x","Clicks":12,"Date":"/Date(1763337600000-0800)/","Impressions":340}]}`))
case "/GetQueryStats":
w.Write([]byte(`{"d":[{"Query":"nextcloud hosting","Clicks":2,"Impressions":90,"AvgImpressionPosition":8.5,"Date":"/Date(1763337600000)/"}]}`))
case "/GetCrawlIssues":
w.Write([]byte(`{"d":[{"Url":"https://us.example/old","HttpCode":404,"Issues":1}]}`))
}
}))
defer srv.Close()
ctx := context.Background()
c := New("k").WithBase(srv.URL)
days, err := c.Traffic(ctx, "https://us.example/")
if err != nil || len(days) != 1 || days[0].Clicks != 12 || days[0].Date.Year() != 2025 {
t.Errorf("traffic: %+v %v", days, err)
}
qs, err := c.Queries(ctx, "https://us.example/")
if err != nil || qs[0].Position != 8.5 {
t.Errorf("queries: %+v %v", qs, err)
}
is, err := c.CrawlIssues(ctx, "https://us.example/")
if err != nil || is[0].HTTPCode != 404 {
t.Errorf("issues: %+v %v", is, err)
}
if _, err := New("bad").WithBase(srv.URL).Traffic(ctx, "https://us.example/"); err != ErrUnauthorized {
t.Errorf("bad key: %v", err)
}
}