@@ -17,7 +17,6 @@ import (
1717 "github.com/netlify/open-api/v2/go/plumbing/operations"
1818 "github.com/netlify/open-api/v2/go/porcelain/context"
1919
20- "github.com/go-openapi/runtime"
2120 apiClient "github.com/go-openapi/runtime/client"
2221 "github.com/go-openapi/strfmt"
2322 "github.com/stretchr/testify/assert"
@@ -93,23 +92,15 @@ func TestOpenAPIClientWithWeirdResponse(t *testing.T) {
9392 }))
9493 defer server .Close ()
9594
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-
10395 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 )
10597 client := NewRetryable (tr , strfmt .Default , 1 )
10698
10799 body := ioutil .NopCloser (bytes .NewReader ([]byte ("hello world" )))
108100 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 )
110102 require .Error (t , operationError )
111103 require .Equal (t , "[PUT /deploys/{deploy_id}/files/{path}][408] uploadDeployFile default &{Code:408 Message:a message}" , operationError .Error ())
112-
113104}
114105
115106func TestConcurrentFileUpload (t * testing.T ) {
@@ -120,21 +111,14 @@ func TestConcurrentFileUpload(t *testing.T) {
120111 }))
121112 defer server .Close ()
122113
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-
130114 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 )
132116 client := NewRetryable (tr , strfmt .Default , 1 )
133117 for i := 0 ; i < 30 ; i ++ {
134118 go func () {
135119 body := ioutil .NopCloser (bytes .NewReader ([]byte ("hello world" )))
136120 params := operations .NewUploadDeployFileParams ().WithDeployID ("1234" ).WithPath ("foo/bar/biz" ).WithFileBody (body )
137- _ , _ = client .Operations .UploadDeployFile (params , authInfo )
121+ _ , _ = client .Operations .UploadDeployFile (params , nil )
138122 }()
139123 }
140124}
@@ -146,18 +130,11 @@ func TestWaitUntilDeployLive_Timeout(t *testing.T) {
146130 }))
147131 defer server .Close ()
148132
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-
156133 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 )
158135 client := NewRetryable (tr , strfmt .Default , 1 )
159136
160- ctx := context .WithAuthInfo (gocontext .Background (), authInfo )
137+ ctx := context .WithAuthInfo (gocontext .Background (), apiClient . BearerToken ( "token" ) )
161138 ctx , _ = gocontext .WithTimeout (ctx , 50 * time .Millisecond )
162139 _ , err := client .WaitUntilDeployLive (ctx , & models.Deploy {})
163140 assert .Error (t , err )
@@ -191,6 +168,38 @@ func TestWalk_IgnoreNodeModulesInRoot(t *testing.T) {
191168 assert .NotNil (t , files .Files ["more/node_modules/inner-package" ])
192169}
193170
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+
194203func TestReadZipRuntime (t * testing.T ) {
195204 runtime , err := readZipRuntime ("../internal/data/hello-rs-function-test.zip" )
196205 if err != nil {
0 commit comments