Skip to content

Commit 98ed702

Browse files
rvilgalyscopybara-github
authored andcommitted
Adds optional max_diff_entries and max_database_entries in ComputeThreatListDiffRequest call from api.go. Version bump from User Agent.
PiperOrigin-RevId: 639091343
1 parent 9c1c0dc commit 98ed702

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

api.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"io/ioutil"
2121
"net/http"
2222
"net/url"
23+
"strconv"
2324
"strings"
2425

2526
"google.golang.org/protobuf/encoding/protojson"
@@ -34,9 +35,11 @@ const (
3435
threatTypeString = "threat_type"
3536
versionTokenString = "version_token"
3637
supportedCompressionsString = "constraints.supported_compressions"
38+
maxDiffEntriesKey = "constraints.max_diff_entries"
39+
maxDatabaseEntriesKey = "constraints.max_database_entries"
3740
hashPrefixString = "hash_prefix"
3841
threatTypesString = "threat_types"
39-
userAgentString = "Webrisk-Client/0.2.1"
42+
userAgentString = "Webrisk-Client/0.2.2"
4043
)
4144

4245
// The api interface specifies wrappers around the Web Risk API.
@@ -131,6 +134,12 @@ func (a *netAPI) ListUpdate(ctx context.Context, req *pb.ComputeThreatListDiffRe
131134
if len(req.GetVersionToken()) != 0 {
132135
q.Set(versionTokenString, base64.StdEncoding.EncodeToString(req.GetVersionToken()))
133136
}
137+
if req.GetConstraints().GetMaxDiffEntries() != 0 {
138+
q.Add(maxDiffEntriesKey, strconv.FormatInt(int64(req.GetConstraints().GetMaxDiffEntries()), 10))
139+
}
140+
if req.GetConstraints().GetMaxDatabaseEntries() != 0 {
141+
q.Add(maxDatabaseEntriesKey, strconv.FormatInt(int64(req.GetConstraints().GetMaxDatabaseEntries()), 10))
142+
}
134143
for _, compressionType := range req.GetConstraints().GetSupportedCompressions() {
135144
q.Add(supportedCompressionsString, compressionType.String())
136145
}

api_test.go

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"net/http"
2424
"net/http/httptest"
2525
"reflect"
26+
"strconv"
2627
"testing"
2728

2829
"github.com/google/go-cmp/cmp"
@@ -54,6 +55,8 @@ func TestNetAPI(t *testing.T) {
5455
var gotReqHashPrefix, wantReqHashPrefix []byte
5556
var gotReqThreatTypes, wantReqThreatTypes []pb.ThreatType
5657
var gotResp, wantResp proto.Message
58+
var gotMaxDiffEntries, wantMaxDiffEntries []int32
59+
var gotMaxDatabaseEntries, wantMaxDatabaseEntries []int32
5760
responseMisformatter := ""
5861
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5962
var p []byte
@@ -88,15 +91,33 @@ func TestNetAPI(t *testing.T) {
8891
gotReqThreatTypes = append(gotReqThreatTypes,
8992
pb.ThreatType(pb.ThreatType_value[threat]))
9093
}
94+
} else if key == maxDiffEntriesKey {
95+
if len(value) == 0 {
96+
t.Fatalf("Missing value for key %v", key)
97+
}
98+
i, err := strconv.ParseInt(value[0], 10, 32)
99+
if err != nil {
100+
t.Fatalf("Error parsing %q: %v", value[0], err)
101+
}
102+
gotMaxDiffEntries = append(gotMaxDiffEntries, int32(i))
103+
} else if key == maxDatabaseEntriesKey {
104+
if len(value) == 0 {
105+
t.Fatalf("Missing value for key %v", key)
106+
}
107+
i, err := strconv.ParseInt(value[0], 10, 32)
108+
if err != nil {
109+
t.Fatalf("Error parsing %q: %v", value[0], err)
110+
}
111+
gotMaxDatabaseEntries = append(gotMaxDatabaseEntries, int32(i))
91112
} else if key != "key" {
92-
t.Fatalf("unexpected request param error for key: %v", key)
113+
t.Fatalf("Unexpected request param error for key: %v", key)
93114
}
94115
}
95116
if p, err = protojson.Marshal(wantResp); err != nil {
96-
t.Fatalf("unexpected json MarshalToString error: %v", err)
117+
t.Fatalf("Unexpected json MarshalToString error: %v", err)
97118
}
98119
if _, err := w.Write([]byte(responseMisformatter + string(p))); err != nil {
99-
t.Fatalf("unexpected ResponseWriter.Write error: %v", err)
120+
t.Fatalf("Unexpected ResponseWriter.Write error: %v", err)
100121
}
101122
}))
102123
defer ts.Close()
@@ -109,6 +130,8 @@ func TestNetAPI(t *testing.T) {
109130
// Test that ListUpdate marshal/unmarshal works.
110131
wantReqThreatType = pb.ThreatType_MALWARE
111132
wantReqCompressionTypes = []pb.CompressionType{0, 1, 2}
133+
wantMaxDiffEntries = []int32{1024}
134+
wantMaxDatabaseEntries = []int32{1024}
112135

113136
wantResp = &pb.ComputeThreatListDiffResponse{
114137
ResponseType: 1,
@@ -121,6 +144,8 @@ func TestNetAPI(t *testing.T) {
121144
ThreatType: wantReqThreatType,
122145
Constraints: &pb.ComputeThreatListDiffRequest_Constraints{
123146
SupportedCompressions: wantReqCompressionTypes,
147+
MaxDiffEntries: 1024,
148+
MaxDatabaseEntries: 1024,
124149
},
125150
VersionToken: []byte{},
126151
}
@@ -140,6 +165,14 @@ func TestNetAPI(t *testing.T) {
140165
if !proto.Equal(gotResp, wantResp) {
141166
t.Errorf("mismatching ListUpdate responses:\ngot %+v\nwant %+v", gotResp, wantResp)
142167
}
168+
if !reflect.DeepEqual(gotMaxDiffEntries, wantMaxDiffEntries) {
169+
t.Errorf("mismatching ListUpdate max diff entries:\ngot %+v\nwant %+v",
170+
gotMaxDiffEntries, wantMaxDiffEntries)
171+
}
172+
if !reflect.DeepEqual(gotMaxDatabaseEntries, wantMaxDatabaseEntries) {
173+
t.Errorf("mismatching ListUpdate max database entries:\ngot %+v\nwant %+v",
174+
gotMaxDatabaseEntries, wantMaxDatabaseEntries)
175+
}
143176

144177
// Test that HashLookup marshal/unmarshal works.
145178
wantReqHashPrefix = []byte("aaaa")

0 commit comments

Comments
 (0)