docs(cooler,diary,festival,fetchguard): compile README Go examples via src= (quick-261118-qsd)

- cooler: ExampleFromApp and the articlesController purge declarations;
  AllowExport run by TestDocsAllowExport on miniredis
- diary: blogLog and ExampleFor; the generated main's run function, run by
  TestDocsRun in a temp directory with diary.Reset on cleanup
- festival: PostPublished/ExampleBus_Fire and PostFormExtended/Collected/
  ExampleBus_Collect, with a TestDocsDeclarations for Collected
- fetchguard: fetch region run by TestDocsFetchImage (loopback refused as
  private_ip), client region run by TestDocsSyncItems on a stub transport
This commit is contained in:
Jakub Zych
2026-11-18 19:48:01 +01:00
parent becb31fbef
commit d04454c57b
8 changed files with 385 additions and 95 deletions

View File

@@ -6,10 +6,13 @@ import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/netip"
"strings"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/fetchguard"
)
@@ -105,3 +108,131 @@ func ExampleIsPrivateAddr() {
// 10.0.0.7 true
// ::ffff:127.0.0.1 true
}
// errRejectedURL answers an image URL that points inside the network.
var errRejectedURL = errors.New("image URL rejected")
// fetchImage downloads an image from imageHost or one of its subdomains
// (images.example.com in production). cfg is the application's config
// (app.Config).
func fetchImage(ctx context.Context, cfg *compass.Config, imageHost, imageURL string) ([]byte, error) {
// docs:start fetch
policy := fetchguard.Policy{
Mode: fetchguard.AllowHostsMode,
AllowHosts: []string{imageHost},
MaxBytes: 5 << 20,
Timeout: 5 * time.Second,
}
res, err := fetchguard.Fetch(ctx, imageURL, policy, cfg)
if err != nil {
var fe *fetchguard.Error
if errors.As(err, &fe) && fe.Reason == fetchguard.ReasonPrivateIP {
return nil, errRejectedURL
}
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, fmt.Errorf("image fetch: status %d", res.StatusCode)
}
image := res.Body
// docs:end fetch
return image, nil
}
// TestDocsFetchImage runs fetchImage offline: an allowed host that resolves
// into the network is rejected at dial time, a host outside the list and a
// plain http URL never reach the network.
func TestDocsFetchImage(t *testing.T) {
site := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Error("the guard let a request through to a loopback server")
}))
defer site.Close()
if _, err := fetchImage(t.Context(), nil, "127.0.0.1", site.URL+"/cover.jpg"); !errors.Is(err, errRejectedURL) {
t.Fatalf("loopback image = %v, want errRejectedURL", err)
}
for url, want := range map[string]fetchguard.Reason{
"https://cdn.attacker.example/cover.jpg": fetchguard.ReasonInvalidURL,
"http://images.example.com/cover.jpg": fetchguard.ReasonScheme,
} {
_, err := fetchImage(t.Context(), nil, "images.example.com", url)
var fe *fetchguard.Error
if !errors.As(err, &fe) || fe.Reason != want {
t.Fatalf("%s: %v, want %s", url, err, want)
}
}
}
// vendorFunc is a vendor API stand-in for WithTransport.
type vendorFunc func(*http.Request) (*http.Response, error)
func (f vendorFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
// syncItems sends payload to the vendor API, then uploads a report file.
func syncItems(ctx context.Context, cfg *compass.Config, token string, payload any, f io.Reader) (*fetchguard.Result, error) {
// docs:start client
vendor, err := fetchguard.NewClient(fetchguard.Policy{
Mode: fetchguard.AllowHostsMode,
AllowHosts: []string{"api.example.com"},
Timeout: 10 * time.Second,
}, cfg)
if err != nil {
return nil, err
}
header := http.Header{}
header.Set("Authorization", fetchguard.Bearer(token))
res, err := vendor.PostJSON(ctx, "https://api.example.com/v1/items", header, payload)
if err != nil {
return nil, err
}
if res.StatusCode == http.StatusTooManyRequests {
wait := res.Header.Get("Retry-After")
return nil, fmt.Errorf("vendor busy, retry after %s seconds", wait)
}
// Multipart upload: fields first, then files, each in slice order.
res, err = vendor.PostMultipart(ctx, "https://api.example.com/v1/files", header,
[]fetchguard.FormField{{Name: "title", Value: "Report"}},
[]fetchguard.FormFile{{Field: "file", Filename: "report.pdf", ContentType: "application/pdf", Body: f}},
)
// docs:end client
return res, err
}
// TestDocsSyncItems runs syncItems against a stub vendor: the JSON call and
// the upload carry the bearer token, and a 429 stops before the upload.
func TestDocsSyncItems(t *testing.T) {
var calls []string
busy := false
vendor := vendorFunc(func(r *http.Request) (*http.Response, error) {
body, _ := io.ReadAll(r.Body)
calls = append(calls, r.URL.Path+" "+r.Header.Get("Authorization"))
status, header := http.StatusCreated, http.Header{}
if busy {
status = http.StatusTooManyRequests
header.Set("Retry-After", "30")
}
if r.URL.Path == "/v1/files" && (!strings.HasPrefix(r.Header.Get("Content-Type"), "multipart/form-data") ||
!strings.Contains(string(body), `filename="report.pdf"`) || !strings.Contains(string(body), "Report")) {
status = http.StatusBadRequest
}
return &http.Response{StatusCode: status, Header: header, Body: io.NopCloser(strings.NewReader(`{}`)), Request: r}, nil
})
ctx := fetchguard.WithTransport(t.Context(), vendor)
res, err := syncItems(ctx, nil, "example-token", map[string]string{"name": "widget"}, strings.NewReader("%PDF-1.7"))
if err != nil || res.StatusCode != http.StatusCreated {
t.Fatalf("syncItems = %+v, %v", res, err)
}
want := []string{"/v1/items Bearer example-token", "/v1/files Bearer example-token"}
if strings.Join(calls, "|") != strings.Join(want, "|") {
t.Fatalf("calls = %q", calls)
}
calls, busy = nil, true
if _, err := syncItems(ctx, nil, "example-token", nil, strings.NewReader("")); err == nil ||
!strings.Contains(err.Error(), "retry after 30 seconds") || len(calls) != 1 {
t.Fatalf("busy vendor: %v after %q", err, calls)
}
}