github.com/isi-lincoln/grab@v2.0.1-0.20200331080741-9f014744ee41+incompatible/util_test.go (about)

     1  package grab
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"net/url"
     7  	"testing"
     8  )
     9  
    10  func TestURLFilenames(t *testing.T) {
    11  	t.Run("Valid", func(t *testing.T) {
    12  		expect := "filename"
    13  		testCases := []string{
    14  			"http://test.com/filename",
    15  			"http://test.com/path/filename",
    16  			"http://test.com/deep/path/filename",
    17  			"http://test.com/filename?with=args",
    18  			"http://test.com/filename#with-fragment",
    19  			"http://test.com/filename?with=args&and#with-fragment",
    20  		}
    21  
    22  		for _, tc := range testCases {
    23  			req, _ := http.NewRequest("GET", tc, nil)
    24  			resp := &http.Response{
    25  				Request: req,
    26  			}
    27  			actual, err := guessFilename(resp)
    28  			if err != nil {
    29  				t.Errorf("%v", err)
    30  			}
    31  
    32  			if actual != expect {
    33  				t.Errorf("expected '%v', got '%v'", expect, actual)
    34  			}
    35  		}
    36  	})
    37  
    38  	t.Run("Invalid", func(t *testing.T) {
    39  		testCases := []string{
    40  			"http://test.com",
    41  			"http://test.com/",
    42  			"http://test.com/filename/",
    43  			"http://test.com/filename/?with=args",
    44  			"http://test.com/filename/#with-fragment",
    45  			"http://test.com/filename\x00",
    46  		}
    47  
    48  		for _, tc := range testCases {
    49  			req, _ := http.NewRequest("GET", tc, nil)
    50  			resp := &http.Response{
    51  				Request: req,
    52  			}
    53  
    54  			_, err := guessFilename(resp)
    55  			if err != ErrNoFilename {
    56  				t.Errorf("expected '%v', got '%v'", ErrNoFilename, err)
    57  			}
    58  		}
    59  	})
    60  }
    61  
    62  func TestHeaderFilenames(t *testing.T) {
    63  	u, _ := url.ParseRequestURI("http://test.com/badfilename")
    64  	resp := &http.Response{
    65  		Request: &http.Request{
    66  			URL: u,
    67  		},
    68  		Header: http.Header{},
    69  	}
    70  
    71  	setFilename := func(resp *http.Response, filename string) {
    72  		resp.Header.Set("Content-Disposition", fmt.Sprintf("attachment;filename=\"%s\"", filename))
    73  	}
    74  
    75  	t.Run("Valid", func(t *testing.T) {
    76  		expect := "filename"
    77  		testCases := []string{
    78  			"filename",
    79  			"path/filename",
    80  			"/path/filename",
    81  			"../../filename",
    82  			"/path/../../filename",
    83  			"/../../././///filename",
    84  		}
    85  
    86  		for _, tc := range testCases {
    87  			setFilename(resp, tc)
    88  			actual, err := guessFilename(resp)
    89  			if err != nil {
    90  				t.Errorf("error (%v): %v", tc, err)
    91  			}
    92  
    93  			if actual != expect {
    94  				t.Errorf("expected '%v' (%v), got '%v'", expect, tc, actual)
    95  			}
    96  		}
    97  	})
    98  
    99  	t.Run("Invalid", func(t *testing.T) {
   100  		testCases := []string{
   101  			"",
   102  			"/",
   103  			".",
   104  			"/.",
   105  			"/./",
   106  			"..",
   107  			"../",
   108  			"/../",
   109  			"/path/",
   110  			"../path/",
   111  			"filename\x00",
   112  			"filename/",
   113  			"filename//",
   114  			"filename/..",
   115  		}
   116  
   117  		for _, tc := range testCases {
   118  			setFilename(resp, tc)
   119  			if actual, err := guessFilename(resp); err != ErrNoFilename {
   120  				t.Errorf("expected: %v (%v), got: %v (%v)", ErrNoFilename, tc, err, actual)
   121  			}
   122  		}
   123  	})
   124  }
   125  
   126  func TestHeaderWithMissingDirective(t *testing.T) {
   127  	u, _ := url.ParseRequestURI("http://test.com/filename")
   128  	resp := &http.Response{
   129  		Request: &http.Request{
   130  			URL: u,
   131  		},
   132  		Header: http.Header{},
   133  	}
   134  
   135  	setHeader := func(resp *http.Response, value string) {
   136  		resp.Header.Set("Content-Disposition", value)
   137  	}
   138  
   139  	t.Run("Valid", func(t *testing.T) {
   140  		expect := "filename"
   141  		testCases := []string{
   142  			"inline",
   143  			"attachment",
   144  		}
   145  
   146  		for _, tc := range testCases {
   147  			setHeader(resp, tc)
   148  			actual, err := guessFilename(resp)
   149  			if err != nil {
   150  				t.Errorf("error (%v): %v", tc, err)
   151  			}
   152  
   153  			if actual != expect {
   154  				t.Errorf("expected '%v' (%v), got '%v'", expect, tc, actual)
   155  			}
   156  		}
   157  	})
   158  }