-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathspur_test.go
More file actions
96 lines (83 loc) · 3 KB
/
Copy pathspur_test.go
File metadata and controls
96 lines (83 loc) · 3 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* ZAnnotate Copyright 2026 Regents of the University of Michigan
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package zannotate
import (
"encoding/json"
"io"
"net"
"net/http"
"strings"
"testing"
)
type mockRoundTripper struct {
expectedToken string
status int
body string
}
func (m mockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// Validate header was set (helps ensure Annotate sets Token)
if req.Header.Get("Token") != m.expectedToken {
return &http.Response{
StatusCode: http.StatusUnauthorized,
Body: io.NopCloser(strings.NewReader(`{"error":"missing token"}`)),
Header: make(http.Header),
}, nil
}
return &http.Response{
StatusCode: m.status,
Body: io.NopCloser(strings.NewReader(m.body)),
Header: make(http.Header),
}, nil
}
func TestSpurAnnotatorMockSuccess(t *testing.T) {
// Build factory and annotator
factory := &SpurAnnotatorFactory{apiKey: "test-key", timeoutSecs: 2}
a := factory.MakeAnnotator(0).(*SpurAnnotator)
// Inject a mock http.Client that returns a fixed successful JSON body
a.client = &http.Client{
Transport: mockRoundTripper{
expectedToken: "test-key",
status: http.StatusOK,
body: `{"as":{"number":13335,"organization":"Cloudflare, Inc."},"infrastructure":"DATACENTER","ip":"1.1.1.1","location":{"city":"Anycast","country":"ZZ","state":"Anycast"},"organization":"Taguchi Digital Marketing System"}`,
},
}
res := a.Annotate(net.ParseIP("1.1.1.1"))
if res == nil {
t.Fatalf("expected non-nil result")
}
raw, ok := res.(json.RawMessage)
if !ok {
t.Fatalf("expected json.RawMessage, got %T", res)
}
expected := `{"as":{"number":13335,"organization":"Cloudflare, Inc."},"infrastructure":"DATACENTER","ip":"1.1.1.1","location":{"city":"Anycast","country":"ZZ","state":"Anycast"},"organization":"Taguchi Digital Marketing System"}`
if string(raw) != expected {
t.Fatalf("unexpected JSON returned: got %s want %s", string(raw), expected)
}
}
func TestSpurAnnotatorMockNon200(t *testing.T) {
factory := &SpurAnnotatorFactory{apiKey: "test-key", timeoutSecs: 2}
a := factory.MakeAnnotator(0).(*SpurAnnotator)
a.client = &http.Client{
Transport: mockRoundTripper{
expectedToken: "test-key",
status: http.StatusInternalServerError,
body: `{"error":"server error"}`,
},
}
res := a.Annotate(net.ParseIP("1.1.1.1"))
if res != nil {
t.Fatalf("expected nil result for non-200 response, got %v", res)
}
// Test should return nil for a non-200 status
}