github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/builder/dockerfile/copy_test.go (about)

     1  package dockerfile // import "github.com/docker/docker/builder/dockerfile"
     2  
     3  import (
     4  	"net/http"
     5  	"testing"
     6  
     7  	"github.com/docker/docker/pkg/containerfs"
     8  	"gotest.tools/v3/assert"
     9  	is "gotest.tools/v3/assert/cmp"
    10  	"gotest.tools/v3/fs"
    11  )
    12  
    13  func TestIsExistingDirectory(t *testing.T) {
    14  	tmpfile := fs.NewFile(t, "file-exists-test", fs.WithContent("something"))
    15  	defer tmpfile.Remove()
    16  	tmpdir := fs.NewDir(t, "dir-exists-test")
    17  	defer tmpdir.Remove()
    18  
    19  	var testcases = []struct {
    20  		doc      string
    21  		path     string
    22  		expected bool
    23  	}{
    24  		{
    25  			doc:      "directory exists",
    26  			path:     tmpdir.Path(),
    27  			expected: true,
    28  		},
    29  		{
    30  			doc:      "path doesn't exist",
    31  			path:     "/bogus/path/does/not/exist",
    32  			expected: false,
    33  		},
    34  		{
    35  			doc:      "file exists",
    36  			path:     tmpfile.Path(),
    37  			expected: false,
    38  		},
    39  	}
    40  
    41  	for _, testcase := range testcases {
    42  		result, err := isExistingDirectory(&copyEndpoint{driver: containerfs.NewLocalDriver(), path: testcase.path})
    43  		if !assert.Check(t, err) {
    44  			continue
    45  		}
    46  		assert.Check(t, is.Equal(testcase.expected, result), testcase.doc)
    47  	}
    48  }
    49  
    50  func TestGetFilenameForDownload(t *testing.T) {
    51  	var testcases = []struct {
    52  		path        string
    53  		disposition string
    54  		expected    string
    55  	}{
    56  		{
    57  			path:     "http://www.example.com/",
    58  			expected: "",
    59  		},
    60  		{
    61  			path:     "http://www.example.com/xyz",
    62  			expected: "xyz",
    63  		},
    64  		{
    65  			path:     "http://www.example.com/xyz.html",
    66  			expected: "xyz.html",
    67  		},
    68  		{
    69  			path:     "http://www.example.com/xyz/",
    70  			expected: "",
    71  		},
    72  		{
    73  			path:     "http://www.example.com/xyz/uvw",
    74  			expected: "uvw",
    75  		},
    76  		{
    77  			path:     "http://www.example.com/xyz/uvw.html",
    78  			expected: "uvw.html",
    79  		},
    80  		{
    81  			path:     "http://www.example.com/xyz/uvw/",
    82  			expected: "",
    83  		},
    84  		{
    85  			path:     "/",
    86  			expected: "",
    87  		},
    88  		{
    89  			path:     "/xyz",
    90  			expected: "xyz",
    91  		},
    92  		{
    93  			path:     "/xyz.html",
    94  			expected: "xyz.html",
    95  		},
    96  		{
    97  			path:     "/xyz/",
    98  			expected: "",
    99  		},
   100  		{
   101  			path:        "/xyz/",
   102  			disposition: "attachment; filename=xyz.html",
   103  			expected:    "xyz.html",
   104  		},
   105  		{
   106  			disposition: "",
   107  			expected:    "",
   108  		},
   109  		{
   110  			disposition: "attachment; filename=xyz",
   111  			expected:    "xyz",
   112  		},
   113  		{
   114  			disposition: "attachment; filename=xyz.html",
   115  			expected:    "xyz.html",
   116  		},
   117  		{
   118  			disposition: "attachment; filename=\"xyz\"",
   119  			expected:    "xyz",
   120  		},
   121  		{
   122  			disposition: "attachment; filename=\"xyz.html\"",
   123  			expected:    "xyz.html",
   124  		},
   125  		{
   126  			disposition: "attachment; filename=\"/xyz.html\"",
   127  			expected:    "xyz.html",
   128  		},
   129  		{
   130  			disposition: "attachment; filename=\"/xyz/uvw\"",
   131  			expected:    "uvw",
   132  		},
   133  		{
   134  			disposition: "attachment; filename=\"Naïve file.txt\"",
   135  			expected:    "Naïve file.txt",
   136  		},
   137  	}
   138  	for _, testcase := range testcases {
   139  		resp := http.Response{
   140  			Header: make(map[string][]string),
   141  		}
   142  		if testcase.disposition != "" {
   143  			resp.Header.Add("Content-Disposition", testcase.disposition)
   144  		}
   145  		filename := getFilenameForDownload(testcase.path, &resp)
   146  		assert.Check(t, is.Equal(testcase.expected, filename))
   147  	}
   148  }