github.com/gofiber/fiber/v2@v2.47.0/middleware/filesystem/filesystem_test.go (about)

     1  //nolint:bodyclose // Much easier to just ignore memory leaks in tests
     2  package filesystem
     3  
     4  import (
     5  	"context"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"testing"
     9  
    10  	"github.com/gofiber/fiber/v2"
    11  	"github.com/gofiber/fiber/v2/utils"
    12  )
    13  
    14  // go test -run Test_FileSystem
    15  func Test_FileSystem(t *testing.T) {
    16  	t.Parallel()
    17  	app := fiber.New()
    18  
    19  	app.Use("/test", New(Config{
    20  		Root: http.Dir("../../.github/testdata/fs"),
    21  	}))
    22  
    23  	app.Use("/dir", New(Config{
    24  		Root:   http.Dir("../../.github/testdata/fs"),
    25  		Browse: true,
    26  	}))
    27  
    28  	app.Get("/", func(c *fiber.Ctx) error {
    29  		return c.SendString("Hello, World!")
    30  	})
    31  
    32  	app.Use("/spatest", New(Config{
    33  		Root:         http.Dir("../../.github/testdata/fs"),
    34  		Index:        "index.html",
    35  		NotFoundFile: "index.html",
    36  	}))
    37  
    38  	app.Use("/prefix", New(Config{
    39  		Root:       http.Dir("../../.github/testdata/fs"),
    40  		PathPrefix: "img",
    41  	}))
    42  
    43  	tests := []struct {
    44  		name         string
    45  		url          string
    46  		statusCode   int
    47  		contentType  string
    48  		modifiedTime string
    49  	}{
    50  		{
    51  			name:        "Should be returns status 200 with suitable content-type",
    52  			url:         "/test/index.html",
    53  			statusCode:  200,
    54  			contentType: "text/html",
    55  		},
    56  		{
    57  			name:        "Should be returns status 200 with suitable content-type",
    58  			url:         "/test",
    59  			statusCode:  200,
    60  			contentType: "text/html",
    61  		},
    62  		{
    63  			name:        "Should be returns status 200 with suitable content-type",
    64  			url:         "/test/css/style.css",
    65  			statusCode:  200,
    66  			contentType: "text/css",
    67  		},
    68  		{
    69  			name:       "Should be returns status 404",
    70  			url:        "/test/nofile.js",
    71  			statusCode: 404,
    72  		},
    73  		{
    74  			name:       "Should be returns status 404",
    75  			url:        "/test/nofile",
    76  			statusCode: 404,
    77  		},
    78  		{
    79  			name:        "Should be returns status 200",
    80  			url:         "/",
    81  			statusCode:  200,
    82  			contentType: "text/plain; charset=utf-8",
    83  		},
    84  		{
    85  			name:       "Should be returns status 403",
    86  			url:        "/test/img",
    87  			statusCode: 403,
    88  		},
    89  		{
    90  			name:        "Should list the directory contents",
    91  			url:         "/dir/img",
    92  			statusCode:  200,
    93  			contentType: "text/html",
    94  		},
    95  		{
    96  			name:        "Should list the directory contents",
    97  			url:         "/dir/img/",
    98  			statusCode:  200,
    99  			contentType: "text/html",
   100  		},
   101  		{
   102  			name:        "Should be returns status 200",
   103  			url:         "/dir/img/fiber.png",
   104  			statusCode:  200,
   105  			contentType: "image/png",
   106  		},
   107  		{
   108  			name:        "Should be return status 200",
   109  			url:         "/spatest/doesnotexist",
   110  			statusCode:  200,
   111  			contentType: "text/html",
   112  		},
   113  		{
   114  			name:        "PathPrefix should be applied",
   115  			url:         "/prefix/fiber.png",
   116  			statusCode:  200,
   117  			contentType: "image/png",
   118  		},
   119  	}
   120  
   121  	for _, tt := range tests {
   122  		t.Run(tt.name, func(t *testing.T) {
   123  			t.Parallel()
   124  			resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, tt.url, nil))
   125  			utils.AssertEqual(t, nil, err)
   126  			utils.AssertEqual(t, tt.statusCode, resp.StatusCode)
   127  
   128  			if tt.contentType != "" {
   129  				ct := resp.Header.Get("Content-Type")
   130  				utils.AssertEqual(t, tt.contentType, ct)
   131  			}
   132  		})
   133  	}
   134  }
   135  
   136  // go test -run Test_FileSystem_Next
   137  func Test_FileSystem_Next(t *testing.T) {
   138  	t.Parallel()
   139  	app := fiber.New()
   140  	app.Use(New(Config{
   141  		Root: http.Dir("../../.github/testdata/fs"),
   142  		Next: func(_ *fiber.Ctx) bool {
   143  			return true
   144  		},
   145  	}))
   146  
   147  	resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
   148  	utils.AssertEqual(t, nil, err)
   149  	utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
   150  }
   151  
   152  func Test_FileSystem_NonGetAndHead(t *testing.T) {
   153  	t.Parallel()
   154  	app := fiber.New()
   155  
   156  	app.Use("/test", New(Config{
   157  		Root: http.Dir("../../.github/testdata/fs"),
   158  	}))
   159  
   160  	resp, err := app.Test(httptest.NewRequest(fiber.MethodPost, "/test", nil))
   161  	utils.AssertEqual(t, nil, err)
   162  	utils.AssertEqual(t, 404, resp.StatusCode)
   163  }
   164  
   165  func Test_FileSystem_Head(t *testing.T) {
   166  	t.Parallel()
   167  	app := fiber.New()
   168  
   169  	app.Use("/test", New(Config{
   170  		Root: http.Dir("../../.github/testdata/fs"),
   171  	}))
   172  
   173  	req, err := http.NewRequestWithContext(context.Background(), fiber.MethodHead, "/test", nil)
   174  	utils.AssertEqual(t, nil, err)
   175  	resp, err := app.Test(req)
   176  	utils.AssertEqual(t, nil, err)
   177  	utils.AssertEqual(t, 200, resp.StatusCode)
   178  }
   179  
   180  func Test_FileSystem_NoRoot(t *testing.T) {
   181  	t.Parallel()
   182  	defer func() {
   183  		utils.AssertEqual(t, "filesystem: Root cannot be nil", recover())
   184  	}()
   185  
   186  	app := fiber.New()
   187  	app.Use(New())
   188  	_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
   189  	utils.AssertEqual(t, nil, err)
   190  }
   191  
   192  func Test_FileSystem_UsingParam(t *testing.T) {
   193  	t.Parallel()
   194  	app := fiber.New()
   195  
   196  	app.Use("/:path", func(c *fiber.Ctx) error {
   197  		return SendFile(c, http.Dir("../../.github/testdata/fs"), c.Params("path")+".html")
   198  	})
   199  
   200  	req, err := http.NewRequestWithContext(context.Background(), fiber.MethodHead, "/index", nil)
   201  	utils.AssertEqual(t, nil, err)
   202  	resp, err := app.Test(req)
   203  	utils.AssertEqual(t, nil, err)
   204  	utils.AssertEqual(t, 200, resp.StatusCode)
   205  }
   206  
   207  func Test_FileSystem_UsingParam_NonFile(t *testing.T) {
   208  	t.Parallel()
   209  	app := fiber.New()
   210  
   211  	app.Use("/:path", func(c *fiber.Ctx) error {
   212  		return SendFile(c, http.Dir("../../.github/testdata/fs"), c.Params("path")+".html")
   213  	})
   214  
   215  	req, err := http.NewRequestWithContext(context.Background(), fiber.MethodHead, "/template", nil)
   216  	utils.AssertEqual(t, nil, err)
   217  	resp, err := app.Test(req)
   218  	utils.AssertEqual(t, nil, err)
   219  	utils.AssertEqual(t, 404, resp.StatusCode)
   220  }
   221  
   222  func Test_FileSystem_UsingContentTypeCharset(t *testing.T) {
   223  	t.Parallel()
   224  	app := fiber.New()
   225  	app.Use(New(Config{
   226  		Root:               http.Dir("../../.github/testdata/fs/index.html"),
   227  		ContentTypeCharset: "UTF-8",
   228  	}))
   229  
   230  	resp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
   231  	utils.AssertEqual(t, nil, err)
   232  	utils.AssertEqual(t, 200, resp.StatusCode)
   233  	utils.AssertEqual(t, "text/html; charset=UTF-8", resp.Header.Get("Content-Type"))
   234  }