Source file src/net/http/filetransport_test.go

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package http
     6  
     7  import (
     8  	"io"
     9  	"os"
    10  	"path/filepath"
    11  	"testing"
    12  	"testing/fstest"
    13  )
    14  
    15  func checker(t *testing.T) func(string, error) {
    16  	return func(call string, err error) {
    17  		if err == nil {
    18  			return
    19  		}
    20  		t.Fatalf("%s: %v", call, err)
    21  	}
    22  }
    23  
    24  func TestFileTransport(t *testing.T) {
    25  	check := checker(t)
    26  
    27  	dname := t.TempDir()
    28  	fname := filepath.Join(dname, "foo.txt")
    29  	err := os.WriteFile(fname, []byte("Bar"), 0644)
    30  	check("WriteFile", err)
    31  	defer os.Remove(fname)
    32  
    33  	tr := &Transport{}
    34  	tr.RegisterProtocol("file", NewFileTransport(Dir(dname)))
    35  	c := &Client{Transport: tr}
    36  
    37  	fooURLs := []string{"file:///foo.txt", "file://../foo.txt"}
    38  	for _, urlstr := range fooURLs {
    39  		res, err := c.Get(urlstr)
    40  		check("Get "+urlstr, err)
    41  		if res.StatusCode != 200 {
    42  			t.Errorf("for %s, StatusCode = %d, want 200", urlstr, res.StatusCode)
    43  		}
    44  		if res.ContentLength != -1 {
    45  			t.Errorf("for %s, ContentLength = %d, want -1", urlstr, res.ContentLength)
    46  		}
    47  		if res.Body == nil {
    48  			t.Fatalf("for %s, nil Body", urlstr)
    49  		}
    50  		slurp, err := io.ReadAll(res.Body)
    51  		res.Body.Close()
    52  		check("ReadAll "+urlstr, err)
    53  		if string(slurp) != "Bar" {
    54  			t.Errorf("for %s, got content %q, want %q", urlstr, string(slurp), "Bar")
    55  		}
    56  		if got := res.Request.URL.String(); got != urlstr {
    57  			t.Errorf("for %s, Response.Request.URL = %s, want = %s", urlstr, got, urlstr)
    58  		}
    59  	}
    60  
    61  	const badURL = "file://../no-exist.txt"
    62  	res, err := c.Get(badURL)
    63  	check("Get "+badURL, err)
    64  	if res.StatusCode != 404 {
    65  		t.Errorf("for %s, StatusCode = %d, want 404", badURL, res.StatusCode)
    66  	}
    67  	res.Body.Close()
    68  }
    69  
    70  func TestFileTransportFS(t *testing.T) {
    71  	check := checker(t)
    72  
    73  	fsys := fstest.MapFS{
    74  		"index.html": {Data: []byte("index.html says hello")},
    75  	}
    76  
    77  	tr := &Transport{}
    78  	tr.RegisterProtocol("file", NewFileTransportFS(fsys))
    79  	c := &Client{Transport: tr}
    80  
    81  	for fname, mfile := range fsys {
    82  		urlstr := "file:///" + fname
    83  		res, err := c.Get(urlstr)
    84  		check("Get "+urlstr, err)
    85  		if res.StatusCode != 200 {
    86  			t.Errorf("for %s, StatusCode = %d, want 200", urlstr, res.StatusCode)
    87  		}
    88  		if res.ContentLength != -1 {
    89  			t.Errorf("for %s, ContentLength = %d, want -1", urlstr, res.ContentLength)
    90  		}
    91  		if res.Body == nil {
    92  			t.Fatalf("for %s, nil Body", urlstr)
    93  		}
    94  		slurp, err := io.ReadAll(res.Body)
    95  		res.Body.Close()
    96  		check("ReadAll "+urlstr, err)
    97  		if string(slurp) != string(mfile.Data) {
    98  			t.Errorf("for %s, got content %q, want %q", urlstr, string(slurp), "Bar")
    99  		}
   100  	}
   101  
   102  	const badURL = "file://../no-exist.txt"
   103  	res, err := c.Get(badURL)
   104  	check("Get "+badURL, err)
   105  	if res.StatusCode != 404 {
   106  		t.Errorf("for %s, StatusCode = %d, want 404", badURL, res.StatusCode)
   107  	}
   108  	res.Body.Close()
   109  }
   110  

View as plain text