-
Notifications
You must be signed in to change notification settings - Fork 3
/
request_builder_test.go
109 lines (103 loc) · 2.57 KB
/
request_builder_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package vertexai
import (
"bytes"
"context"
"encoding/json"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_newRequestBuilder(t *testing.T) {
got := newRequestBuilder()
assert.NotNil(t, got)
}
func Test_buildRequest(t *testing.T) {
type testBody struct {
Content string `json:"content"`
}
content := "content"
type args struct {
method string
projectID string
endpointID string
urlSuffix string
req any
}
rawReq := testBody{Content: content}
jsonReq, err := json.Marshal(rawReq)
if err != nil {
t.Errorf("failed to setup test request: %v", err)
}
httpReq, err := http.NewRequestWithContext(context.Background(),
"POST",
"https://us-central1-aiplatform.googleapis.com/v1/projects/cloud-large-language-models/locations/us-central1/publishers/google/models/4511608470067216384:predict",
bytes.NewBuffer(jsonReq),
)
if err != nil {
t.Errorf("failed to setup test httpRequest: %v", err)
}
httpReqNilBody, err := http.NewRequestWithContext(context.Background(),
"POST",
"https://us-central1-aiplatform.googleapis.com/v1/projects/cloud-large-language-models/locations/us-central1/publishers/google/models/4511608470067216384:predict",
nil,
)
if err != nil {
t.Errorf("failed to setup test httpRequest: %v", err)
}
tests := []struct {
name string
args args
want *http.Request
wantErr bool
}{
{
name: "success",
args: args{
req: testBody{Content: content},
method: "POST",
projectID: "cloud-large-language-models",
endpointID: "4511608470067216384",
urlSuffix: ":predict",
},
want: httpReq,
},
{
name: "nil request",
args: args{
method: "POST",
projectID: "cloud-large-language-models",
endpointID: "4511608470067216384",
urlSuffix: ":predict",
},
want: httpReqNilBody,
},
{
name: "invalid method",
args: args{
req: testBody{Content: content},
method: "!@#$",
projectID: "cloud-large-language-models",
endpointID: "4511608470067216384",
urlSuffix: ":predict",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := (&httpRequestBuilder{}).build(
context.Background(),
tt.args.method,
tt.args.projectID,
tt.args.endpointID,
tt.args.urlSuffix, tt.args.req)
assert.Equal(t, tt.wantErr, err != nil)
if !tt.wantErr {
assert.Equal(t, tt.want.Method, got.Method)
assert.Equal(t, tt.want.URL, got.URL)
assert.Equal(t, tt.want.Header, got.Header)
assert.Equal(t, tt.want.Body, got.Body)
}
})
}
}