55 "context"
66 "encoding/json"
77 "fmt"
8+ "io"
89 "net/http"
9- "net/http/httptest"
1010 "os"
1111 "reflect"
1212 "testing"
@@ -19,6 +19,26 @@ import (
1919 "github.com/ignite/cli/v29/ignite/pkg/tarball"
2020)
2121
22+ type roundTripperFunc func (* http.Request ) (* http.Response , error )
23+
24+ func (f roundTripperFunc ) RoundTrip (req * http.Request ) (* http.Response , error ) {
25+ return f (req )
26+ }
27+
28+ func newTestClient (statusCode int , body []byte ) * http.Client {
29+ return & http.Client {
30+ Transport : roundTripperFunc (func (req * http.Request ) (* http.Response , error ) {
31+ return & http.Response {
32+ StatusCode : statusCode ,
33+ Body : io .NopCloser (bytes .NewReader (body )),
34+ Header : make (http.Header ),
35+ ContentLength : int64 (len (body )),
36+ Request : req ,
37+ }, nil
38+ }),
39+ }
40+ }
41+
2242func TestJSONFile_Field (t * testing.T ) {
2343 type (
2444 invalidStruct struct {
@@ -356,17 +376,24 @@ func TestFromURL(t *testing.T) {
356376 t .Run (tt .name , func (t * testing.T ) {
357377 url := tt .args .url
358378 if url == "" {
359- ts := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , _ * http.Request ) {
360- file , err := os .ReadFile (tt .args .filepath )
361- require .NoError (t , err )
362- _ , err = w .Write (file )
363- require .NoError (t , err )
364- }))
365- url = ts .URL
379+ url = "https://example.com/testdata"
380+ }
381+
382+ var body []byte
383+ if tt .args .filepath != "" {
384+ var err error
385+ body , err = os .ReadFile (tt .args .filepath )
386+ require .NoError (t , err )
387+ }
388+
389+ statusCode := http .StatusOK
390+ if tt .err == ErrInvalidURL {
391+ statusCode = http .StatusNotFound
366392 }
393+ client := newTestClient (statusCode , body )
367394
368395 filepath := fmt .Sprintf ("%s/jsonfile.json" , t .TempDir ())
369- got , err := FromURL (context .TODO (), url , filepath , tt .args .tarballFileName )
396+ got , err := FromURLWithClient (context .TODO (), url , filepath , tt .args .tarballFileName , client )
370397 if tt .err != nil {
371398 require .Error (t , err )
372399 require .ErrorIs (t , err , tt .err )
0 commit comments