anderfv21.dev/ason@v0.0.3/dl/downloader_test.go (about)

     1  package dl_test
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	"anderfv21.dev/ason/dl"
     8  )
     9  
    10  func TestDownload(t *testing.T) {
    11  	os.Chdir("./testdata")
    12  	defer os.Chdir("..")
    13  
    14  	type args struct {
    15  		info dl.Info
    16  	}
    17  	tests := []struct {
    18  		name    string
    19  		args    args
    20  		wantErr bool
    21  		panics  bool
    22  	}{
    23  		{
    24  			name: "download with invalid url",
    25  			args: args{
    26  				info: dl.Info{
    27  					URL: "~",
    28  				},
    29  			},
    30  			wantErr: true,
    31  			panics:  false,
    32  		},
    33  		{
    34  			name: "download with invalid outputfile",
    35  			args: args{
    36  				info: dl.Info{
    37  					URL:        "https://example.com/",
    38  					Outputfile: string(os.PathSeparator),
    39  				},
    40  			},
    41  			wantErr: true,
    42  		},
    43  		{
    44  			name: "download with success",
    45  			args: args{
    46  				info: dl.Info{
    47  					URL:        "https://example.com/",
    48  					Outputfile: "download_with_success",
    49  				},
    50  			},
    51  			wantErr: false,
    52  			panics:  false,
    53  		},
    54  		{
    55  			name: "download guess outputfile from http header",
    56  			args: args{
    57  				info: dl.Info{
    58  					URL:        "https://www.hatigarmscans.net/download/tales-of-demons-and-gods/1267", //       "https://example.com/",
    59  					Outputfile: "",                                                                     // to be guessed
    60  				},
    61  			},
    62  			wantErr: false,
    63  			panics:  false,
    64  		},
    65  		{
    66  			name: "download with outputfile guessed from url",
    67  			args: args{
    68  				info: dl.Info{
    69  					URL:        "https://example.com/",
    70  					Outputfile: "", // to be guessed
    71  				},
    72  			},
    73  			wantErr: false,
    74  			panics:  false,
    75  		},
    76  	}
    77  
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			defer func() {
    81  				if obj := recover(); (obj != nil) != tt.panics {
    82  					t.Errorf("wants panic = %v, got %v(%v)", tt.panics, (obj != nil), obj)
    83  				}
    84  			}()
    85  			err := dl.Download(tt.args.info)
    86  			if (err != nil) != tt.wantErr {
    87  				t.Errorf("Download() error = %v, wantErr %v", err, tt.wantErr)
    88  				return
    89  			}
    90  
    91  		})
    92  	}
    93  }