@@ -17,7 +17,6 @@ import (
17
17
"github.com/netlify/open-api/v2/go/plumbing/operations"
18
18
"github.com/netlify/open-api/v2/go/porcelain/context"
19
19
20
- "github.com/go-openapi/runtime"
21
20
apiClient "github.com/go-openapi/runtime/client"
22
21
"github.com/go-openapi/strfmt"
23
22
"github.com/stretchr/testify/assert"
@@ -93,23 +92,15 @@ func TestOpenAPIClientWithWeirdResponse(t *testing.T) {
93
92
}))
94
93
defer server .Close ()
95
94
96
- httpClient := http .DefaultClient
97
- authInfo := runtime .ClientAuthInfoWriterFunc (func (r runtime.ClientRequest , _ strfmt.Registry ) error {
98
- r .SetHeaderParam ("User-Agent" , "buildbot" )
99
- r .SetHeaderParam ("Authorization" , "Bearer 1234" )
100
- return nil
101
- })
102
-
103
95
hu , _ := url .Parse (server .URL )
104
- tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, httpClient )
96
+ tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, http . DefaultClient )
105
97
client := NewRetryable (tr , strfmt .Default , 1 )
106
98
107
99
body := ioutil .NopCloser (bytes .NewReader ([]byte ("hello world" )))
108
100
params := operations .NewUploadDeployFileParams ().WithDeployID ("1234" ).WithPath ("foo/bar/biz" ).WithFileBody (body )
109
- _ , operationError := client .Operations .UploadDeployFile (params , authInfo )
101
+ _ , operationError := client .Operations .UploadDeployFile (params , nil )
110
102
require .Error (t , operationError )
111
103
require .Equal (t , "[PUT /deploys/{deploy_id}/files/{path}][408] uploadDeployFile default &{Code:408 Message:a message}" , operationError .Error ())
112
-
113
104
}
114
105
115
106
func TestConcurrentFileUpload (t * testing.T ) {
@@ -120,21 +111,14 @@ func TestConcurrentFileUpload(t *testing.T) {
120
111
}))
121
112
defer server .Close ()
122
113
123
- httpClient := http .DefaultClient
124
- authInfo := runtime .ClientAuthInfoWriterFunc (func (r runtime.ClientRequest , _ strfmt.Registry ) error {
125
- r .SetHeaderParam ("User-Agent" , "buildbot" )
126
- r .SetHeaderParam ("Authorization" , "Bearer 1234" )
127
- return nil
128
- })
129
-
130
114
hu , _ := url .Parse (server .URL )
131
- tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, httpClient )
115
+ tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, http . DefaultClient )
132
116
client := NewRetryable (tr , strfmt .Default , 1 )
133
117
for i := 0 ; i < 30 ; i ++ {
134
118
go func () {
135
119
body := ioutil .NopCloser (bytes .NewReader ([]byte ("hello world" )))
136
120
params := operations .NewUploadDeployFileParams ().WithDeployID ("1234" ).WithPath ("foo/bar/biz" ).WithFileBody (body )
137
- _ , _ = client .Operations .UploadDeployFile (params , authInfo )
121
+ _ , _ = client .Operations .UploadDeployFile (params , nil )
138
122
}()
139
123
}
140
124
}
@@ -146,18 +130,11 @@ func TestWaitUntilDeployLive_Timeout(t *testing.T) {
146
130
}))
147
131
defer server .Close ()
148
132
149
- httpClient := http .DefaultClient
150
- authInfo := runtime .ClientAuthInfoWriterFunc (func (r runtime.ClientRequest , _ strfmt.Registry ) error {
151
- r .SetHeaderParam ("User-Agent" , "buildbot" )
152
- r .SetHeaderParam ("Authorization" , "Bearer 1234" )
153
- return nil
154
- })
155
-
156
133
hu , _ := url .Parse (server .URL )
157
- tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, httpClient )
134
+ tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, http . DefaultClient )
158
135
client := NewRetryable (tr , strfmt .Default , 1 )
159
136
160
- ctx := context .WithAuthInfo (gocontext .Background (), authInfo )
137
+ ctx := context .WithAuthInfo (gocontext .Background (), apiClient . BearerToken ( "token" ) )
161
138
ctx , _ = gocontext .WithTimeout (ctx , 50 * time .Millisecond )
162
139
_ , err := client .WaitUntilDeployLive (ctx , & models.Deploy {})
163
140
assert .Error (t , err )
@@ -191,6 +168,38 @@ func TestWalk_IgnoreNodeModulesInRoot(t *testing.T) {
191
168
assert .NotNil (t , files .Files ["more/node_modules/inner-package" ])
192
169
}
193
170
171
+ func TestUploadFiles_Cancelation (t * testing.T ) {
172
+ ctx , cancel := gocontext .WithCancel (gocontext .Background ())
173
+ server := httptest .NewServer (http .HandlerFunc (func (rw http.ResponseWriter , req * http.Request ) {
174
+ cancel () // Cancel deploy as soon as first file upload is attempted.
175
+ rw .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
176
+ rw .Write ([]byte (`{ "state": "canceled" }` ))
177
+ }))
178
+ defer server .Close ()
179
+
180
+ hu , _ := url .Parse (server .URL )
181
+ tr := apiClient .NewWithClient (hu .Host , "/api/v1" , []string {"http" }, http .DefaultClient )
182
+ client := NewRetryable (tr , strfmt .Default , 1 )
183
+ client .uploadLimit = 1
184
+ ctx = context .WithAuthInfo (ctx , apiClient .BearerToken ("token" ))
185
+
186
+ // Create some files to deploy
187
+ dir , err := ioutil .TempDir ("" , "deploy" )
188
+ require .NoError (t , err )
189
+ defer os .RemoveAll (dir )
190
+ require .NoError (t , ioutil .WriteFile (filepath .Join (dir , "foo.html" ), []byte ("Hello" ), 0644 ))
191
+ require .NoError (t , ioutil .WriteFile (filepath .Join (dir , "bar.html" ), []byte ("World" ), 0644 ))
192
+
193
+ files , err := walk (dir , nil , false , false )
194
+ require .NoError (t , err )
195
+ d := & models.Deploy {}
196
+ for _ , bundle := range files .Files {
197
+ d .Required = append (d .Required , bundle .Sum )
198
+ }
199
+ err = client .uploadFiles (ctx , d , files , nil , fileUpload , time .Minute )
200
+ require .ErrorIs (t , err , gocontext .Canceled )
201
+ }
202
+
194
203
func TestReadZipRuntime (t * testing.T ) {
195
204
runtime , err := readZipRuntime ("../internal/data/hello-rs-function-test.zip" )
196
205
if err != nil {
0 commit comments