-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve_test.go
More file actions
82 lines (77 loc) · 2.32 KB
/
Copy pathresolve_test.go
File metadata and controls
82 lines (77 loc) · 2.32 KB
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
74
75
76
77
78
79
80
81
82
package tango
import (
"context"
"encoding/json"
"errors"
"io"
"net/http"
"testing"
)
func TestValidateRequiresValue(t *testing.T) {
c := NewClient(WithAPIKey("k"), WithBaseURL("http://localhost:0"), WithRetries(0))
_, err := c.Validate(context.Background(), ValidateInput{Type: ValidatePIID})
var ve *ValidationError
if !errors.As(err, &ve) {
t.Fatalf("expected *ValidationError for empty Value, got %T: %v", err, err)
}
}
func TestValidateSendsCorrectBody(t *testing.T) {
var capturedBody []byte
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
capturedBody, _ = io.ReadAll(r.Body)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"result":"valid","type":"piid","value":"W15P7T19C0001"}`))
})
result, err := c.Validate(context.Background(), ValidateInput{
Type: ValidatePIID,
Value: "W15P7T19C0001",
})
if err != nil {
t.Fatalf("unexpected: %v", err)
}
if result.Result != "valid" {
t.Errorf("expected result=valid, got %q", result.Result)
}
var body map[string]any
if err := json.Unmarshal(capturedBody, &body); err != nil {
t.Fatalf("failed to decode body: %v", err)
}
if body["type"] != "piid" {
t.Errorf("body.type: want piid, got %v", body["type"])
}
if body["value"] != "W15P7T19C0001" {
t.Errorf("body.value mismatch: %v", body["value"])
}
}
func TestValidateAllInputTypes(t *testing.T) {
cases := []ValidateInputType{
ValidatePIID,
ValidateSolicitation,
ValidateUEI,
}
for _, typ := range cases {
t.Run(string(typ), func(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"result":"valid"}`))
})
result, err := c.Validate(context.Background(), ValidateInput{Type: typ, Value: "test-val"})
if err != nil {
t.Fatalf("unexpected error for type %q: %v", typ, err)
}
if result.Result != "valid" {
t.Errorf("expected result=valid, got %q", result.Result)
}
})
}
}
func TestValidateServerErrorPropagated(t *testing.T) {
c, _ := newTestClient(t, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(401)
})
_, err := c.Validate(context.Background(), ValidateInput{Type: ValidateUEI, Value: "UEI123"})
var ae *AuthError
if !errors.As(err, &ae) {
t.Fatalf("expected *AuthError, got %T: %v", err, err)
}
}