|
1 | 1 | package forme |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/base64" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 | "io" |
@@ -600,6 +601,130 @@ func TestRenderS3ReturnsURL(t *testing.T) { |
600 | 601 | } |
601 | 602 | } |
602 | 603 |
|
| 604 | +func TestRedactSendsCorrectBody(t *testing.T) { |
| 605 | + var gotBody []byte |
| 606 | + var gotPath, gotContentType string |
| 607 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 608 | + gotPath = r.URL.Path |
| 609 | + gotContentType = r.Header.Get("Content-Type") |
| 610 | + gotBody, _ = io.ReadAll(r.Body) |
| 611 | + w.Header().Set("Content-Type", "application/pdf") |
| 612 | + w.Write([]byte("%PDF-redacted")) |
| 613 | + })) |
| 614 | + defer server.Close() |
| 615 | + |
| 616 | + client := New("forme_sk_test", WithBaseURL(server.URL)) |
| 617 | + page := 2 |
| 618 | + result, err := client.Redact( |
| 619 | + []byte("%PDF-original"), |
| 620 | + RedactOptions{ |
| 621 | + Patterns: []RedactionPattern{ |
| 622 | + {Pattern: `\d{3}-\d{2}-\d{4}`, PatternType: "Regex", Color: "#000000"}, |
| 623 | + {Pattern: "John Doe", PatternType: "Literal", Page: &page}, |
| 624 | + }, |
| 625 | + Presets: []string{"ssn", "email"}, |
| 626 | + }, |
| 627 | + ) |
| 628 | + if err != nil { |
| 629 | + t.Fatalf("unexpected error: %v", err) |
| 630 | + } |
| 631 | + if string(result) != "%PDF-redacted" { |
| 632 | + t.Fatalf("expected redacted PDF, got %q", result) |
| 633 | + } |
| 634 | + if gotPath != "/v1/redact" { |
| 635 | + t.Fatalf("expected '/v1/redact', got %q", gotPath) |
| 636 | + } |
| 637 | + if gotContentType != "application/json" { |
| 638 | + t.Fatalf("expected 'application/json', got %q", gotContentType) |
| 639 | + } |
| 640 | + |
| 641 | + var body map[string]any |
| 642 | + json.Unmarshal(gotBody, &body) |
| 643 | + if body["pdf"] == "" { |
| 644 | + t.Fatal("expected pdf to be base64-encoded") |
| 645 | + } |
| 646 | + |
| 647 | + patterns, ok := body["patterns"].([]any) |
| 648 | + if !ok || len(patterns) != 2 { |
| 649 | + t.Fatalf("expected 2 patterns, got %v", body["patterns"]) |
| 650 | + } |
| 651 | + p0 := patterns[0].(map[string]any) |
| 652 | + if p0["pattern"] != `\d{3}-\d{2}-\d{4}` { |
| 653 | + t.Fatalf("unexpected pattern: %v", p0["pattern"]) |
| 654 | + } |
| 655 | + if p0["pattern_type"] != "Regex" { |
| 656 | + t.Fatalf("unexpected pattern_type: %v", p0["pattern_type"]) |
| 657 | + } |
| 658 | + p1 := patterns[1].(map[string]any) |
| 659 | + if p1["page"] != float64(2) { |
| 660 | + t.Fatalf("expected page=2, got %v", p1["page"]) |
| 661 | + } |
| 662 | + |
| 663 | + presets, ok := body["presets"].([]any) |
| 664 | + if !ok || len(presets) != 2 { |
| 665 | + t.Fatalf("expected 2 presets, got %v", body["presets"]) |
| 666 | + } |
| 667 | + if presets[0] != "ssn" || presets[1] != "email" { |
| 668 | + t.Fatalf("unexpected presets: %v", presets) |
| 669 | + } |
| 670 | + |
| 671 | + if _, ok := body["redactions"]; ok { |
| 672 | + t.Fatal("expected redactions to be omitted when empty") |
| 673 | + } |
| 674 | + if _, ok := body["template"]; ok { |
| 675 | + t.Fatal("expected template to be omitted when empty") |
| 676 | + } |
| 677 | +} |
| 678 | + |
| 679 | +func TestRasterizeReturnsPages(t *testing.T) { |
| 680 | + page1 := []byte("PNG-page-1-data") |
| 681 | + page2 := []byte("PNG-page-2-data") |
| 682 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 683 | + // Verify path and method |
| 684 | + if r.URL.Path != "/v1/rasterize" { |
| 685 | + t.Errorf("expected '/v1/rasterize', got %q", r.URL.Path) |
| 686 | + } |
| 687 | + if r.Method != "POST" { |
| 688 | + t.Errorf("expected POST, got %q", r.Method) |
| 689 | + } |
| 690 | + |
| 691 | + // Verify DPI is sent |
| 692 | + body, _ := io.ReadAll(r.Body) |
| 693 | + var reqBody map[string]any |
| 694 | + json.Unmarshal(body, &reqBody) |
| 695 | + if reqBody["dpi"] != float64(300) { |
| 696 | + t.Errorf("expected dpi=300, got %v", reqBody["dpi"]) |
| 697 | + } |
| 698 | + if reqBody["pdf"] == "" { |
| 699 | + t.Error("expected pdf to be base64-encoded") |
| 700 | + } |
| 701 | + |
| 702 | + w.Header().Set("Content-Type", "application/json") |
| 703 | + json.NewEncoder(w).Encode(map[string]any{ |
| 704 | + "pages": []string{ |
| 705 | + base64.StdEncoding.EncodeToString(page1), |
| 706 | + base64.StdEncoding.EncodeToString(page2), |
| 707 | + }, |
| 708 | + }) |
| 709 | + })) |
| 710 | + defer server.Close() |
| 711 | + |
| 712 | + client := New("forme_sk_test", WithBaseURL(server.URL)) |
| 713 | + pages, err := client.Rasterize([]byte("%PDF-original"), RasterizeOptions{DPI: 300}) |
| 714 | + if err != nil { |
| 715 | + t.Fatalf("unexpected error: %v", err) |
| 716 | + } |
| 717 | + if len(pages) != 2 { |
| 718 | + t.Fatalf("expected 2 pages, got %d", len(pages)) |
| 719 | + } |
| 720 | + if string(pages[0]) != "PNG-page-1-data" { |
| 721 | + t.Fatalf("expected page 1 data, got %q", pages[0]) |
| 722 | + } |
| 723 | + if string(pages[1]) != "PNG-page-2-data" { |
| 724 | + t.Fatalf("expected page 2 data, got %q", pages[1]) |
| 725 | + } |
| 726 | +} |
| 727 | + |
603 | 728 | // roundTripperFunc is a helper to create custom http.RoundTripper from a function. |
604 | 729 | type roundTripperFunc func(*http.Request) (*http.Response, error) |
605 | 730 |
|
|
0 commit comments