-
Notifications
You must be signed in to change notification settings - Fork 2
/
text_test.go
73 lines (69 loc) · 1.69 KB
/
text_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package nightfall
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestScanText(t *testing.T) {
tests := []struct {
name string
handler http.HandlerFunc
wantErr bool
}{
{
name: "happy path",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
},
wantErr: false,
},
{
name: "transient error",
handler: func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
wantErr: true,
},
}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer s.Close()
client, err := NewClient(OptionAPIKey("some key"))
if err != nil {
t.Fatal("Error initializing client")
}
client.baseURL = s.URL + "/"
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
s.Config.Handler = test.handler
_, err = client.ScanText(context.Background(), &ScanTextRequest{
Payload: []string{"4242 4242 4242 4242"},
Policy: &Config{
DetectionRules: []DetectionRule{{
Detectors: []Detector{{
MinNumFindings: 1,
MinConfidence: ConfidencePossible,
DisplayName: "cc#",
DetectorType: DetectorTypeNightfallDetector,
NightfallDetector: "CREDIT_CARD_NUMBER",
}},
LogicalOp: LogicalOpAny,
}},
DefaultRedactionConfig: &RedactionConfig{
MaskConfig: &MaskConfig{
MaskingChar: "🤫",
CharsToIgnore: []string{"-"},
},
RemoveFinding: true,
},
},
})
if !test.wantErr && err != nil {
t.Errorf("Got unexpected error: %v", err)
}
if test.wantErr && err == nil {
t.Error("Did not get expected error")
}
})
}
}