github.com/IRelaxxx/servefiles/v3@v3.4.6/gin_adapter/handler_test.go (about)

     1  package gin_adapter_test
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"net/http/httptest"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/IRelaxxx/servefiles/v3/gin_adapter"
    11  	"github.com/gin-gonic/gin"
    12  	. "github.com/onsi/gomega"
    13  	"github.com/spf13/afero"
    14  )
    15  
    16  func ExampleHandlerFunc() {
    17  	// This is a webserver using the asset handler provided by
    18  	// github.com/IRelaxxx/servefiles/v3, which has enhanced
    19  	// HTTP expiry, cache control, compression etc.
    20  	// 'Normal' bespoke handlers are included as needed.
    21  
    22  	// where the assets are stored (replace as required)
    23  	localPath := "./assets"
    24  
    25  	// how long we allow user agents to cache assets
    26  	// (this is in addition to conditional requests, see
    27  	// RFC7234 https://tools.ietf.org/html/rfc7234#section-5.2.2.8)
    28  	maxAge := time.Hour
    29  
    30  	h := gin_adapter.NewAssetHandler(localPath).
    31  		WithMaxAge(maxAge).
    32  		WithNotFound(http.NotFoundHandler()). // supply your own
    33  		StripOff(1).
    34  		HandlerFunc("filepath")
    35  
    36  	router := gin.Default()
    37  	// ... add other routes / handlers / middleware as required
    38  	router.GET("/files/*filepath", h)
    39  
    40  	log.Fatal(http.ListenAndServe(":8080", router))
    41  }
    42  
    43  func TestHandlerFunc(t *testing.T) {
    44  	g := NewGomegaWithT(t)
    45  
    46  	maxAge := time.Hour
    47  	fs := afero.NewMemMapFs()
    48  	fs.MkdirAll("/foo/bar", 0755)
    49  	afero.WriteFile(fs, "/foo/bar/x.txt", []byte("hello"), 0644)
    50  
    51  	h := gin_adapter.NewAssetHandlerFS(fs).
    52  		WithMaxAge(maxAge).
    53  		WithNotFound(http.NotFoundHandler()). // supply your own
    54  		StripOff(1).
    55  		HandlerFunc("filepath")
    56  
    57  	router := gin.Default()
    58  	// ... add other routes / handlers / middleware as required
    59  	router.GET("/files/*filepath", h)
    60  	router.HEAD("/files/*filepath", h)
    61  
    62  	r, _ := http.NewRequest(http.MethodGet, "http://localhost/files/101/foo/bar/x.txt", nil)
    63  	w := httptest.NewRecorder()
    64  	router.ServeHTTP(w, r)
    65  
    66  	g.Expect(w.Code).To(Equal(200))
    67  	g.Expect(w.Header().Get("Content-Type")).To(Equal("text/plain; charset=utf-8"))
    68  	g.Expect(w.Header().Get("Expires")).NotTo(Equal(""))
    69  	g.Expect(w.Body.Len()).To(Equal(5))
    70  
    71  	r, _ = http.NewRequest(http.MethodHead, "http://localhost/files/101/foo/bar/x.txt", nil)
    72  	w = httptest.NewRecorder()
    73  	router.ServeHTTP(w, r)
    74  
    75  	g.Expect(w.Code).To(Equal(200))
    76  	g.Expect(w.Header().Get("Content-Type")).To(Equal("text/plain; charset=utf-8"))
    77  	g.Expect(w.Header().Get("Expires")).NotTo(Equal(""))
    78  	g.Expect(w.Body.Len()).To(Equal(0))
    79  
    80  	r, _ = http.NewRequest(http.MethodHead, "http://localhost/files/101/foo/baz.png", nil)
    81  	w = httptest.NewRecorder()
    82  	router.ServeHTTP(w, r)
    83  
    84  	g.Expect(w.Code).To(Equal(404))
    85  }